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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I use the COM ports in Win32/Visual C++?

Serial Communication

How do I use the COM ports in Win32/Visual C++?

by  StukA  Posted    (Edited  )
Serial communication on Win32 platforms involves the following functions/structures:

COMMTIMEOUTS - for setting up your, you guessed it, time outs for reads and writes
DCB - structure containing all the communications options for your port
CreateFile() - creates a file for I/O
WriteFile() - writes data to the port
ReadFile() - reads data from the port
GetCommState() - retrieves information (using a DCB) about the port
SetCommState() - sets port options (again with a DCB)
GetCommTimeouts() - uses COMMTIMEOUTS to retrieve timeout information
SetCommTimeouts() - sets timeout options via a COMMTIMEOUTS structure

And now some examples of using these functions (this is from a class I wrote):

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);
}

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;
}

int SerialConnection::Recv(char* recv_block, int length)
{
DWORD bytes_read;
ReadFile(port, recv_block, length, &bytes_read, NULL);
return bytes_read;
}

void SerialConnection::SetBaudRate(BaudRate baud)
{
new_dcb->BaudRate = (baud ? CBR_9600 : CBR_4800);
SetCommState(port, new_dcb);
}

void SerialConnection::SetDataParityStop(DPS dps)
{
new_dcb->ByteSize = (dps ? 8 : 7);
new_dcb->fParity = (dps ? NOPARITY : EVENPARITY);
new_dcb->StopBits = (dps ? ONESTOPBIT : TWOSTOPBITS);
SetCommState(port, new_dcb);
}

SerialConnection::~SerialConnection()
{
SetCommState(port, old_dcb);
delete new_dcb;
delete old_dcb;
}

This should give you a good idea of how to proceed - the online help can fill in the details.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top