// Serial. h: interface for the cserial class. // //////////////////////////////////////// ////////////////////////////// # If! Defined (afx_serial_hsf-59575586_aaa9_4fef_b2a7_e089554178ef1_included _) # Define afx_serial_hsf-59575586_aaa9_4fef_b2a7_e089554178ef1_encoded _ # If _ msc_ver> 1000 # Pragma once # Endif // _ msc_ver> 1000DWORD winapi readportthread (lpvoid); // read data thread Class cserial { Public: Bool initcommtimeouts (); // you can specify the timeout parameter. Bool initdcb (); // configure the serial port Bool m_bconnected; Bool closeport (handle hcommp ORT); // close the serial port DWORD writeport (tchar * Buf, DWORD dwbytestowrite); // write data Bool openport (lptstr lpszportname); // open the serial port Cserial (); Handle hreadthread; Virtual ~ Cserial (); }; # Endif //! Defined (afx_serial_hsf-59575586_aaa9_4fef_b2a7_e089554178ef1_included _) Source File: Serial. cpp // Serial. cpp: Implementation of the cserial class. // //////////////////////////////////////// ////////////////////////////// # Include "stdafx. H" # Include "Serial. H" # Ifdef _ debug # UNDEF this_file Static char this_file [] =__ file __; # Define new debug_new # Endif Handle hport; Cstring strinchar; //////////////////////////////////////// ////////////////////////////// // Construction/destruction //////////////////////////////////////// ////////////////////////////// Cserial: cserial () { } Cserial ::~ Cserial () { If (hport! = Invalid_handle_value) Closeport (hport ); } Bool cserial: openport (lptstr lpszportname) { DWORD dwerror, Dwthreadid; If (hport) { Return false; } // Open the serial port Hport = createfile (lpszportname, generic_read | generic_write, 0, null, open_existing, 0, null ); // If an error occurs while enabling the port, false is returned. If (hport = invalid_handle_value) { // The port cannot be opened Cstring strerror; Strerror. Format (_ T ("unable to open % s, error no. = % d "), Lpszportname, getlasterror ()); MessageBox (null, strerror, text ("error"), mb_ OK ); Return false; } // Event set monitored by the specified port Setcommmask (hport, ev_rxchar ); // Allocate the device Buffer Setupcomm (hport 512,512 ); // Initialize the information in the buffer. Purgecomm (hport, purge_txclear | purge_rxclear ); // Configure the serial port If (! Initdcb ()) Return false; // Set the port timeout value If (! Initcommtimeouts ()) Return false; // Set the status of the specified signal on the port // Setdtr: Send DTR (data-terminal-ready) signals // Setrts: send the request-to-Send signal. Escapecommfunction (hport, setdtr ); Escapecommfunction (hport, setrts ); // Create a thread for reading data from the serial port If (hreadthread = createthread (null, 0, readportthread, 0, 0, & dwthreadid )) { } Else { // The thread cannot be created. MessageBox (null, text ("unable to create the read thread "), Text ("error"), mb_ OK ); Dwerror = getlasterror (); Return false; } M_bconnected = true; Return true; } DWORD cserial: writeport (tchar * Buf, DWORD dwchartowrite) { Bool fwritestate; DWORD dwbyteswritten; // Write data Fwritestate = writefile (hport, Buf, dwchartowrite * sizeof (tchar), & dwbyteswritten, null ); If (! Fwritestate) { // Data cannot be written. MessageBox (null, text ("can't write string to comm"), text ("error"), mb_ OK ); Dwbyteswritten = 0; } Return dwbyteswritten; } DWORD winapi readportthread (lpvoid) { Bool freadstate; DWORD dwcommmodemstatus; DWORD dwlength; COMSTAT; DWORD dwerrorflags; While (hport! = Invalid_handle_value) { // Wait for the serial port event to occur Waitcommevent (hport, & dwcommmodemstatus, 0 ); If (dwcommmodemstatus & ev_rxchar) { Clearcommerror (hport, & dwerrorflags, & COMSTAT ); // Cbinque returns the number of characters in the serial driver input queue Dwlength = COMSTAT. cbinque; If (dwlength> 0) { // Read data from the serial port Tchar * Buf = new tchar [256]; Freadstate = readfile (hport, Buf, dwlength, & dwlength, null ); If (! Freadstate) { // Data cannot be read from the serial port MessageBox (null, text ("error in read from serial port"), text ("read error"), mb_ OK ); } Else { // Assign a value to the global variable Strinchar = Buf; } Delete [] Buf; } } Getcommmodemstatus (hport, & dwcommmodemstatus ); } Return 0; } Bool cserial: closeport (handle hcommp ORT) { If (hcommp ORT! = Invalid_handle_value) { // Set the connection property to false M_bconnected = false; // Waitcommevent waiting in the queue Setcommmask (hport, 0 ); // The thread stops. If (hreadthread) { Terminatethread (hreadthread, 0 ); Closehandle (hreadthread ); } // Clear the status of the specified signal on the port Escapecommfunction (hport, clrdtr ); Escapecommfunction (hport, clrrts ); // Clear the internal sending and receiving queues of the driver Purgecomm (hport, purge_txclear | purge_rxclear ); // Close the serial port Closehandle (hcommp ORT ); Hcommp ORT = invalid_handle_value; Return true; } Else { Return true; } } Bool cserial: initdcb () { DCB portdcb; DWORD dwerror; Portdcb. dcblength = sizeof (DCB ); // Obtain the default port settings. Getcommstate (hport, & portdcb ); // Change the DCB structure settings Portdcb. baudrate = 19200; // baud rate Portdcb. fbinary = true; // Win32 does not support non-binary serial transmission mode, which must be true Portdcb. fparity = true; // enable parity Portdcb. foutxctsflow = true; // the output of the serial port is controlled by the CTS line. Portdcb. foutxdsrflow = false; // disable the DSR Flow Control of the serial port. Portdcb. fdtrcontrol = dtr_control_enable; // enables the DTr line. Portdcb. fdsrsensitivity = false; // If set to true, any input bytes are ignored unless the DSR line is enabled. // Portdcb. ftxcontinueonxoff = true; // if the value is true, if the receiving buffer is full and the driver has transferred xoff characters, the driver stops transmitting characters. Portdcb. ftxcontinueonxoff = false; Portdcb. foutx = false; // set to true specify that the Xon/xoff control is used to control the serial output. Portdcb. finx = false; // set to true specify that the Xon/xoff control is used to control serial input. Portdcb. ferrorchar = false; // The default execution of the wince serial driver ignores this field. Portdcb. fnull = false; // if it is set to true, the serial driver ignores the NULL bytes received. Portdcb. frtscontrol = rts_control_enable; // enables the RTS line. Portdcb. fabortonerror = false; // The default execution of the wince serial driver ignores this field. Portdcb. bytesize = 8; // The number of digits per byte Portdcb. Parity = noparity; // No parity Portdcb. stopbits = onestopbit; // One Stop bit per byte // Configure the port according to the DCB Structure If (! Setcommstate (hport, & portdcb )) { // The serial port cannot be configured. MessageBox (null, text ("unable to configure the serial port"), text ("error"), mb_ OK ); Dwerror = getlasterror (); Return false; } Return true; } Bool cserial: initcommtimeouts () { Commtimeouts; DWORD dwerror; // Get the timeout Parameter 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 port timeout value If (! Setcommtimeouts (hport, & commtimeouts )) { // The timeout value cannot be set. MessageBox (null, text ("unable to set the time-out parameters"), text ("error"), mb_ OK ); Dwerror = getlasterror (); Return false; } Return true; } |