[Wince] serial communication instance under Windows CE
Source: Internet
Author: User
Serial Communication under Windows CE instances is currently the most basic communication mode in the computer, communication and control fields. In the "embedded development/wince" community of csdn, some people often ask where to find serial communication examples. In fact, I have also asked this question myself. :) The general answer is to provide you with a Pocket PC 2002 SDK example program. But in the end, the SDK program and the structure of the MFC are very different. It is not very convenient for people who want to use the MFC to write communication programs. On the other hand, because Windows CE is a Unicode-based operating system, and Windows CE does not support the commonly used serial communication overlapped I/O mode in windows ), therefore, the serial communication classes under Windows CE are different from those on Windows. The following is an example of the MFC program I have adapted from the SDK program. I hope to have more exchanges with my friends who are dedicated to developing the SDK. Due to my lack of learning skills, there are many imperfections in the program. please correct me. My program is based on the assumption of "actively sending requests and passively receiving responses", so I only set a thread to receive data. Thanks to the "embedded development/wince" community for providing me with SDK examples, and to all the enthusiastic friends on csdn, I hope that China's software and hardware level will soon become world-class. The header file serial. h // serial. h: interface for the cserial class. //////////////////////////////////////// /// // If! Defined (success _) # define success _ # If _ msc_ver> 1000 # pragma once # endif // _ msc_ver> 1000 DWORD winapi readportthread (lpvoid ); // read data Thread class cserial {public: bool initcommtimeouts (); // set the timeout parameter bool initdcb (); // configure the serial port bool m_bconnected; bool closeport (handle hcommp ORT ); // disable the serial DWORD writep ORT (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_filestatic 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 when the port is opened, returns false if (hport = invalid_handle_value) {// The port cstring strerror; strerror cannot be opened. format (_ T ("unable to open % s, error no. = % d "), lpszportname, Getlasterror (); MessageBox (null, strerror, text ("error"), mb_ OK); Return false ;}// setcommmask (hport, ev_rxchar) of the event set monitored by the specified port ); // allocate the device buffer setupcomm (hport, 512,512); // initialize the information purgecomm (hport, purge_txclear | purge_rxclear) in the buffer; // 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 the DTr (data-terminal-ready) signal // setrts: send the request-to-send (RTS) 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 MessageBox (null, text ("unable to create the read thread"), text ("error"), mb_ OK) cannot be created; dwerr OR = 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) {// cannot write data 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! = Warn) {// 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 MessageBox (null, text ("error in read from serial port"), text ("read error"), mb_ OK) cannot be read from the serial port );} else {// assign the data value to the global variable strinchar = Buf;} Delete [] Buf ;}} getcommmodemstatus (hport, & dwcommmodemstatus);} return 0;} bool cserial :: closeport (handle hcommort) {If (hcommort! = Invalid_handle_value) {// set the connection property to false m_bconnected = false; // waitcommevent waits for setcommmask (hport, 0) in the queue; // The thread is blocked until the thread stops if (hreadthread) {terminatethread (hreadthread, 0); closehandle (hreadthread);} // clear the status escapecommfunction (hport, clrdtr) of the specified signal on the port; escapecommfunction (hport, clrrts ); // purgecomm (hport, purge_txclear | purge_rxclear) in the internal sending and receiving queues of the driver; // close the serial closehandle (hcommp ORT); hcommp ORT = invalid_handle _ Value; return true;} else {return true;} bool cserial: initdcb () {DCB portdcb; DWORD dwerror; portdcb. dcblength = sizeof (DCB); // get the default port setting information getcommstate (hport, & portdcb); // change the DCB structure setting portdcb. baudrate = 19200; // baud rate portdcb. fbinary = true; // Win32 does not support non-binary serial transmission mode. It 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; // enable DTR-line portdcb. fdsrsensiticb = false; // If set to true, any input bytes are ignored unless the DSR line is enabled // portdcb. ftxcontinueonxoff = true; // when it is true, if the receiving buffer is full and the driver has transferred xoff characters, the driver stops the transmission character 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 the 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 received Null Byte portdcb. frtscontrol = rts_control_enable; // enables the rts_control_enable. fabortonerror = false; // The default execution of the wince serial driver ignores this field portdcb. bytesize = 8; // The number of portdcb bits per byte. parity = noparity; // portdcb Without parity. stopbits = onestopbit; // One Stop bit per byte // configure the port if (! Setcommstate (hport, & portdcb) {// the serial port MessageBox (null, text ("unable to configure the serial port"), text ("error") cannot be configured "), 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 to set commtimeouts. readintervaltimeout = maxdword; commtimeouts. readtotal Timeoutmultiplier = 0; commtimeouts. readtotaltimeoutconstant = 0; commtimeouts. writetotaltimeoutmultiplier = 10; commtimeouts. writetotaltimeoutconstant = 1000; // set the port timeout value if (! Setcommtimeouts (hport, & commtimeouts) {// The timeout value MessageBox (null, text ("unable to set the time-out parameters"), text ("error "), mb_ OK); dwerror = getlasterror (); Return false;} return true;} The above class code runs on Embedded Visual C ++ 4.0 and Samsung S3C2410 Development Board Based on ARM9.. Net 4.1. Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 17885
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.