I am trying to return a value from an RTU by using a serial port connection. My settings are 19.2k, parity none, 8 and 1. I am trying retrieve information by typing in a TLP. The code below is what I currently have from going off another example but I'm not sure how to type in a TLP, click a button to send the data and then receive it back in another text box.
public partial class Form1 : Form
{
string myString;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 19200;
serialPort1.Open();
serialPort1.DtrEnable = true;
serialPort1.RtsEnable = true;
if (serialPort1.IsOpen)
{
button1.Enabled = true;
textBox2.ReadOnly = true;
}
}
private void Form1_FormClosing(object sender, FormClosedEventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!serialPort1.IsOpen)
{
return;
}
char[] buff = new char[1];
buff[0] = e.KeyChar;
serialPort1.Write(buff, 0, 1);
e.Handled = true;
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(myString);
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
myString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
}