Richard Castle

Richard Castle

  • NA
  • 14
  • 757

Poll Serial Port with sequence of queries, manual commands

May 21 2020 10:30 AM

Hello I am trying to develop an interface that sends queries to the serial port automatically. I would also like to send specific commands at will as well.

Please see code below. I apologize if it looks sloppy. I am still learning.

- Form loads and connects to serial port - Done, with manual connect and disconnect buttons as well
- Poll sequence of queries and read responses - (I've got it done manually on two separate queries via single button click, with thread.sleep(100) (not sure if this is efficient)
- Display the responses in their corresonding textboxes (also done manually from the two separate queries, converted to float to display on seven segment control). Crashes if I only do one query, I think because of the datareceived event. How to handle?

- Discard responses from serial port when sending a "command" versus a "query"- ?????
 
My ultimate goal is to create a "real-time" interface that is constantly querying the serial port up to 5 queries, but can also manually check specific queries, and enter commands while discarding responses from a command.
 
namespace SerialPort
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
cmdClose.Enabled = false;

}
public System.IO.Ports.SerialPort myport;
public void serialport_connect(String port, int baudrate , Parity parity, int databits, StopBits stopbits)
{

myport = new System.IO.Ports.SerialPort(
port, baudrate, parity, databits, stopbits);
try
{
myport.Open();
cmdClose.Enabled = true;
cmdConnect.Enabled = false;
txtReceive.Text = ("Connected\n");
myport.DataReceived += new SerialDataReceivedEventHandler(myport_DataReceived_1);
}
catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); }
}
public void myport_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
{

txt1.Text= (myport.ReadExisting().ToString().Replace(“@”, "").Replace(“O”, "").Replace(“A”, "") + "\n");
float f1 = (float)double.Parse(txt1.Text);
SevenSegment.Value = f1;

Thread.Sleep(100);
 
txt2.Text= (myport.ReadExisting().ToString().Replace(“@”, "").Replace(“O”, "").Replace(“A”, "") + "\n");
float f2 = (float)double.Parse(txt2.Text);
SevenSegment2.Value = f2;

}
private void Form1_Load(object sender, EventArgs e)
{
String port = "COM1";
int baudrate = 9600;
Parity parity = Parity.None;
int databits = 8;
StopBits stopbits = StopBits.One;
serialport_connect(port, baudrate, parity, databits, stopbits);
}

private void cmdConnect_Click(object sender, EventArgs e)
{
String port = "COM1";
int baudrate = 9600;
Parity parity = Parity.None;
int databits = 8;
StopBits stopbits = StopBits.One;
serialport_connect(port, baudrate, parity, databits, stopbits);


}
private void cmdClose_Click_1(object sender, EventArgs e)
{

if (myport.IsOpen)
{
myport.Close();
cmdClose.Enabled = false;
cmdConnect.Enabled = true;
txtReceive.AppendText("Disconnected\n");
}
}

private void btn1_Click(object sender, EventArgs e)
{

byte[] byte1 = { 0x4f, 0x49, 0x0D };
myport.Write(byte1, 0, byte1.Length);
txt1.AppendText(byte1 + "\r" + "\n");

}
private void btn2_Click(object sender, EventArgs e)
{
byte[] byte2 = { 0x2f, 0x33, 0x0D };
myport.Write(byte2, 0, byte2.Length);
txt2.AppendText(myport.ReadExisting().ToString().Replace(“@”, "").Replace(“O”, "").Replace(“A”, "") + "\n");

}
 
 
//This is the code that works ok, but for the single queries, it crashes. 
private void btn12_Click(object sender, EventArgs e)
{
byte[] byte1 = { 0x3f, 0x56, 0x0D };
myport.Write(byte1, 0, byte1.Length);
txt1.AppendText(byte1 + "\r" + "\n");

Thread.Sleep(100);

byte[] byte2 = { 0x3f, 0x49, 0x0D };
myport.Write(byte2, 0, byte2.Length);
txt2.AppendText(byte2 + "\r" + "\n");
}
}

Answers (1)