Win32 serial communication
Archiver
Posts: 46,084
Anyone out there have any experience with programming a Windows Win32
program and using the serial ports? I want my Windows program to send
bytes to the BS2 over the same line you program it with.
I'm still struggling. I suspect my DCB structure has not been set up
correctly. My port setup looks like this:
// Now the serial port
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened
w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use
OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
wsprintf(temp,"CreateFile Error %d",GetLastError());
MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);
}
// We will build on the current configuration, and skip setting the
size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
wsprintf(temp,"GetCommState Error %d",GetLastError());
MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);
}
// Fill in the DCB: baud=9,600 bps, 8 data bits, no parity, and 1 stop
bit.
dcb.BaudRate = CBR_9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
wsprintf(temp,"SetCommState Error %d",GetLastError());
MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);
}
When I want to push out a single byte to the BS2 I use
WriteFile(hCom,&lights[noparse][[/noparse]0],1,&byteswritten,NULL);
where the lights array contains a single byte.
I question the dcb structure setup, especially is the control set
correctly with DTR_CONTROL_HANDSHAKE? Also, is no overlapped I/O
correct?
Lastly, the stamp must be grabbing certain bytes. When you set the I/O
line to 16, it uses the serial port connected to the PC (which is what I
want). However, I suspect there are certain bytes which flag the stamp
to take the stream and load a program into stamp memory (a 0xFF
perhaps?).
Any help appreciated. I've just found a document on the MSDN site on
serial that reads like a PhD thesis, and I'm trying to avoid this. I
just need to pump a single byte to the stamp from time to time. I've
found a nice example in qBasic, but nothing in Win32.
Regards, Theron Wierenga
program and using the serial ports? I want my Windows program to send
bytes to the BS2 over the same line you program it with.
I'm still struggling. I suspect my DCB structure has not been set up
correctly. My port setup looks like this:
// Now the serial port
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened
w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use
OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
wsprintf(temp,"CreateFile Error %d",GetLastError());
MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);
}
// We will build on the current configuration, and skip setting the
size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
wsprintf(temp,"GetCommState Error %d",GetLastError());
MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);
}
// Fill in the DCB: baud=9,600 bps, 8 data bits, no parity, and 1 stop
bit.
dcb.BaudRate = CBR_9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
wsprintf(temp,"SetCommState Error %d",GetLastError());
MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);
}
When I want to push out a single byte to the BS2 I use
WriteFile(hCom,&lights[noparse][[/noparse]0],1,&byteswritten,NULL);
where the lights array contains a single byte.
I question the dcb structure setup, especially is the control set
correctly with DTR_CONTROL_HANDSHAKE? Also, is no overlapped I/O
correct?
Lastly, the stamp must be grabbing certain bytes. When you set the I/O
line to 16, it uses the serial port connected to the PC (which is what I
want). However, I suspect there are certain bytes which flag the stamp
to take the stream and load a program into stamp memory (a 0xFF
perhaps?).
Any help appreciated. I've just found a document on the MSDN site on
serial that reads like a PhD thesis, and I'm trying to avoid this. I
just need to pump a single byte to the stamp from time to time. I've
found a nice example in qBasic, but nothing in Win32.
Regards, Theron Wierenga