I'm trying to get access to the serial port on on Windows XP, using VC
2008.
Here is the code I am using:
int CCommunicationSerial::Init(
char const * const comPort, // in
const unsigned int baudRate, // in
const unsigned char bitsPerByte, // in
const eCommunicationSerialParity_t parity, // in
const float stopBits // in
)
{
// Function return value;
int result = -1;
// Communication port properties
DCB commProperties;
TCHAR szDevName[32];
// TODO: THIS IS TO BE FIXED
wsprintf(szDevName, TEXT("COM1"));
// Open the port in non overlapped mode
m_Handle = CreateFile( szDevName,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0, // non overlap
0);
if(INVALID_HANDLE_VALUE != m_Handle)
{
memset(&commProperties, 0, sizeof(commProperties));
commProperties.DCBlength = sizeof(DCB);
if(TRUE == GetCommState(m_Handle, &commProperties))
{
result = 0;
}
// Set the serial communication properties
// Initialize the DCB
// TODO: For now all of these contain hardcoded values.
// Some of these values are rather best-guessed and may not be
appropriate.
// Though it is important to specify default values that are
applied for
// each initialization so we insure the state of the harware
when the app
// starts.
commProperties.BaudRate = baudRate;
commProperties.fBinary = true; // Always with
Windows
switch(parity)
{
case COMMUNICATIONSERIALPARITY_NONE:
{
commProperties.fParity = false;
break;
}
default:
{}
} // switch(parity)
commProperties.fOutxCtsFlow = false;
commProperties.fOutxDsrFlow = false;
commProperties.fDtrControl = DTR_CONTROL_DISABLE;
commProperties.fDsrSensitivity = false;
commProperties.fTXContinueOnXoff = true;
commProperties.fOutX = false;
commProperties.fInX = false;
commProperties.fErrorChar = false;
commProperties.fNull = false;
commProperties.fRtsControl = RTS_CONTROL_DISABLE;
commProperties.fAbortOnError = true;
commProperties.fDummy2 = 0;
commProperties.wReserved = 0;
commProperties.XonLim = 2048;
commProperties.XoffLim = 512;
commProperties.ByteSize = 8;
commProperties.BaudRate = CBR_19200; // set the baud rate
commProperties.ByteSize = 8; // data size, xmit, and
rcv
commProperties.Parity = NOPARITY; // no parity bit
commProperties.StopBits = ONESTOPBIT; // one stop bit
switch(parity)
{
case COMMUNICATIONSERIALPARITY_NONE:
{
commProperties.Parity = NOPARITY;
break;
}
default:
{}
} // switch(parity)
if(1.0 == stopBits)
{
commProperties.StopBits = ONESTOPBIT;
}
else if (1.5 == stopBits)
{
commProperties.StopBits = ONE5STOPBITS;
}
else if (2 == stopBits)
{
commProperties.StopBits = TWOSTOPBITS;
}
else
{}
commProperties.XonChar = 17;
commProperties.XoffChar = 19;
commProperties.ErrorChar = 0;
commProperties.EofChar = 0;
commProperties.EvtChar = 0;
commProperties.wReserved1 = 0;
if(0 != SetCommState(m_Handle, &commProperties))
{
result = 0;
}
} // if(INVALID_HANDLE_VALUE != m_Handle)
return result;
} // CCommunicationSerial::Init
I want to configure the port as follow: 19200 bauds, no parity, no
hardware control.
If I run the code as is, my application is unable to read from the
port.
If I run Hyperterminal (19200 bauds, no parity, no hardware control)
and then execute this code, I can read from the serial port.
I believe I am covering all the settings that can be modified on the
serial port, though it looks like Hyperterminal does something else
that enables access to the serial port...
Any idea!?
Thanks
Also how are you reading the serial port in your code ?
Whats your setup ? Are you running a null modem cable from a pc
to pc with one end running hyperterminal hooked up ?
Hyperterminal maybe asserting the DTR signal to your port allowing
data to come in so your target that replaces Hypertermnal may have to do the
same.
Get a break out box so you can monitor the pin outs
-M