C#のシリアル入力スキャンコード銃

シリアルスキャンコード読み取りガンメインコードセグメント

1つの  公共 部分 クラスをForm1:フォーム
 2      {
 3          公共のForm1()
 4          {
 5              のInitializeComponent();
6          }
 7          SERIALPORT SP = 新しい SERIALPORT(" COM1 "115200、Parity.None、8 、StopBits.One)。
8  
9          デリゲート のボイド UpdateTextEventHandler(文字列テキスト);
10          UpdateTextEventHandler UPDATETEXT。
11  
12          民間 のボイドForm1_Load(オブジェクト送信者、System.EventArgs e)の
 13          {
 14              トライ
15              {
 16                  であれば(sp.IsOpen)
 17                  {
 18                      sp.Close()。
19                  }
 20                  sp.Open()。
21                  toolStripStatusLabel3.Text + = " レディ" 22                  toolStripStatusLabel3.ForeColor = System.Drawing.Color.Green。
23              }
 24              キャッチ(例外)
 25              {
26                  toolStripStatusLabel3.Text + = " COM1打開失敗" 27                  toolStripStatusLabel3.ForeColor = System.Drawing.Color.Red。
28              }
 29              
30  
31              UPDATETEXT + = 新しい UpdateTextEventHandler(UpdateTextBox)。
32              sp.DataReceived + = 新しい SerialDataReceivedEventHandler(sp_DataReceived)。
33          }
 34  
35                  
36          プライベート ボイド UpdateTextBox(文字列テキスト)
 37          {
 38             listBox1.Items.Add(テキスト);
39          }
 40          
41          プライベート ボイド sp_DataReceived(オブジェクト送信者、SerialDataReceivedEventArgs E)
 42          {
 43              のThread.sleep(100 )。
44  
45               readString = sp.ReadExisting()。
46              この .Invoke(UPDATETEXT、新しい 文字列[] {readString})。 
47          }
 48      }

 

おすすめ

転載: www.cnblogs.com/lakeliu/p/11968978.html