繁体   English   中英

C#和arduino之间的串行通信

[英]Serial communication between c# and arduino

我正在尝试向arduino MEGA的Serial1发送一些字节。 我正在发送此byte[] writebuffer = { 1, 2, 3, 4 }; 但是Serial在arduino中的输出是127 191 247 0

我正在使用DB9,我已经将GND连接到GND,Tx连接到Rx1,Rx连接到Tx1(从DB9到arduino的连接)。

这是我的C#代码:

SerialPort sepo = new SerialPort("COM6", 9600);
sepo.Open();
byte[] writebuffer = { 1, 2, 3, 4 };
sepo.Write(writebuffer, 0, writebuffer.Length);
sepo.Close();

这是arduino代码:

void setup()
{
  Serial.begin(115200);
  Serial1.begin(9600);
}
void loop()
{
  if(Serial1.available())
  {
     while(Serial1.available())
     {
        Serial.print((byte)Serial1.read());
     }
     Serial.println();
     Serial1.println("recibi datos");
  }
} 

我建议您在打开串口之前先关闭串口,然后检查串口是否打开。

同样,您应该使用基于max232或类似产品的ttl usart转换器,或基于ft232或ch340的USB到串行转换器。 这是因为arduino具有5V ttl串行端口,而台式机具有12V端口。

不能将PC串行直接连接到Arduino,因为arduino和PC之间的电压不同,因此无法正常工作。 我现在正在使用FTDI,并且运行良好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM