我已经编写了从串口读取数据作为字节数组并将其显示在文本框中的代码。代码编译正常,但无法正常工作:从串口读取字节
private void button2_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == false)
serialPort1.Open();
serialPort1.WriteLine(textBox1.Text);
int bytes = serialPort1.BytesToRead;
byte[] byte_buffer = new byte[bytes];
serialPort1.Read(byte_buffer, 0, bytes);
//textBox2.Text = " ";
for (int t = 0; t < bytes; t++)
{
textBox2.Text += (byte_buffer[t]).ToString();
}
}
你有调试过吗? textBox1.Text中是否有价值? 另外你如何创建串行端口?串口端口=新串口( “COM1”,9600,Parity.None,8,StopBits.One); ETC ... – Pogrindis
_“代码无法正常工作”_ - 解释原因,显示输入和输出以及您希望发生的事情。 – CodeCaster
yes.i已经使用SerialPort port = new SerialPort(“COM1”,9600,Parity.None,8,StopBits.One)创建了串口。有时在textbox1没有价值,有时错误的价值“的代码无法正常工作”的意思是:例如,当我在键盘上按“s”我看不到它在textbox1中的值。 –