first of all I started using c# yesterday morning so I apologize for potential numb questions.
So far I wrote the following code. I'm able to send data from c# to arduino which seems to be working fine. Now i would like to receive data from arduino, through serial.print, at the same time and display it in a box (ListBox?). How could add this to the code?
I would be thankful for any input, tips and ideas.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
stop.Enabled = false;
left.Enabled = false;
right.Enabled = false;
up.Enabled = false;
down.Enabled = false;
string[] ports = SerialPort.GetPortNames();
comboBox1.Items.AddRange(ports);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void start_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex > -1)
{
string port = (string)comboBox1.SelectedItem;
serialPort2.PortName = port; // "port";
serialPort2.BaudRate = 9600;
serialPort2.Open();
if (serialPort2.IsOpen)
{
start.Enabled = false;
stop.Enabled = true;
left.Enabled = true;
right.Enabled = true;
up.Enabled = true;
down.Enabled = true;
}
}
else
{
MessageBox.Show("Please connect the Arduino and select a Port");
}
}
private void stop_Click(object sender, EventArgs e)
{
if (serialPort2.IsOpen)
{
serialPort2.Close();
start.Enabled = true;
stop.Enabled = false;
left.Enabled = false;
right.Enabled = false;
up.Enabled = false;
down.Enabled = false;
}
}
private void up_Click(object sender, EventArgs e)
{
if (serialPort2.IsOpen)
{
serialPort2.WriteLine("1");
}
}
private void left_Click(object sender, EventArgs e)
{
if (serialPort2.IsOpen)
{
serialPort2.WriteLine("4");
}
}
private void right_Click(object sender, EventArgs e)
{
if (serialPort2.IsOpen)
{
serialPort2.WriteLine("2");
}
}
private void down_Click(object sender, EventArgs e)
{
if (serialPort2.IsOpen)
{
serialPort2.WriteLine("3");
}
}