PDA

View Full Version : how to read from serial port



KAYSHEHU
08-08-2012, 07:32 AM
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);

jwatte
08-08-2012, 10:41 AM
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.

KAYSHEHU
08-09-2012, 05:03 AM
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"