PsychoMantis
asked on
Setting up, sending and recieving serial port
I am using DEV-C++ 4 complier. Tried using inport() and outport() for communicating the serial port, but it return compling error. Later found out that WIN32 API is needed. Can someone tell me how to implement WIN32 API into C++ programming. If possiable provide some examples for setting up the serial port communications...
Thanks
Thanks
ASKER
Could you also show me what i need, for example include header for the win32 API... Thanks.. Cause i am confused on Win32 API. Win32API is just another porgramming lanaguae?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp ("Serial Communications in Win32") - this is a comprehensive tutorial & comes with sample code...
Since you have C++ compiler try this one:
http://www.naughter.com/serialport.html
should work fine for u...
Hope it helps...
http://www.naughter.com/serialport.html
should work fine for u...
Hope it helps...
ASKER
Thanks jlsjls the points is yours!
Finally my confuse ends...after days of search :)
Thanks again guys!
Finally my confuse ends...after days of search :)
Thanks again guys!
Here you'll find the different steps in using a serial port (f.e. COM1).
1. Create handle to serial port and set communication params :
HANDLE hPort;
DCB dcb;
hPort= ::CreateFile( Device, GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL |
0,
NULL );
if (hPort== INVALID_HANDLE_VALUE)
{
DWORD dwLastError;
dwLastError = ::GetLastError();
return FALSE;
}
::GetCommState(hBanksys,&d
dcb.BaudRate = CBR_9600;
dcb.fParity =TRUE;
dcb.ByteSize = 8;
dcb.Parity = EVENPARITY;
dcb.StopBits = TWOSTOPBITS;
::BuildCommDCB(Port+":"+Po
::SetCommState(hPort,&dcb)
2. Use the serial port (read and write) :µ
READ :
COMMTIMEOUTS CommTimeout;
BOOL bResult;
DWORD dwBytesRead;
CommTimeout.ReadIntervalTi
CommTimeout.ReadTotalTimeo
CommTimeout.ReadTotalTimeo
::SetCommTimeouts(hPort,&C
bResult = ::ReadFile(hPort,(void *lpszText,lCount,&dwBytesR
if (bResult == FALSE)
{
return bResult;
}
WRITE :
COMMTIMEOUTS CommTimeout;
char szBuf[100];
DWORD dwLastError;
CommTimeout.WriteTotalTime
CommTimeout.WriteTotalTime
::SetCommTimeouts(hPort,&C
bResult = ::WriteFile(hPort,lpszOutS
if (bResult == FALSE)
{
dwLastError = ::GetLastError();
return FALSE;
}
3. Stop using the serial port :
::CloseHandle(hPort);
Hope this helps.
jlsjls