E
espro88
Guest
Hi everyone,
I fairly new to C# and have been trying to create a program that connects to a port and sends text to it. However when I try to connect or disconnect to the port it throws an error: port is already open. I've never worked with serial port communication before, so I'm having a hard time figuring out what I'm doing wrong. The goal is to open the port, send a hex command to setup the screen (clear the screen), then write a new string to the port, and finally disconnect from the port. Any help would be greatly appreciated.
public partial class Form1 : Form
{
static string device_name = "COM3";
static int baud_rate = 19200;
SerialPort sp = new SerialPort(device_name, baud_rate, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
// open port
private void btnConnect_Click(object sender, EventArgs e)
{
byte[] clear_screen = new byte[2] { 0xFE, 0x58 };
// setup screen
try
{
sp.Close();
sp.Open();
sp.Write(clear_screen, 0, clear_screen.Length);
}
catch (Exception E) { MessageBox.Show(E.ToString(), "Error"); }
}
// close port
private void btnDisconnect_Click(object sender, EventArgs e)
{
byte[] save_screen = new byte[2] { 0xFE, 0xC2 };
try
{
sp.Write(save_screen, 0, save_screen.Length);
sp.Close();
}
catch (Exception E) { MessageBox.Show(E.ToString(), "Error"); }
}
// write to port
private void btnWrite_Click(object sender, EventArgs e)
{
byte[] write_line = new byte[2] { 0xFE, 0x40 };
string str = textBox1.Text.ToString();
try
{
sp.Close();
sp.Open();
sp.Write(str);
sp.Write(write_line, 0, write_line.Length);
}
catch (Exception E) { MessageBox.Show(E.ToString(), "Error"); }
}
}
Continue reading...
I fairly new to C# and have been trying to create a program that connects to a port and sends text to it. However when I try to connect or disconnect to the port it throws an error: port is already open. I've never worked with serial port communication before, so I'm having a hard time figuring out what I'm doing wrong. The goal is to open the port, send a hex command to setup the screen (clear the screen), then write a new string to the port, and finally disconnect from the port. Any help would be greatly appreciated.
public partial class Form1 : Form
{
static string device_name = "COM3";
static int baud_rate = 19200;
SerialPort sp = new SerialPort(device_name, baud_rate, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
// open port
private void btnConnect_Click(object sender, EventArgs e)
{
byte[] clear_screen = new byte[2] { 0xFE, 0x58 };
// setup screen
try
{
sp.Close();
sp.Open();
sp.Write(clear_screen, 0, clear_screen.Length);
}
catch (Exception E) { MessageBox.Show(E.ToString(), "Error"); }
}
// close port
private void btnDisconnect_Click(object sender, EventArgs e)
{
byte[] save_screen = new byte[2] { 0xFE, 0xC2 };
try
{
sp.Write(save_screen, 0, save_screen.Length);
sp.Close();
}
catch (Exception E) { MessageBox.Show(E.ToString(), "Error"); }
}
// write to port
private void btnWrite_Click(object sender, EventArgs e)
{
byte[] write_line = new byte[2] { 0xFE, 0x40 };
string str = textBox1.Text.ToString();
try
{
sp.Close();
sp.Open();
sp.Write(str);
sp.Write(write_line, 0, write_line.Length);
}
catch (Exception E) { MessageBox.Show(E.ToString(), "Error"); }
}
}
Continue reading...