Results 1 to 3 of 3

Thread: how to read from serial port

  1. Post how to read from serial port

    Please i would like for any one to look into thi code and tell me what is wrong with it and probably profer solution to it. I want to read from a serial port, but when i run this code on DEV C++ compiler, i get this first error from the hSerial + CreatFile("COM23".......,
    So please i need your help.

    expected constructor, destructor, or type conversion before '=' token


    #include <windows.h.>



    HANDLE hSerial;
    hSerial = CreateFile("COM23",
    gszPort,
    GENERIC_READ | GENERIC_WRITE,
    0,
    0,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);
    if(hSerial==INVALID_HANDLE_VALUE){
    if(GetLastError()==ERROR_FILE_NOT_FOUND){
    //serial port does not exist. Inform user.
    }
    //some other error occurred. Inform user.
    }
    DCB dcbSerialParams = {0};
    dcbSerial.DCBlength=sizeof(dcbSerialParams);
    if (!GetCommState(hSerial, &dcbSerialParams)) {
    //error getting state
    }
    dcbSerialParams.BaudRate=CBR_9600;
    dcbSerialParams.ByteSize=1;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;
    if(!SetCommState(hSerial, &dcbSerialParams)){
    //error setting serial port state
    }
    COMMTIMEOUTS timeouts={0};
    timeouts.ReadIntervalTimeout=50;
    timeouts.ReadTotalTimeoutConstant=50;
    timeouts.ReadTotalTimeoutMultiplier=10;
    //timeouts.WriteTotalTimeoutConstant=50;
    //timeouts.WriteTotalTimeoutMultiplier=10;
    if(!SetCommTimeouts(hSerial, &timeouts)){
    //error occureed. Inform user
    }
    char szBuff[1 + 1] = {0};
    DWORD dwBytesRead = 0;
    if(!ReadFile(hSerial, szBuff, n, &dwBytesRead, NULL)){
    //error occurred. Report to user.

    CloseHandle(hSerial);

  2. #2

    Re: how to read from serial port

    This can't be the code that you actually compiled? There is no file called <windows.h.> for example. (Note the second ".")
    The code you've pasted is at global scope, although you're writing the code as if it's within a function. Did you copy only some parts of the code, or is this what you wrote? If so, that won't work.
    Also, you should copy and paste the exact error you're getting, to make it possible for someone to help you.

  3. Re: how to read from serial port

    thanks for your reply jwatte, Actually i got this code from the net, please visit the link: gtk-study-korea.googlecode.com/files/serial-win.pdf
    'Windows Serial Port Programming, Robertson Bayer, March 30, 2008'.

    and these are the exact errors am getting:
    23 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected constructor, destructor, or type conversion before '.' token
    23 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected `,' or `;' before '.' token
    24 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected unqualified-id before "if"
    24 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected `,' or `;' before "if"
    27 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected constructor, destructor, or type conversion before '.' token
    27 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected `,' or `;' before '.' token
    28 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected constructor, destructor, or type conversion before '.' token
    28 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected `,' or `;' before '.' token
    29 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected constructor, destructor, or type conversion before '.' token
    29 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected `,' or `;' before '.' token
    30 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected constructor, destructor, or type conversion before '.' token
    30 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected `,' or `;' before '.' token
    31 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected unqualified-id before "if"
    31 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected `,' or `;' before "if"
    35 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected constructor, destructor, or type conversion before '.' token
    35 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected `,' or `;' before '.' token
    36 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected constructor, destructor, or type conversion before '.' token
    36 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected `,' or `;' before '.' token
    37 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected constructor, destructor, or type conversion before '.' token
    37 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected `,' or `;' before '.' token
    40 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected unqualified-id before "if"
    40 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected `,' or `;' before "if"
    45 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected unqualified-id before "if"
    45 C:\Users\DELL\Documents\DEV C and C++\Serialcomm1.cpp expected `,' or `;' before "if"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Question(s) where in the Arbotix AVR code is it talking to the XBEE serial port?
    By stephen.hazel in forum Arbotix, Microcontrollers, Arduino
    Replies: 5
    Last Post: 05-04-2012, 12:21 PM
  2. Question(s) Using a WRT54GL router with serial port
    By Sundsted in forum Software and Programming
    Replies: 0
    Last Post: 07-07-2010, 12:53 PM
  3. Serial Port in VBScript
    By Judge in forum Software and Programming
    Replies: 11
    Last Post: 01-04-2010, 08:12 AM
  4. Question(s) Cannot figure out how to write to the serial port in C++
    By Superlaxstar112 in forum Software and Programming
    Replies: 4
    Last Post: 07-23-2008, 05:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •