Serial Communication under. NET Compact Framework

Source: Internet
Author: User

In Windows Mobile and Wince, many devices provide access interfaces through Serial ports (Serial Port/Com Port). For example, you can access the GPS receiver through Serial ports to receive NMEA Data.

In CF. starting from NET2.0, MS encapsulates serial port operations into System. IO. ports. the SerialPort greatly simplifies the operations on the serial port. You can directly operate the serial port without P/Invoke.

The following two Serial Communication classes are displayed: one is responsible for communication, the other is responsible for receiving, and the other two are running on devices that do not need them.

Public class ReceiverPort: IDisposable
{
Private readonly System. IO. Ports. SerialPort serialPort;

Public ReceiverPort ()
{
SerialPort = new System. IO. Ports. SerialPort ("COM1", 4800,

System. IO. Ports. Parity. None, 8,
System. IO. Ports. StopBits. One );
SerialPort. Handshake = System. IO. Ports. Handshake. None;

Try
{
SerialPort. DataReceived + = new

System. IO. Ports. SerialDataReceivedEventHandler (this. serialPort_DataReceived );
SerialPort. Open ();
If (serialPort. IsOpen)
{
Console. WriteLine ("Open the serial port successful ");
}
Else
{
Console. WriteLine ("Fail to open the serial port ");
}
}
Catch (Exception e)
{
Console. WriteLine (e. Message );
}
}

Public void Dispose ()
{
If (serialPort. IsOpen)
{
SerialPort. Close ();
}
SerialPort. Dispose ();
}

Private void serialPort_DataReceived (object sender,

System. IO. Ports. SerialDataReceivedEventArgs e)
{
Console. WriteLine ("RECEIVED:" + serialPort. ReadLine ());
}
}

Public class SenderPort: IDisposable
{
Private readonly System. IO. Ports. SerialPort serialPort;

Public SenderPort ()
{
SerialPort = new System. IO. Ports. SerialPort ("COM1", 4800,

System. IO. Ports. Parity. None, 8,
System. IO. Ports. StopBits. One );
SerialPort. Handshake = System. IO. Ports. Handshake. None;

Try
{
SerialPort. Open ();
If (serialPort. IsOpen)
{
Console. WriteLine ("Open the serial port successful ");
}
Else
{
Console. WriteLine ("Fail to open the serial port ");
}
}
Catch (Exception e)
{
Console. WriteLine (e. Message );
}
}

Public void Dispose ()
{
If (serialPort. IsOpen)
{
SerialPort. Close ();
}
SerialPort. Dispose ();
}

Public bool Send (string str)
{
Try
{
If (serialPort. IsOpen)
{
Console. WriteLine ("SENT:" + str );
SerialPort. WriteLine (str + "\ r ");
Return true;
}
Else
{
Return false;
}
}
Catch (Exception ex)
{
Console. WriteLine (ex. Message );
Return false;
}
}
}

For serial communication, both parties need to execute a common protocol. The so-called common protocol is that the communication parameters are the same. The communication parameters include BaudRate, Parity, DataBits, StopBits, and Handshake. The key is the baud rate (BaudRate). The communication idea should be the same for BaudRate.

 

The first parameter of the SerialPort constructor is the port number. Generally, the port number is composed of 'com' and a number, for example, com1.

All serial port operations are based on logic serial port (logical serial port). Instead of physical serial port (physical serial port), logic serial ports are mapped to physical serial ports by drivers, that is, the corresponding driver is installed on the device in use. This logical serial port exists. The logical serial port operation does not mean that the communication can be normal, and the hardware connection needs to be checked. One advantage of logical serial port operations is that the same program can operate physical or virtual serial ports.

In ReceiverPort, You need to register a receiving function serialPort_DataReceived to delegate. In this way, the processing function is called back when data is received.

Since serial port operations are unique, exclusive, and exclusive operations, it is best to use Dispose.

 

References

SerialPort Class

 

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.