zoukankan      html  css  js  c++  java
  • 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;
    }
    }
    }
    }

  • 相关阅读:
    Spring MVC Ajax 嵌套表单数据的提交
    Spring MVC 过滤静态资源访问
    Spring MVC 页面跳转时传递参数
    IDEA Maven 三层架构 2、运行 springMVC
    IDEA Maven 三层架构 1、基本的Archetype 搭建
    EasyUI DataGrid 基于 Ajax 自定义取值(loadData)
    Spring MVC Ajax 复杂参数的批量传递
    Mybatis Sql片段的应用
    在 Tomcat 8 部署多端口项目
    自动升级的设计思路与实现
  • 原文地址:https://www.cnblogs.com/ModBus/p/8527509.html
Copyright © 2011-2022 走看看