0
votes

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. 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. 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. Like I said, I know for a fact that data is arriving because I can see it in 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();
        }
    }
}
1
I have solved my own problem. For anyone who has the same hellish issue that I did, I had to change the property DTREnable on the serial port object to True.user2172571

1 Answers

2
votes

Try putting

serialPort1.DataReceived += serialPort1_DataReceived;

in 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);