Hello,
I Could not Display data from Serial port. Please check my code and help me how to display data from serial port. The error is "error C3861: 'ProcessChar': identifier not found". I understood, it need to write some code for ProcessChar function to access data. could anybody help me in details.
#include <windows.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
DWORD dwError;
extern HWND hMainWnd;
int main(){
// Open the serial port.
HANDLE hPort = CreateFile (TEXT("COM1"), // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// 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
DCB PortDCB;
// Initialize the DCBlength member.
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
GetCommState (hPort, &PortDCB);
// Change the DCB structure settings.
PortDCB.BaudRate = 9600; // 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
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
// 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
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error
PortDCB.ByteSize = 8; // Number of bits/byte, 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 configure the serial port.
/*
dwError = GetLastError ();
MessageBox (hMainWnd, TEXT("Unable to configure the serial port"),
TEXT("Error"), MB_OK);
*/
cout<<"SetCommState failed" <<endl;
return FALSE;
}
// Retrieve the timeout parameters for all read and write operations
// on the port.
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts (hPort, &CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = 100;
CommTimeouts.ReadTotalTimeoutMultiplier = 100;
CommTimeouts.ReadTotalTimeoutConstant = 100;
CommTimeouts.WriteTotalTimeoutMultiplier = 100;
CommTimeouts.WriteTotalTimeoutConstant = 100;
// Set the timeout parameters for all read and write operations
// on the port.
if (!SetCommTimeouts (hPort, &CommTimeouts))
{
// Could not set the timeout parameters.
/* MessageBox (hMainWnd, TEXT("Unable to set the timeout parameters"),
TEXT("Error"), MB_OK);
dwError = GetLastError ();
*/
cout<<"SetCommTimeouts failed" <<endl;
return FALSE;
}
BYTE Byte;
DWORD dwBytesTransferred;
DWORD dwCommModemStatus;
// Specify a set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);
while (hPort != INVALID_HANDLE_VALUE)
{
// Wait for an event to occur for the port.
WaitCommEvent (hPort, &dwCommModemStatus, 0);
// Re-specify the set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RING);
if (dwCommModemStatus & EV_RXCHAR)
{
// Loop for waiting for the data.
do
{
// Read the data from the serial port.
ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0);
// Display the data read.
if (dwBytesTransferred == 1)
ProcessChar (Byte);
} while (dwBytesTransferred == 1);
}
}
CloseHandle(hPort);
//cout<<"Program Run Successfully!!!"<<endl;
}
Thanks in advance.