It is said that tomorrow the sample was sent, but my PC serial port has not been done, thought it is difficult, dragged, read the http://www.cnblogs.com/tuyile006/archive/2008/10/06/514300.html
Then, it's even more confusing.
I was so worried today that I didn't expect it to be solved in less than two hours. It is convenient for you to make a tutorial.
Use the SerialPort class, C # built-in
For the first example, see http://book.csdn.net/bookfiles/499/10049917236.shtml
First, data is sent and received on the same serial port of a PC. We need to short connect Tx and Rx.
What are the headers and wires used for jumper in my software major?ProgramThe two feet were dropped by the key during the operation.
Public partial class form1: Form
{
SerialPort serialport1 = new SerialPort ();
Create an object serialport1, and thenCodeSee http://book.csdn.net/bookfiles/499/10049917236.shtml
In the second example, the communication between the upper computer and the lower computer is meaningless. I decided to write a read
Sample of the serial port of the mini2440 Development Board. Like dnw.
In the first example, change the event processing of the accept button
Private void button2_click (Object sender, eventargs E)
{
Serialport1.portname = "COM1 ";
Serialport1.baudrate = 115200;
Serialport1.databits = 8;
Serialport1.ededbytesthreshold = 1;
Serialport1.open ();
}
Because we want to read data asynchronously, we cannot write an endless loop in private void button2_click (Object sender, eventargs E ).
Keep reading. I want to get an event driver. If this SerialPort is not a Form Control, I just need to select it and it is easy to find related events in the attribute, double-click to add the event handling method. Unfortunately, it is not.
It really caused me to hit the game and I found the answer.
Receivedbytesthreshold attribute. When the number of data bytes in the buffer zone is greater than or equal to the value of receivedbytesthreshold of the seralport class object, the datareceived event is triggered.
First, add a method in frm1, that is, the method that is executed every time a byte is read, and the read byte is converted into ASCII code.
Void ps_datareceived (Object sender, serialdatareceivedeventargs E)
{
String svalue;
Byte [] BS;
BS = system. bitconverter. getbytes (serialport1.readbyte ());
Svalue = system. Text. encoding. ASCII. getstring (BS );
Textbox2.text + = svalue;
}
Then register the event.
Serialport1.datareceived + = new serialdatareceivedeventhandler (ps_datareceived );
During the test, the Windows form control is called across threads. This. Net freamwork2 is really annoying.
Control. checkforillegalcrossthreadcils = false;
Public form1 ()
{
Initializecomponent ();
Serialport1.datareceived + = new serialdatareceivedeventhandler (ps_datareceived );
Control. checkforillegalcrossthreadcils = false;
}
Now, the demo is complete.
Finally, take it for granted that the serial port is closed, and you do not know whether to place the serial port.
Private void form=formclosing (Object sender, formclosingeventargs E)
{
Serialport1.close ();
}