📄 ceserial.cpp
字号:
#include "stdafx.h"
#include "ceserial.h"
CCESerial::CCESerial()
{
hPort=NULL;
}
CCESerial::~CCESerial()
{
Close();
}
UINT SerialReadThread (LPVOID lpvoid)
{
struct ReadThreadParam {
HANDLE hdl;
TCHAR *buf;
} *lparam;
HANDLE hPort;
TCHAR *buffer;
BYTE Byte;
DWORD dwCommModemStatus,
dwBytesTransferred;
lparam=( struct ReadThreadParam *)lpvoid;
hPort=lparam->hdl;
buffer=lparam->buf;
// 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);
}
// Retrieve modem control-register values.
GetCommModemStatus (hPort, &dwCommModemStatus);
}
AfxEndThread(0);
return 0;
}
// Initialize Serial with PortName and Baud rate
BOOL CCESerial::Open(LPTSTR lpszPortName,int nBaud)
{
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
// Open the serial port.
hPort = CreateFile (lpszPortName, // 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
// If it fails to open the port, return FALSE.
if ( hPort == INVALID_HANDLE_VALUE ) return FALSE;
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
GetCommState (hPort, &PortDCB);
// Change the DCB structure settings.
PortDCB.BaudRate = nBaud; // 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)) return FALSE;
// 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)) return FALSE;
// Direct the port to perform extended functions SETDTR and SETRTS
// SETDTR: Sends the DTR (data-terminal-ready) signal.
// SETRTS: Sends the RTS (request-to-send) signal.
EscapeCommFunction (hPort, SETDTR);
EscapeCommFunction (hPort, SETRTS);
// Create a read thread for reading data from the communication port.
struct {
HANDLE hdl;
TCHAR *buf;
} lparam;
lparam.hdl=hPort;
lparam.buf=lBuffer;
AfxBeginThread(SerialReadThread,&lparam);
return TRUE;
}
// Close serial port
BOOL CCESerial::Close ()
{
if (hPort != INVALID_HANDLE_VALUE)
{
// Close the communication port.
if (!CloseHandle (hPort))
return FALSE;
else
{
hPort = INVALID_HANDLE_VALUE;
return TRUE;
}
}
return FALSE;
}
// Write bytes to serial port
BOOL CCESerial::WriteByte(BYTE Byte)
{
DWORD dwNumBytesWritten;
if (!WriteFile (hPort, // Port handle
&Byte, // Pointer to the data to write
1, // Number of bytes to write
&dwNumBytesWritten, // Pointer to the number of bytes
// written
NULL)) // Must be NULL for Windows CE
{
// WriteFile failed.
return FALSE;
}
return TRUE;
}
// Write bytes to serial port
int CCESerial::WriteData(TCHAR *ptchar,int count)
{
int i,nWriteCount;
BYTE byte;
nWriteCount=0;
for(i=0;i<count;i++){
// Unicode to ASCII
byte=(BYTE)( ptchar[i] & 0x00ff );
if(WriteByte(byte)) nWriteCount++;
else break;
}
return (nWriteCount);
}
int CCESerial::ReadWaiting();
// Read bytes from serial port
int CCESerial::ReadData(TCHAR *buffer,int limit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -