简体   繁体   English

USB-Serial Converter不会在C#中触发DataReceived

[英]USB-Serial Converter does not trigger DataReceived in C#

I am writing a program in C# which I would like to read data in from a USB port and write into a text box as it is received. 我正在用C#编写一个程序,我希望从USB端口读取数据并在收到时写入文本框。 I have a multimeter that is sending data (verified via PuTTY) over a RS-232, and I have the 232 hooked up to a Prolific 2303 USB-Serial converter. 我有一个通过RS-232发送数据(通过PuTTY验证)的万用表,我将232连接到Prolific 2303 USB-Serial转换器。 I used device manager to find out that this is COM4, and checked the specifications for the multimeter to find the baud, stop bits, parity, etc. I'm using System.IO.Ports.SerialPort, and I set up the serial port object with the appropriate COM reference, baud, etc. For some reason, the DataReceived event just never seems to get triggered because my delegate function doesn't execute. 我用设备管理器发现这是COM4,并检查万用表的规格,找到波特率,停止位,奇偶校验等。我正在使用System.IO.Ports.SerialPort,我设置了串口具有适当的COM引用,波特等的对象。由于某种原因,DataReceived事件似乎永远不会被触发,因为我的委托函数没有执行。 Like I said, I know for a fact that data is arriving because I can see it in PuTTY. 就像我说的那样,我知道数据到达的事实是因为我可以在PuTTY中看到它。 Below is my code with hopes that someone can help me please... Let me know if you need some more information to figure out what's going on with this. 以下是我的代码,希望有人可以帮助我...如果您需要更多信息来弄清楚这是怎么回事,请告诉我。 Many, many thanks in advance! 很多,非常感谢提前!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Reflection;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class dataAcquisitionMain : Form
    {
        string RxString;

        public dataAcquisitionMain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)//"Start" button
        {
            serialPort1.PortName = "COM4";
            serialPort1.BaudRate = 2400;

            serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                inputStream.ReadOnly = false; //A text box I want to put data into
                startButton.Enabled = false;
                stopButton.Enabled = true;
            }
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            serialPort1.DataReceived += serialPort1_DataReceived;
            RxString = serialPort1.ReadExisting();
            this.Invoke((MethodInvoker)delegate{DisplayText();});
        }
        private void DisplayText()
        {
            textBox1.Text = "here";
            inputStream.Text = RxString;
            inputStream.AppendText(RxString);
        }

        private void dataAcquisitionMain_FormClosing(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen) serialPort1.Close();
        }
    }
}

Try putting 试试看

serialPort1.DataReceived += serialPort1_DataReceived;

in dataAcquisitionMain(). 在dataAcquisitionMain()中。

It looks like you are trying to initialize the event that calls 看起来您正在尝试初始化调用的事件

serialPort1_DataReceived;

In the event handler. 在事件处理程序中。

If that makes sense. 如果这是有道理的。

Try: 尝试:

SerialPort.comPort.DataReceived +=new SerialDataReceivedEventHandler(comPort_DataReceived);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM