Implementation of Windows CE Serial Communication

Source: Internet
Author: User

Serial Communication is currently the most basic communication mode in the computer, communication and control fields. But what kind of serial communication is used? Generally, we provide you with an SDK example program for the Pocket PC 2002. 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 the MFC class I have adapted from the SDK program. I hope to have a lot of exchanges with my friends who are dedicated to developing the SDK program. 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. I would be very grateful if a friend can provide classes that independently send data and receive data threads.

Header file serial. h

// 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> 1000

DWORD 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;
}

The above code was tested and passed on Embedded Visual C ++ 4.0 and Samsung S3C2410 Development Board (running Windows CE. Net 4.1) based on ARM9.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.