Link to home
Start Free TrialLog in
Avatar of PsychoMantis
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
Avatar of jlsjls
jlsjls

In order to use the serial port in Win32, you need indeed the Win32-API.
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,&dcb);
dcb.BaudRate = CBR_9600;
dcb.fParity =TRUE;
dcb.ByteSize = 8;
dcb.Parity = EVENPARITY;
dcb.StopBits = TWOSTOPBITS;
::BuildCommDCB(Port+":"+PortSettings,&dcb);
::SetCommState(hPort,&dcb);

2. Use the serial port (read and write) :µ
READ :
COMMTIMEOUTS CommTimeout;
BOOL bResult;
DWORD dwBytesRead;

CommTimeout.ReadIntervalTimeout = 100;
CommTimeout.ReadTotalTimeoutMultiplier =1;
CommTimeout.ReadTotalTimeoutConstant = lTimeout;
::SetCommTimeouts(hPort,&CommTimeout);
     
bResult = ::ReadFile(hPort,(void *lpszText,lCount,&dwBytesRead,NULL);
if (bResult == FALSE)
{
return bResult;
}

WRITE :
COMMTIMEOUTS CommTimeout;
char szBuf[100];
DWORD dwLastError;

CommTimeout.WriteTotalTimeoutConstant = 2000;     // 2 seconds
CommTimeout.WriteTotalTimeoutMultiplier = 0;
::SetCommTimeouts(hPort,&CommTimeout);

bResult = ::WriteFile(hPort,lpszOutString,lLength,&dwBytesWritten,NULL);
if (bResult == FALSE)
{
   dwLastError = ::GetLastError();
   return FALSE;
}

3. Stop using the serial port :
::CloseHandle(hPort);


Hope this helps.
jlsjls
Avatar of PsychoMantis

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
Avatar of jlsjls
jlsjls

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkr
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...
Thanks  jlsjls the points is yours!
Finally my confuse ends...after days of search :)
Thanks again guys!