• c#串口扫码枪输入


    串口扫码枪读取主要代码段

     1  public partial class Form1 : Form
     2     {
     3         public Form1()
     4         {
     5             InitializeComponent();
     6         }
     7         SerialPort sp = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);
     8 
     9         delegate void UpdateTextEventHandler(string text);
    10         UpdateTextEventHandler updateText;
    11 
    12         private void Form1_Load(object sender, System.EventArgs e)
    13         {
    14             try
    15             {
    16                 if (sp.IsOpen)
    17                 {
    18                     sp.Close();
    19                 }
    20                 sp.Open();
    21                 toolStripStatusLabel3.Text += "Ready";
    22                 toolStripStatusLabel3.ForeColor = System.Drawing.Color.Green;
    23             }
    24             catch (Exception)
    25             {
    26                 toolStripStatusLabel3.Text += "COM1打開失敗";
    27                 toolStripStatusLabel3.ForeColor = System.Drawing.Color.Red;
    28             }
    29             
    30 
    31             updateText += new UpdateTextEventHandler(UpdateTextBox);
    32             sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
    33         }
    34 
    35                 
    36         private void UpdateTextBox(string text)
    37         {
    38             listBox1.Items.Add(text);
    39         }
    40         
    41         private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
    42         {
    43             Thread.Sleep(100);
    44 
    45             string readString = sp.ReadExisting();
    46             this.Invoke(updateText, new string[] { readString }); 
    47         }
    48     }
  • 相关阅读:
    访问虚拟机
    w3school JavaScript 简介
    蘑菇街2016研发工程师在线编程题
    乐视2017暑期实习生笔试题(二)
    今日头条2017后端工程师实习生笔试题
    c# 读取 excel文件内容,写入txt文档
    处理字符串
    XML获取节点信息值
    SVN仓库目录结构
    sql 知识点
  • 原文地址:https://www.cnblogs.com/lakeliu/p/11968978.html
Copyright © 2020-2023  润新知