首先我开始使用C#昨天上午,所以我为潜在的麻木的问题道歉。
到目前为止,我写了下面的代码。 我能够从C#的数据发送到Arduino的,这似乎是工作的罚款。 现在我想从Arduino的接收数据,通过serial.print,在同一时间,并在框中显示它(列表框?)。 怎么能添加此的代码?
我将感谢任何输入,提示和建议。
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");
}
}