SerialPort implements communication with lbs
Favorites
1. Drag a SerialPort named sp.
2.
The key lies in the processing of datareceived events.
. The baud rate setting is consistent with the meter setting: 1200
. Readbuffersize can be set to the Data Length of the communication protocol: 12
Note the start and end codes: 0x02, 0x03
. Invoke implement delegation, allowing the auxiliary thread to modify the content of the main program-controlled parts (you can also set checkforillegalcrossthreadcils to directly cross-thread jobs)
(Reference: http://www.cnblogs.com/worldreason/archive/2008/06/09/1216127.html)
- Using system;
- Using system. Collections. Generic;
- Using system. componentmodel;
- Using system. Data;
- Using system. drawing;
- Using system. text;
- Using system. Windows. forms;
- Using system. Io. ports;
- Using system. Threading;
- Namespace WMS. SD. deliverygoods
- {
- Public partial class frmupload: Form
- {
- Private delegate void handleinterfaceupdatedelegate (string astring );
- Handleinterfaceupdatedelegate interfaceupdatehandle;
- Static string strreceive; // buffer for receiving data
- Public frmupload ()
- {
- Initializecomponent ();
- }
- Private void frmupload_load (Object sender, eventargs E)
- {
- // Whether cross-thread access is allowed
- // Checkforillegalcrossthreadcils = false;
- Interfaceupdatehandle = new handleinterfaceupdatedelegate (showtext );
- Try
- {
- If (! Sp. isopen)
- {
- Sp. open (); // Method for enabling the serial port
- }
- Btngetweight. Enabled = false;
- } // Throw an exception
- Catch (exception ex)
- {
- MessageBox. Show (ex. Message. tostring ());
- }
- }
- Private void showtext (string astring)
- {
- // Display data according to the communication data specification
- This. lbldata. Text = astring. substring (1, 5) + "." + astring. substring (5, 1 );
- }
- Private void btngetweight_click (Object sender, eventargs E)
- {
- Try
- {
- Btngetweight. Enabled = false;
- If (! Sp. isopen)
- {
- Thread. Sleep (5000 );
- Sp. open (); // Method for enabling the serial port
- }
- } // Throw an exception
- Catch (exception ex)
- {
- MessageBox. Show (ex. Message. tostring ());
- }
- }
- Private void sp_datareceived (Object sender, system. Io. Ports. serialdatareceivedeventargs E)
- {
- Try
- {
- // Obtain the first byte
- If (sp. isopen)
- {
- Byte firstbyte = convert. tobyte (sp. readbyte ());
- // Determine whether the first byte is the start bit: hexadecimal 0x02
- If (firstbyte = 0x02)
- {
- // Define the length of the received data
- Int bytesread = sp. readbuffersize;
- // Data receiving byte array
- Byte [] bytesdata = new byte [bytesread];
- // Receive byte
- Byte bytedata;
- For (INT I = 0; I <= bytesread-1; I ++)
- {
- Try
- {
- If (sp. isopen)
- {
- Bytedata = convert. tobyte (sp. readbyte ());
- // Determine the data end byte
- If (bytedata = 0x03)
- {
- Break;
- }
- Bytesdata [I] = bytedata;
- }
- }
- Catch (exception ex)
- {
- // MessageBox. Show (ex. Message + ex. GetType (). fullname );
- }
- }
- // Convert the byte array into a string
- Strreceive = system. Text. encoding. Default. getstring (bytesdata );
- // This. lbldata. Text = strreceive. substring (1, 5) + "." + strreceive. substring (5, 1 );
- // Invoke (interfaceupdatehandle, strczdata );
- Begininvoke (interfaceupdatehandle, strreceive );
- }
- }
- }
- Catch (exception ex)
- {
- // MessageBox. Show (ex. Message + ex. GetType (). fullname );
- }
- }
- Private void btnexit_click (Object sender, eventargs E)
- {
- If (sp. isopen)
- {
- Sp. discardinbuffer ();
- Sp. Close ();
- }
- This. Close ();
- }
- Private void btnreset_click (Object sender, eventargs E)
- {
- Try
- {
- If (sp. isopen)
- {
- Sp. discardinbuffer ();
- Sp. Close ();
- }
- This. lbldata. Text = "";
- Btngetweight. Enabled = true;
- }
- Catch (exception ex)
- {
- MessageBox. Show (ex. Message );
- }
- }
- Private void frmupload_formclosing (Object sender, formclosingeventargs E)
- {
- If (sp. isopen)
- {
- Sp. discardinbuffer ();
- Sp. Close ();
- }
- }
- }
- }