Click here to Skip to main content
15,354,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I bought a book called " The Windows Serial Port Programming Handbook" by Ying Bai.

It seems to be a good book. Of course it was written around 2002. I don't know what compilers were avainalble at the time, but I am having trouble with some issues regarding that,

IN one project, I am to make an MFC Application. I figured out several little problems along the way, but I have one I cannot figure out.

I am supposed to use the "Add Member Function Wizard". The one descripbed in the book is much different from the one available in Visual Studio 2008.

The function I am supposed to add is a Type ERR_CODE. IN VS2008, it does not offer ERR_CODE as a return type to add as a function.

Anybody got any clues on how to proceed?


I am not sure how to check the return types of implemented functions, but maybe if I copied the function over to you, you could tell me. The MFC "add funtion wizard" does not allow you to make up your own typedef. There are only the usual ones (int, char, unsigned char, long, etc...)

Here is one of the functions-

ERR_CODE PortInitialize(LPTSTR lpszPortName, pSerialCreate pCreate)
DWORD dwError;
ERR_CODE ecStatus = OK;
unsigned char dBit;
// Open the serial port.
hPort = CreateFile(lpszPortName, // Pointer to the name of the port
// Access (read/write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute to copy
// If it fails to open the port, return error.
// Could not open the port.
dwError = GetLastError();
msg("Unable to open the port");
return EC_FOPEN;
PortDCB.DCBlength = sizeof(DCB);
// Get the default port setting information.
GetCommState(hPort, &PortDCB);
// Change the DCB structure settings.
PortDCB.BaudRate = pCreate->lBaudRate; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = TRUE; // Enable parity checking.
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
// DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement.
PortDCB.fNull = FALSE; // Disable null stripping.
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on error.
dBit = (unsigned char)pCreate->lDataBits;
PortDCB.ByteSize = dBit; // Number of bits/bytes, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB structure.
if (!SetCommState (hPort, &PortDCB))
// Could not create the read thread.
dwError = GetLastError();
msg("Unable to configure the serial port");
// Retrieve the time-out parameters for all read and write operations on the port.
GetCommTimeouts(hPort, &CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
// Set the time-out parameters for all read and write operations on the port.
if (!SetCommTimeouts (hPort, &CommTimeouts))
// Could not create the read thread.
dwError = GetLastError();
msg("Unable to set the time-out parameters");
EscapeCommFunction(hPort, SETDTR);
EscapeCommFunction(hPort, SETRTS);
pCreate->h_Port = hPort;
return ecStatus;

I am thinking you are right and it is probably an int that is returned. But will specifying an int type work as I add this funtion with the function wizard in VS2008?

Thank You,


Albert Holguin 24-Jun-11 18:30pm
you need to depend a lot less on wizards... this looks like some enumeration that's defined somewhere...

as for using the site, don't post updates as solutions, post updates as question improvements
Possibly ERR_CODE is a typedef introduced by the book author (you should check it). Tipically error codes have been historically represented using 32 bit integers, like HRESULT. You may infer the correct data type represented by ERR_CODE by inspecting the actual return values of the implemented functions.
Albert Holguin 22-Jun-11 9:22am
that's what comes to my mind, 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900