Asp.net c serialPort display data code

Source: Internet
Author: User
Tags foreach

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 ();
  }

Related Article

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.