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.