0

I am trying to develop a piece of software which can listen and talk to a serial port(COM3, COM5, etc.). The file 'form1.cs' is copied and pasted below.

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.Ports;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        string DispString;
        public Form1()
        {
            InitializeComponent();
        }

        private void StartCOM1(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM1";
            StartCOM_General();
        }

        private void StartCOM3(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM3";
            StartCOM_General();
        }

        public void StartCOM_General()
        {
            serialPort1.BaudRate = 115200;
            serialPort1.Parity = Parity.None;
            serialPort1.StopBits = StopBits.One;
            serialPort1.Handshake = Handshake.None;
            try { serialPort1.Open(); } finally{ }
            serialPort1.ReadTimeout = 2000;
            if (serialPort1.IsOpen)
            {
                DispString = "";
            }
        }

        private void ResetClosePort(object sender, EventArgs e)
        {
            DispString = "";
            textBox1.Text = "";
            serialPort1.Close();
        }

        private void UpdateData(object sender, SerialDataReceivedEventArgs e)
        {
            DispString += serialPort1.ReadExisting();
            textBox1.Text = DispString;
        }
    }
}

The issue I have is that my port doesn't display anything. Even though my serialPort object's event for 'DataReceived' is set to call UpdateData, I don't think that UpdateData is being called at all.

This is the image of the form, with the event tab open for serialPort1. enter image description here

Max
  • 6,821
  • 3
  • 43
  • 59
AwesomeCronk
  • 421
  • 6
  • 16
  • The image adds nothing to the question at all. Also you havent hooked up the event. maybe you should read about events a little – TheGeneral Oct 05 '19 at 22:09
  • Please read the section about threading concerns in the [SerialPort.DataReceived documentation](https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.datareceived?view=netframework-4.8) – Klaus Gütter Oct 06 '19 at 09:37

2 Answers2

0

You could check with

MessageBox.Show("Data recieved!");

in UpdateData(..) to see if UpdateData is getting called.
If it is maybe try replacing

DispString += serialPort1.ReadExisting();

with

SerialPort sp = (SerialPort)sender;
DispString += sp.ReadExisting();
Luzian
  • 1
  • 2
0

As stated in the SerialPort.DataReceived documentation, the event is signaled on a secondary thread. Therefore, you have to properly marshal back to the UI thread using Control.Invoke, e.g.:

    private void UpdateData(object sender, SerialDataReceivedEventArgs e)
    {
        DispString += serialPort1.ReadExisting();
        textBox1.Invoke((MethodInvoker)(() => textBox1.Text = DispString));
    }
Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36