|
-
October 15th, 2002, 01:32 AM
#1
RS-232 Problem
Hey!
I have a little problem. I need to read and write to serial port but i'm doing something wrong in port initialization (setting up baudrate etc.)??? because after that SetCommState(...) function returns error. Can anyone help me?
Here's my code:
HANDLE hPort;
DCB dcbPort;
BOOL b_State;
DWORD dwError;
hPort = CreateFile("COM1",GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL ); //Opening serial port
b_State = GetCommState(hPort,&dcbPort);
if(!b_State)
{
dwError = GetLastError();
CloseHandle(hPort);
return 0;
}
else
{
dcbPort.DCBlength = sizeof(DCB);
dcbPort.BaudRate = CBR_9600; // Baudrate
dcbPort.ByteSize = 8; // Size of one byte data
dcbPort.Parity = NOPARITY; // Parity-control
dcbPort.StopBits = 1; // Stopbits
}
b_State = SetCommState(hPort,&dcbPort); //This returns false
if(!b_State)
{
dwError = GetLastError(); //Error number is 87
MessageBox(NULL,"Not working","!!!!",MB_OK);
CloseHandle(hPort);
return 0;
}
while(m_bListenThreadContinue)
{
}
CloseHandle(hPort);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|