標(biāo)題: modbus上位機(jī) Test_SerialPort c#源碼 [打印本頁]

作者: 木木棉百合    時(shí)間: 2018-4-16 10:28
標(biāo)題: modbus上位機(jī) Test_SerialPort c#源碼
modbus上位機(jī)運(yùn)行界面:


Test_SerialPort的c#源程序如下:
  1.   /*
  2.             //讀取串口中一個字節(jié)的數(shù)據(jù)
  3.             String ch = mySerialPort.ReadExisting();

  4.             switch (ch)
  5.             {
  6.                 case "$":
  7.                     //接收到串口頭
  8.                     ReceiveData = "";
  9.                     break;
  10.                 case "\n":
  11.                     //接收到串口尾

  12.                     //在擁有此控件的基礎(chǔ)窗口句柄的線程上執(zhí)行委托Invoke(Delegate)
  13.                     //即在控件textBoxInformation的父窗口form中執(zhí)行委托.
  14.                     textBoxInformation.Invoke
  15.                     (
  16.                         new MethodInvoker
  17.                         (
  18.                             delegate
  19.                             {
  20.                                 //textBoxInformation.AppendText(ReceiveData);
  21.                                 textBoxReceiveData.Text = ReceiveData;
  22.                             }
  23.                         )
  24.                      );
  25.                     break;
  26.                 default:
  27.                     ReceiveData += ch;
  28.                     break;

  29.              }

  30.             int ch = mySerialPort.ReadByte();
  31.             string str = string.Empty;
  32.             switch (ch)
  33.             {
  34.                 case 0x12:
  35.                     //接收到串口頭,清空數(shù)組
  36.                     Array.Clear(ReceiveData, 0, ReceiveData.Length);
  37.                     ReceiveDataIndex = 0;
  38.                     break;
  39.                 case 0x14:
  40.                     //接收到串口尾,輸出string
  41.                     for (int i = 0; i < ReceiveData.Length; i++)
  42.                     {
  43.                         str += (ReceiveData[i] - '0').ToString();
  44.                     }
  45.                     //在擁有此控件的基礎(chǔ)窗口句柄的線程上執(zhí)行委托Invoke(Delegate)
  46.                     //即在控件textBoxInformation的父窗口form中執(zhí)行委托.
  47.                     textBoxInformation.Invoke
  48.                     (
  49.                         new MethodInvoker
  50.                         (
  51.                             delegate
  52.                             {
  53.                                 //textBoxInformation.AppendText(ReceiveData);
  54.                                 textBoxReceiveData.Text = str;
  55.                             }
  56.                         )
  57.                      );
  58. ……………………

  59. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼



所有資料51hei提供下載:
串口Mudbud.rar (79.71 KB, 下載次數(shù): 273)





作者: 875113301    時(shí)間: 2018-10-11 13:23
感謝大佬
作者: smilexxy    時(shí)間: 2018-11-21 14:13
感謝大佬分享
作者: 597543289    時(shí)間: 2019-1-19 15:59
挺好的一個學(xué)習(xí)的例子,可以修改成自己的項(xiàng)目程序
作者: 51cc    時(shí)間: 2019-2-14 17:05
正好參考。。
作者: xtuwz    時(shí)間: 2019-3-7 17:26
急需要,怎么獲取
作者: ly81222    時(shí)間: 2019-3-13 07:45

急需要,怎么獲取
作者: Alan_zcb    時(shí)間: 2019-3-27 11:04
感謝大佬,學(xué)習(xí)了。
作者: Garfieldsss    時(shí)間: 2019-4-9 21:48
感謝大佬

作者: hxa4056    時(shí)間: 2019-9-27 08:46
感謝大佬
作者: 神蜂    時(shí)間: 2020-3-29 16:20
感謝大佬,下載學(xué)習(xí)
作者: xintiandi    時(shí)間: 2020-5-11 22:05
支持一下。。。
作者: nymz    時(shí)間: 2020-6-6 22:27

感謝大佬分享




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1