Asp.net c serialPort displays the data code // serial port is a so-called serial port or serial port. It is one of the interfaces of a computer. It is used to connect to some devices. If you do not need to know it carefully, you can use it as an interface similar to USB. But sometimes the serial port does not support hot swapping
Asp tutorial. net c serialport display data code
// Serial port is the so-called serial port or serial port. It is one of the interfaces of a computer. It is used to connect to some devices. If you do not need to know it carefully, you can use it as an interface similar to usb. But sometimes the serial port does not support hot swapping
Void comm_datareceived (object sender, serialdatareceivedeventargs e)
{
Int n = comm. bytestoread; // record it first to avoid some reason, artificial reasons, long operation times, inconsistent Cache
Byte [] buf = new byte [n]; // declare a temporary array to store the current Serial Port Data
Received_count + = n; // increase the receiving count.
Comm. read (buf, 0, n); // read the buffered data
Builder. clear (); // clear the content of the string Constructor
// To access the ui resources, you must use the invoke method to synchronize the ui.
This. invoke (eventhandler) (delegate
{
// Determine whether the display is 16 forbidden
If (checkboxhexview. checked)
{
// Concatenate a hex string in sequence
Foreach (byte B in buf)
{
Builder. append (B. tostring ("x2") + "");
}
}
Else
{
// Convert the string directly according to ascii rules
Builder. append (encoding. ascii. getstring (buf ));
}
// Append the object to the end of the text box and scroll to the end.
This. txget. appendtext (builder. tostring ());
// Modify the receipt count
Labelgetcount. text = "get:" + received_count.tostring ();
}));
}
// Method 2
Private void serialport1_datareceived (object sender, serialdatareceivedeventargs e)
{
System. threading. thread. sleep (100 );
Int bytes = sepor. bytestoread;
Byte [] buffer = new byte [bytes];
If (bytes = 0)
{
Return;
}
Sepor. read (buffer, 0, bytes );
String srt = string. empty;
Foreach (byte a in buffer)
{
Srt + = a. tostring ();
}
Textbox1.text = srt;
}
Private void form1_load (object sender, eventargs e)
{
Sepor. portname = "com4"; // select the serial port com4
Sepor. baudrate = 4800; // set the communication port Parameters
Sepor. databits = 7; // data bit
Sepor. parity = system. io. ports. parity. none; // The check bit is not verified.
Sepor. stopbits = system. io. ports. stopbits. one; // one Stop bit
Sepor. readbuffersize = 1024;
Sepor. open ();
Sepor. readexisting (); // sets the input to read all data from the receiving buffer.
Sepor. receivedbytesthreshold = 1; // you can specify the length of the byte that triggers the oncomm event.
Sepor. discardinbuffer (); // clearly accept the buffer
Timer1.start ();
}