C#获取Honeywell voyager 1400g扫码后的数据

一、在类方法中加入
  System.IO.Ports.SerialPort com;
二、在构造方法中加入
  try
  {
      com = new System.IO.Ports.SerialPort("COM3",19600);
    com.Open();
    com.DataReceived += Com_DataReceived;
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }

三、加入接收扫码后的数据的方法
  private void Com_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  {
    StringBuilder sb = new StringBuilder();
    do
    {
      int count = com.BytesToRead;
      if (count == 0)
        return;
      receiveBytes = new byte[count];
      com.Read(receiveBytes, 0, count);
      for (int i = 0; i < receiveBytes.Length; i++)
      {
        char c = Convert.ToChar(receiveBytes[i]);
        sb.Append(c);
      }

  } while (com.BytesToRead > 0);
string str=sb.ToString();
}

四、在窗体关闭方法中加入

if (com.IsOpen)
{
  com.Close();
  com.Dispose();
}

原文地址:https://www.cnblogs.com/dingguidong/p/5057627.html