SerialPort implements communication with lbs

Source: Internet
Author: User
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)

  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. componentmodel;
  4. Using system. Data;
  5. Using system. drawing;
  6. Using system. text;
  7. Using system. Windows. forms;
  8. Using system. Io. ports;
  9. Using system. Threading;
  10. Namespace WMS. SD. deliverygoods
  11. {
  12. Public partial class frmupload: Form
  13. {
  14. Private delegate void handleinterfaceupdatedelegate (string astring );
  15. Handleinterfaceupdatedelegate interfaceupdatehandle;
  16. Static string strreceive; // buffer for receiving data
  17. Public frmupload ()
  18. {
  19. Initializecomponent ();
  20. }
  21. Private void frmupload_load (Object sender, eventargs E)
  22. {
  23. // Whether cross-thread access is allowed
  24. // Checkforillegalcrossthreadcils = false;
  25. Interfaceupdatehandle = new handleinterfaceupdatedelegate (showtext );
  26. Try
  27. {
  28. If (! Sp. isopen)
  29. {
  30. Sp. open (); // Method for enabling the serial port
  31. }
  32. Btngetweight. Enabled = false;
  33. } // Throw an exception
  34. Catch (exception ex)
  35. {
  36. MessageBox. Show (ex. Message. tostring ());
  37. }
  38. }
  39. Private void showtext (string astring)
  40. {
  41. // Display data according to the communication data specification
  42. This. lbldata. Text = astring. substring (1, 5) + "." + astring. substring (5, 1 );
  43. }
  44. Private void btngetweight_click (Object sender, eventargs E)
  45. {
  46. Try
  47. {
  48. Btngetweight. Enabled = false;
  49. If (! Sp. isopen)
  50. {
  51. Thread. Sleep (5000 );
  52. Sp. open (); // Method for enabling the serial port
  53. }
  54. } // Throw an exception
  55. Catch (exception ex)
  56. {
  57. MessageBox. Show (ex. Message. tostring ());
  58. }
  59. }
  60. Private void sp_datareceived (Object sender, system. Io. Ports. serialdatareceivedeventargs E)
  61. {
  62. Try
  63. {
  64. // Obtain the first byte
  65. If (sp. isopen)
  66. {
  67. Byte firstbyte = convert. tobyte (sp. readbyte ());
  68. // Determine whether the first byte is the start bit: hexadecimal 0x02
  69. If (firstbyte = 0x02)
  70. {
  71. // Define the length of the received data
  72. Int bytesread = sp. readbuffersize;
  73. // Data receiving byte array
  74. Byte [] bytesdata = new byte [bytesread];
  75. // Receive byte
  76. Byte bytedata;
  77. For (INT I = 0; I <= bytesread-1; I ++)
  78. {
  79. Try
  80. {
  81. If (sp. isopen)
  82. {
  83. Bytedata = convert. tobyte (sp. readbyte ());
  84. // Determine the data end byte
  85. If (bytedata = 0x03)
  86. {
  87. Break;
  88. }
  89. Bytesdata [I] = bytedata;
  90. }
  91. }
  92. Catch (exception ex)
  93. {
  94. // MessageBox. Show (ex. Message + ex. GetType (). fullname );
  95. }
  96. }
  97. // Convert the byte array into a string
  98. Strreceive = system. Text. encoding. Default. getstring (bytesdata );
  99. // This. lbldata. Text = strreceive. substring (1, 5) + "." + strreceive. substring (5, 1 );
  100. // Invoke (interfaceupdatehandle, strczdata );
  101. Begininvoke (interfaceupdatehandle, strreceive );
  102. }
  103. }
  104. }
  105. Catch (exception ex)
  106. {
  107. // MessageBox. Show (ex. Message + ex. GetType (). fullname );
  108. }
  109. }
  110. Private void btnexit_click (Object sender, eventargs E)
  111. {
  112. If (sp. isopen)
  113. {
  114. Sp. discardinbuffer ();
  115. Sp. Close ();
  116. }
  117. This. Close ();
  118. }
  119. Private void btnreset_click (Object sender, eventargs E)
  120. {
  121. Try
  122. {
  123. If (sp. isopen)
  124. {
  125. Sp. discardinbuffer ();
  126. Sp. Close ();
  127. }
  128. This. lbldata. Text = "";
  129. Btngetweight. Enabled = true;
  130. }
  131. Catch (exception ex)
  132. {
  133. MessageBox. Show (ex. Message );
  134. }
  135. }
  136. Private void frmupload_formclosing (Object sender, formclosingeventargs E)
  137. {
  138. If (sp. isopen)
  139. {
  140. Sp. discardinbuffer ();
  141. Sp. Close ();
  142. }
  143. }
  144. }
  145. }

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.