Reputation: 11
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SerialPort
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
cmdClose.Enabled = false;
foreach (String s in System.IO.Ports.SerialPort.GetPortNames())
{
txtPort.Items.Add(s);
}
}
public System.IO.Ports.SerialPort sport;
public void serialport_connect(String port, int baudrate , Parity parity, int databits, StopBits stopbits)
{
DateTime dt = DateTime.Now;
String dtn = dt.ToShortTimeString();
sport = new System.IO.Ports.SerialPort(
port, baudrate, parity, databits, stopbits);
try
{
sport.Open();
cmdClose.Enabled = true;
cmdConnect.Enabled = false;
txtReceive.AppendText("[" + dtn + "] " + "Connected\n");
sport.DataReceived += new SerialDataReceivedEventHandler(sport_DataReceived);
}
catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); }
}
private void sport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
DateTime dt = DateTime.Now;
String dtn = dt.ToShortTimeString();
txtReceive.AppendText("["+dtn+"] "+"Received: "+sport.ReadExisting()+"\n");
}
private void cmdConnect_Click(object sender, EventArgs e)
{
String port = txtPort.Text;
int baudrate = Convert.ToInt32(cmbbaudrate.Text);
Parity parity = (Parity)Enum.Parse(typeof(Parity), cmbparity.Text);
int databits = Convert.ToInt32(cmbdatabits.Text);
StopBits stopbits = (StopBits)Enum.Parse(typeof(StopBits), cmbstopbits.Text);
serialport_connect(port, baudrate, parity, databits, stopbits);
}
private void button1_Click(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
String dtn = dt.ToShortTimeString();
String data = txtDatatoSend.Text;
sport.Write(data);
txtReceive.AppendText("[" + dtn + "] " + "Sent: " + data + "\n");
}
private void cmdClose_Click_1(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
String dtn = dt.ToShortTimeString();
if (sport.IsOpen)
{
sport.Close();
cmdClose.Enabled = false;
cmdConnect.Enabled = true;
txtReceive.AppendText("[" + dtn + "] " + "Disconnected\n");
}
}
}
}
Hi all, I get into trouble with thrown InvalidOperationException when I try to set my Baud rate from 9600 to 115200.
Any idea why that is? Thanks in advance.
Upvotes: 0
Views: 540
Reputation: 109862
What's happening here is that you are getting a serial port callback from a non-UI thread, and in that callback you are trying to change a UI component.
You're not allowed to do that, and you'll get the exception you're seeing.
The best solution to this depends on what version of .Net you're using, but whatever version you're using you can always use Control.BeginInvoke()
to solve it.
There's some sample code on the link I provided.
Note that it's not setting the baud rate that's causing the problem. It's the code in sport_DataReceived()
that tries to update a UI component (which you can see from the second image you posted).
You can probably change that function as follows:
private void sport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.BeginInvoke(new Action(() =>
{
DateTime dt = DateTime.Now;
String dtn = dt.ToShortTimeString();
txtReceive.AppendText("[" + dtn + "] " + "Received: " + sport.ReadExisting() + "\n");
}));
}
What this does is cause the code for the Action
above to be called on the UI thread.
Upvotes: 1