VC Write a program for serial communication in Windows

xiaoxiao2021-03-06  98

Since there are so many people asking this style, Babe gives a Visual C 4.2

Window 95 serial communication function collection (only 32 digits)

It is to be explained: This is part of my program, so there are some parts that have nothing to do with specific applications.

But I think the key is principle, not the program itself. Some use introductions later help understand this long program.

Head file (.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 State

#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 connect (x) (x -> fconnected)

#define bytesize (x) (x -> bbytesize)

#define parity (x) (x -> bparity)

#define stopbits (x) (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 -> OSWRITE)

#define 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);

Please see the following (for this article, I have 2 times in this article)

CPP implementation section:

#include "stdafx.h"

#include "com.h"

HWND HGPSWND = NULL;

Pgpsinfo npgpsinfo = null;

Lresult Near CreategpsInfo (HWND HWND, BYTE NPORT)

{

IF (NULL == (npgpsinfo = (pgpsinfo) Localalloc (LPTR, SIZEOF (GPSInfo)))))))))

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) .hevent);

Localfree (npgpsinfo);

Return (-1);

}

Return (LRESULT) TRUE;

}

Bool Near DestroyGpsInfo ()

{

IF (! npgpsinfo) return (false);

IF (Connected (npgpsinfo)) CloseConnection ();

CloseHandle (Read_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 (! npgpsinfo) return (false);

HWAITCURSOR = loadingcursor (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 = 0xffffffffff;

CommTIMEOUTS.ReadtotalTimeoutMultiplier = 0;

CommTIMEOUTS.ReadtotalTimeoutConstant = 1000;

CommTIMEOUTS.WRITETOTIMEOUTMULTIPLIER = 0;

CommTIMEOUTS.WRITETOTALTIMEOUTCONSTANT = 1000;

Setcommtimeouts (Commtimeout); & 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 (HOLDCURSOR);

Return (FRETVAL);

}

Bool Near SetupConnection ()

{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);

Connected (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);

Clearcommorror (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 ("Io Pending");

While (! GetoverlappedResult (Comdev (NPGPSInfo), & Read_OS (NPGPSINFO), & DWLENGTH, TR

UE))

{dwerror = getLastError ();

IF (dwerror == error_io_incumplete)

CONTINUE;

}

}

Else

{dwlength = 0;

Clearcommorror (Comdev (NpgpsInfo), & dwerrorflags, & COMSTAT

}

}

}

Return (DWLENGTH);

}

Bool Near WriteCommblock (LPSTR LPBYTE, DWORD DWBYTESTOWRITE)

{BOOL FWRITESTAT;

DWORD DWBYTESWRITTEN

DWORD DWERRORFLAGS;

DWORD DWERROR;

COMSTAT COMSTAT;

IF (! npgpsinfo) return (false);

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

{

Clearcommorror (Comdev (NpgpsInfo), & dwerrorflags, & COMSTAT

Break;

}

}

}

Else

{

Clearcommorror (Comdev (NpgpsInfo), & dwerrorflags, & COMSTAT

Return (False);

}

}

Return (TRUE);

}

DWORD FAR PASCAL COMMWATCHPROC (LPSTR)

{DWORD DWEVTMASK;

Overlapped OS;

int NLENGTH;

Byte Abin [1024];

MEMSET (& OS, 0, SIZEOF (OVERLAPPED);

// Create I / O Event Used for overlapped read

Os.hevent = CreateEvent (NULL, TRUE, FALSE, NULL);

IF (os.hevent == null)

{MessageBox (Null, "Failed to Create Event For Thread!", "GPS

Error! ", MB_ICONEXCLAMATION | MB_OK);

Return (False);

}

IF (! setcommmask (comdev (npgpsInfo), ev_rxchar)) Return (false);

While (connection (npgpsinfo))

{dwevtmask = 0;

Waitcommevent (Comdev (NpgpsInfo), & DWEVTMASK, NULL

IF (DWEVTMASK & EV_RXCHAR) == EV_RXCHAR)

{DO

{IF

(NLENGTH = ReadcomMBlock ((LPSTR) Abin, 1024))

{

// WriteCommblock ((LPSTR) Abin, NLENGTH;

* (Abin NLENGTH) = 0;

:: SendMessage (HgpsWnd, CN_SEND, NLENGTH, (LONG) (LPSTR) ABIN;

}

}

While ((Nlength> 0) && (connection (npgpsinfo));

}

}

CloseHandle (Os.hevent);

ThreadId (npgpsinfo) = 0;

HThread (npgpsinfo) = null;

Return (TRUE);

}

Just here, I hope to help friends asking these questions!

The order in general use is:

CreategpsInfo (notified window handle, serial port number 1 or 2);

OpenConnection (); // Establish a connection it will call setupConnection

DestroyGpsInfo (); // Release connection it will call CloseConnection

You can read / write serial ports with ReadcomMBlock / WriteCommblock

CommWatchProc is a thread that monitors the serial port. It is created by OpenConnection. When the serial port has data, it will notify the 'The message transmitted to the window data of the window handle' (custom)

SendMessage (HgpsWnd, CN_SEND, NLENGTH, (LONG) (LPSTR) ABIN;

Ok, the article is over! I hope to help you!

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

New Post(0)