C#与Arduino之间的串行通信(字节)

huangapple go评论160阅读模式
英文:

Serial Communication between C# and Arduino (bytes)

问题

I am Sending an byte from C# winform Application to Arduino And Recieving it back on Winform Application and display it on Text box.

Arduino code

  1. void setup()
  2. {
  3. // put your setup code here, to run once:
  4. Serial.begin(9600);
  5. }
  6. int numBytes = 1;
  7. void loop()
  8. {
  9. if (Serial.available() > 0)
  10. {
  11. byte data = Serial.read();
  12. Serial.print(data);
  13. delay(1000);
  14. }
  15. }

C# code

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. serialPort1.Write(new byte[] { 0x52 }, 0, 1);
  4. }

This is serial port receiving function

  1. private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  2. {
  3. //read data
  4. byte data = (byte)serialPort1.ReadByte();
  5. this.Invoke(new EventHandler(delegate
  6. {
  7. richTextBox1.Text += data;
  8. }));
  9. }

I am sending byte 0x052. I want that byte displayed on text box, but this will appear in the textbox as 5650. I do not know what I am doing wrong. I actually want to send 3 bytes and receive 3 bytes from both Arduino and C#. I tried many answers from Stack Overflow but failed to establish proper and correct communication between C# and Arduino.

英文:

I am Sending an byte from C# winform Application to Arduino And Recieving it back on Winform Application and display it on Text box.

Arduino code

  1. void setup()
  2. {
  3. // put your setup code here, to run once:
  4. Serial.begin(9600);
  5. }
  6. int numBytes =1;
  7. void loop()
  8. {
  9. if (Serial.available() > 0)
  10. {
  11. byte data = Serial.read();
  12. Serial.print(data);
  13. delay(1000);
  14. }
  15. }

C# code

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. serialPort1.Write(new byte[] { 0x52 }, 0, 1);
  4. }

This is serial port receiving function

  1. private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  2. {
  3. //read data
  4. byte data = (byte)serialPort1.ReadByte();
  5. this.Invoke(new EventHandler(delegate
  6. {
  7. richTextBox1.Text += data;
  8. }));
  9. }

I am sending byte 0x052 I want that byte displayed on text box but this will appear in textbox 5650.
I do not know what I am doing wrong. I actually want to send 3 byte and received 3 byte from both Arduino and C#.I tried many answers from stack overflow but failed to make proper and correct communication between C# and Arduino.

答案1

得分: 2

在Arduino端执行以下代码:

  1. byte data = Serial.read();

这将获取正确的数值,即 data = 0x52 或者十进制为 82

但是当你执行以下代码:

  1. Serial.print(data);

它会以ASCII形式发送该数值。

Serial.print(82) 会发送 "82",其中 "8" 对应ASCII码为 56,而 "2" 对应ASCII码为 50。这就是为什么在C#端你会得到 "5650"

如果你想在C#端看到数值以 0x52/82 的形式显示,你需要使用以下代码:

  1. Serial.write(data);
英文:

On the Arduino side when you do:

  1. byte data = Serial.read();

That is getting the proper value, i.e. data = 0x52 or 82 in decimal

But then when you do:

  1. Serial.print(data);

it's sending the value in ascii.

Serial.print(82) sends "82", "8" = 56 in ascii and "2" = 50 in ascii. That's why you end up with "5650" on the C# side.

If you want the value to show up as 0x52/82 on the C# side you instead need to use:

  1. Serial.write(data)

huangapple
  • 本文由 发表于 2023年8月5日 00:56:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定