• C# serialPort的DataReceived事件无法触发 ,用的霍尼韦尔的扫码枪并且装了相应的USB转串口驱动。


    昨天想试试霍尼韦尔的扫码枪,扫码枪有两种模式,键盘模式和串口模式,

    1、键盘模式直接插上就行了,就像一个键盘一样不需要任何驱动,扫出来的数据直接落到PC的输入焦点上。就像一个键盘一样,只能输入字符。

    2、而串口模式,则是安装驱动以后,能够虚拟成一个COM口,

    如果我们用扫码枪给应用程序输入数据的时候肯定是不行的,因为程序需要能够在后台运行的时候也能用扫码枪作为数据输入的。这就需要用串口模式了。

    今天我写程序的时候,问题是怎么也不能触发,DataReceived事件,最后发现问题所在,几个要点

    1、串口必须New新实例,必须和能够正确获取硬件串口的名称。

    2、sp.ReceivedBytesThreshold = 1; 这个是默认值就是1,就是有一个字节的数据就触发事件。

    3、sp.RtsEnable = true;sp.DtrEnable = true;这两个属性必须得正确设置,哈

    我就是因为第3个原因一直不不能正确触发事件。记得添加事件绑定代码啊。

    public partial class Form1 : Form
    {
    SerialPort sp;
    bool bIsReading = false;
    // Dynamic d = new Dynamic();

    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    bool b = false;
    try
    {
    sp = new SerialPort();
    sp.BaudRate = 115200;
    sp.DataBits = 8;
    sp.Parity = Parity.None;
    sp.StopBits = StopBits.One;
    sp.PortName = System.IO.Ports.SerialPort.GetPortNames()[0];

    sp.RtsEnable = true;
    sp.DtrEnable = true;
    sp.ReadTimeout = 3000;
    sp.ReceivedBytesThreshold = 1;
    sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.sp_DataReceived_1);
    // sp.DataReceived += Sp_DataReceived;
    // sp.DataReceived -= Sp_DataReceived;
    sp.Open();


    }
    catch (Exception xe)
    {
    b = true;
    MessageBox.Show("异常:" + xe.Message);

    }
    finally {
    if (sp != null && b == true)
    {
    if (sp.IsOpen)
    {
    sp.Close();

    } else
    {
    sp = null;
    }

    }

    b = false;
    }

    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {

    //sp.DiscardInBuffer();
    if (sp != null)
    {
    sp.Close();
    sp.Dispose();
    }
    }

    private void sp_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
    {


    if (bIsReading == true)
    return;
    bIsReading = true;
    try
    {
    StringBuilder currentline = new StringBuilder();
    //循环接收数据
    while (sp.BytesToRead > 0)
    {
    char ch = (char)sp.ReadByte();
    currentline.Append(ch);
    }
    //在这里对接收到的数据进行处理
    MessageBox.Show("数据为:" + currentline.ToString());

    //GlobalPublic.BasePublic.ShowMessage("数据为:" + currentline.ToString(), (BaseFrm as BaseForm.FrmBaseDoc).MTC_oGlobal);
    //InvokeDelegate del = new InvokeDelegate(setItem);
    //this.BeginInvoke(del, currentline.ToString());
    //setItem(currentline.ToString());
    // currentline = new StringBuilder();
    }
    catch (Exception ex)
    {
    MessageBox.Show("异常:" + ex.Message);
    }
    finally
    {
    bIsReading = false;
    }
    }
    }
    }

  • 相关阅读:
    springboot中filter的配置和顺序执行
    springboot整合fastdfs实现上传和下载
    移动端通过fiddler代理调试PC端代码
    react-native windows 环境搭建
    带你逐行阅读redux源码
    前端单页面拆分多个单页面
    Koa2学习(九)与mongoDB交互
    Koa2学习(八)使用session
    Koa2学习(七)使用cookie
    Koa2学习(六)使用koa-router
  • 原文地址:https://www.cnblogs.com/ModBus/p/8527509.html
Copyright © 2020-2023  润新知