Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WriteFile Function for I/O 1

Status
Not open for further replies.

Denyson

Programmer
Mar 20, 2001
19
BR
Hi,

I´m trying to use the writefile function to send and receive some datas by serial port (COM 1).

Could anybody tell me how to use correctly the Writefile function for I/O serial communications and also give me an example about how to declare their parameters and how to use the function.

Thanks
Denyson
 
Hi I´m looking for a simple programm to read and write on the serial port under WinNT 4 SP 5
with Microsoft Visual C++ Standart Edition.
Can anybody send me a simple examble programm??

Thank you very much

Jan.Wirl@acts.de
 
The usual trick to using WriteFile with a com port is the call to CreateFile. The trick is you have to use "COM1:"
as the filename (or COM2, etc). I'm not 100% sure about the colon, though. Try it with and without.

Afterwards, you can read and write it just like any other file.

Once you've got basic read/write functionality, you might want to investigate I/O Completion Ports. These will help you handle the case where you try and read from the port when the other end hasn't sent anything (your program blocks).

Chip H.
 
Here's some example code for initializing a serial port. I used this class for simple (no flow control or handshaking) serial communications.

SerialConnection::SerialConnection(string port_name, BaudRate baud, Parity parity, DataBits data_bits, StopBits stop_bits)
{
old_dcb = new DCB; //Pointers for Get/SetCommState
new_dcb = new DCB;
COMMTIMEOUTS timeout;

if (port_name != "COM1" && port_name != "COM2" && port_name != "COM3" && port_name != "COM4" )
throw WBException(WBException::INV_PORT_NAME);

port = CreateFile( port_name.data(),
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
if (port == INVALID_HANDLE_VALUE)
throw WBException(WBException::SER_PORT_ERR);

GetCommState(port, old_dcb);

*new_dcb = *old_dcb;

new_dcb->BaudRate = (baud ? CBR_9600 : CBR_4800);
new_dcb->ByteSize = (data_bits ? 8 : 7);
new_dcb->fParity = (parity ? EVENPARITY : NOPARITY);
new_dcb->StopBits = (stop_bits ? TWOSTOPBITS : ONESTOPBIT);
new_dcb->fOutxDsrFlow = 0;
new_dcb->fOutxCtsFlow = 0;
new_dcb->fDsrSensitivity = 0;

SetCommState(port, new_dcb);

timeout.ReadIntervalTimeout = 500;
timeout.ReadTotalTimeoutMultiplier = 100;
timeout.ReadTotalTimeoutConstant = 100;

SetCommTimeouts(port, &timeout);
}

And this is the sending function:

int SerialConnection::Send(const char* send_block, int length)
{
DWORD bytes_written;
for (int i = 0; i < length; ++i)
{
WriteFile(port, send_block, 1, &bytes_written, NULL);
send_block++;
if (bytes_written < 1)
throw WBException(WBException::SER_PORT_ERR);
Sleep(100);
}
return bytes_written;
}
 
StukA -

Pretty good looking code!

Can you post it as a FAQ for others to view?

Thanks.
Chip H.
 
Sure - I'll be more than happy to do that. It was no fun figuring this stuff out!
 
StukA, so far I see this question is frequently asqued. I think is a good idea you to write a FAQ. John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top