Window 95 serial communication function collection (only 32 digits)

zhaozj2021-02-11  205

Window 95 serial communication function collection (only 32 digits)

It should be noted: This is part of the program, so there are some parts that are independent of the specific application. The key is principle, not the program itself. Some use introductions later help understand this long program.

Header (.H) #include "StdAfx.h" #define GWL_PGPSINFO 0 #define GPSEXTRABYTES sizeof (LONG) #define MAXPORTS 4 #define CN_SEND WM_USER 100 #define RXQUEUE 4096 #define TXQUEUE 4096 // cursor states #define CS_HIDE 0x00 #define CS_SHOW 0x01 // Flow control flags #define FC_DTRDSR 0x01 #define FC_RTSCTS 0x02 #define FC_XONXOFF 0x04 // ascii definitions #define ASCII_BEL 0x07 #define ASCII_BS 0x08 #define ASCII_LF 0x0A #define ASCII_CR 0x0D #define ASCII_XON 0x11 #define ASCII_XOFF 0x13 / / data structures typedef struct tagGPSINFO {HANDLE idComDev; BYTE bPort; BOOL fConnected; BYTE bByteSize, bParity, bStopBits; DWORD dwBaudRate; HANDLE hPostEvent, hWatchThread, hWatchEvent; HWND hTermWnd; DWORD dwThreadID; OVERLAPPED osWrite, osRead;} ​​GPSINFO, * PGPSINFO; #define comdev (x) (x -> idcomdev) #define port (x) (x -> bPort) #define connection (x) (x -> fconnected) #define bytesize (x) (x -> bbytesize) #define Parity (x) (x -> bparity) #define stopbits (x -> bstopbits) #define baudrate (x) (x -> dwbaudrate) #define poste (x) (x -> hpostevent) #define hthread (x) (x -> hwatchthread) #define threadid (x) (x -> dwthreadid) #define Write_OS (x) (X) (X) -> OSWRITE #Define read_OS (x) (x-> osread) // function prototypes (private) LRESULT NEAR CREATEGPSINFO (hwnd, byte nport = 1); Bool Near DestroyGpsInfo (); int Near ReadcomMblock (lpstr, int); BOOL NEAR WriteCommBlock (LPSTR, DWORD); BOOL NEAR OpenConnection (); BOOL NEAR SetupConnection (); BOOL NEAR CloseConnection (); // function prototypes (public) DWORD FAR PASCAL CommWatchProc (LPSTR); see below embodied (for this Article, I have 2 times 2 times) CPP implementation part: #include "stdafx.h" #include "com.h" hwnd hgpsWnd = null; pgpsinfo npgpsinfo =

NULL; LRESULT NEAR CREATEGPSINFO (HWND HWND, BYTE NPORT) {if (null == (npgpsinfo = (pgpsinfo))) Return (LRESULT) -1); hgpsWnd = hwnd; comDev npGPSInfo) = 0; CONNECTED (npGPSInfo) = FALSE; PORT (npGPSInfo) = nPort; BAUDRATE (npGPSInfo) = CBR_9600; BYTESIZE (npGPSInfo) = 8; PARITY (npGPSInfo) = NOPARITY; STOPBITS (npGPSInfo) = ONESTOPBIT; WRITE_OS (npGPSInfo ) .Offset = 0; WRITE_OS (npGPSInfo) .OffsetHigh = 0; READ_OS (npGPSInfo) .Offset = 0; READ_OS (npGPSInfo) .OffsetHigh = 0; // create I / O event used for overlapped reads / writes READ_OS (npGPSInfo) . HEVENT = CreateEvent (NULL, TRUE, FALSE, NULL); if (read_OS (npgpsinfo) .hevent == null) {localfree (npgpsInfo); return (-1);} Write_OS (npgpsInfo) .hevent = CreateEvent (null, True, false, null; if (null == write_os (npgpsInfo) .hevent) {closeHandle (read_OS (npgpsinfo); localfree (npgpsInfo); Return (-1);} return ((LRESULT) TRUE); } Bool Near DestroyGpsInfo () {if (! Npgpsinfo) Return (false); if (connect (npgpsinfo) CloseConnection (); CloseHandle (Re AD_OS (npGPSInfo) .hEvent); CloseHandle (WRITE_OS (npGPSInfo) .hEvent); CloseHandle (POSTEVENT (npGPSInfo)); LocalFree (npGPSInfo); return (TRUE);} BOOL NEAR OpenConnection () {char szPort [15]; BOOL fRetVal; HCURSOR hOldCursor, hWaitCursor; HANDLE hCommWatchThread; DWORD dwThreadID; COMMTIMEOUTS CommTimeOuts; if return (FALSE) (npGPSInfo!); hWaitCursor = LoadCursor (NULL, IDC_WAIT); hOldCursor = SetCursor (hWaitCursor); wsprintf (szPort, "COM% d ", PORT (npGPSInfo)); if ((COMDEV (npGPSInfo) = CreateFile (szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL)) == (HANDLE) -1) return (FALSE);

else {SetCommMask (COMDEV (npGPSInfo), EV_RXCHAR); SetupComm (COMDEV (npGPSInfo), 4096,4096); PurgeComm (COMDEV (npGPSInfo), PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_R XCLEAR); CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF; CommTimeOuts.ReadTotalTimeoutMultiplier = 0; CommTimeOuts.ReadTotalTimeoutConstant = 1000; CommTimeOuts.WriteTotalTimeoutMultiplier = 0; CommTimeOuts.WriteTotalTimeoutConstant = 1000; SetCommTimeouts (COMDEV (npGPSInfo), & CommTimeOuts);} fRetVal = SetupConnection (); if (fRetVal) {CONNECTED (npGPSInfo) = TRUE; if (NULL == (hCommWatchThread = CreateThread ((LPSECURITY_ATTRIBUTES) NULL, 0, (LPTHREAD_START_ROUTINE) CommWatchProc, (LPVOID) NULL, 0, & dwThreadID))) {CONNECTED (npGPSInfo) = FALSE; CloseHandle (COMDEV (npGPSInfo)); fRetVal = FALSE;} else {THREADID (npGPSInfo) = dwThreadID; hTHREAD (npGPSInfo) = hCommWatchThread; EscapeCommFunction (COMDEV (npGPSInfo), SETDTR);}} else {CONNECTED (npGPSInfo) = FALSE; CloseHandle (COMDEV (npGPSInfo));} SetCursor; Return (Fretval);} Bool Near SetupConnecti ON () {bool FretVal; DCB DCB; if (! npgpsinfo) return (false); dcb.dcblength = sizeof (DCB); getcommstate (Comdev (npgpsInfo), & DCB); dcb.baudrate = baudrate (npgpsInfo); DCB. ByteSize = BYTESIZE (npGPSInfo); dcb.Parity = PARITY (npGPSInfo); dcb.StopBits = STOPBITS (npGPSInfo); dcb.fOutxDsrFlow = FALSE; dcb.fDtrControl = DTR_CONTROL_ENABLE; dcb.fOutxCtsFlow = FALSE; dcb.fRtsControl = RTS_CONTROL_ENABLE; dcb .finx = dcb.foutx = false; dcb.fbinary = true; dcb.fparity = true; fretval = setcommstate (Comdev (npgpsInfo), & DCB); return (fretval);} Bool Near CloseConnection () {if (! npgpsInfo) Return (false); connection (npgpsinfo) = false;

SetCommMask (COMDEV (npGPSInfo), 0); while (THREADID (npGPSInfo) = 0!); EscapeCommFunction (COMDEV (npGPSInfo), CLRDTR); PurgeComm (COMDEV (npGPSInfo), PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); CloseHandle (COMDEV (npGPSInfo)); return (TRUE);} int NEAR ReadCommBlock (LPSTR lpszBlock, int nMaxLength) {BOOL fReadStat; COMSTAT ComStat; DWORD dwErrorFlags; DWORD dwLength; DWORD dwError; if (npGPSInfo) return (FALSE);! ClearCommError (COMDEV (npGPSInfo), & dwErrorFlags, & ComStat); dwLength = min ((DWORD) nMaxLength, ComStat.cbInQue); if (dwLength> 0) {fReadStat = ReadFile (COMDEV (npGPSInfo), lpszBlock, dwLength, & dwLength, & READ_OS (npGPSInfo)) ; if (fReadStat!) {if (GetLastError () == ERROR_IO_PENDING) {OutputDebugString ( "/ n / rIO Pending"); while (! GetOverlappedResult (COMDEV (npGPSInfo), & READ_OS (npGPSInfo), & dwLe ngth, TR UE)) UE)) {dwError = GetLastError (); if (dwError == ERROR_IO_INCOMPLETE) continue;}} else {dwLength = 0; ClearCommError (COMDEV (npGPSInfo), & dwErrorFlags, & ComStat);}}} return (dwLength);} BOOL NEAR WriteCommBlock (LPSTR lpByte, DWORD dwBytesToWrite) {BOOL fWriteStat; DWORD dwBytesWritten; DWORD dwErrorFlags; DWORD dwError; COMSTAT ComStat; if return (FALSE) (npGPSInfo!); FWriteStat = WriteFile (COMDEV (npGPSInfo), lpByte, dwBytesToWrite, & dwBytesWritten , & WRITE_OS (npGPSInfo)); if (! fWriteStat) {if (GetLastError () == ERROR_IO_PENDING) {while (! GetOverlappedResult (COMDEV (npGPSInfo), & WRITE_OS (npGPSInfo), & dwBytesWritten, TRUE)) {dwError = GetLastError (); IF (dwerror == error_io_incumplete) Continue; Else {Clearcommeror (Comdev (NpgpsInfo), & dwerrorflags, & COMSTAT;

转载请注明原文地址:https://www.9cbs.com/read-4024.html

New Post(0)