与arduino串行通信

时间:2015-01-16 10:40:13

标签: c# c++ serial-port arduino

为了简单明了,我删除了上一个问题......

我编写了一些C#代码,通过Serial将字节数组发送到Arduino Nano板(通过USB转串口转换器)

为什么我没有收到与我发送的值相同的HEX值(在Arduino串行监视器中)?

(值应为数字0 - 9的HEX)

C#代码:

if(!serialPort1.IsOpen)
        {
            serialPort1.BaudRate = 9600;
            serialPort1.DataBits = 8;
            serialPort1.StopBits = System.IO.Ports.StopBits.One;
            serialPort1.Parity = System.IO.Ports.Parity.None;
            serialPort1.PortName = "COM" + cbCOMPort.Text;

            serialPort1.Open();
        }

        byte[] ByteArr = new byte[10];
        ByteArr[0] = 0x30; 
        ByteArr[1] = 0x31;
        ByteArr[2] = 0x32;  
        ByteArr[3] = 0x33;
        ByteArr[4] = 0x34;
        ByteArr[5] = 0x35;
        ByteArr[6] = 0x36;
        ByteArr[7] = 0x37;
        ByteArr[8] = 0x38;
        ByteArr[9] = 0x39;


        serialPort1.Write(ByteArr, 0, ByteArr.Length);
        serialPort1.Close();

Arduino代码:

void loop()
{
while (Serial.available()) {
delay(250);
// get the new byte:
byte thisByte = Serial.read(); 
// add it to the inputString:
//inputString += char(intChar);

Serial.print(thisByte,HEX); 
Serial.print(" | ");
}
}

Arduino串行监视器中收到的值:

D6 | B6 | 96 | 76 | 56 | 36 | 16 | F6 | 8D | 0 |

0 个答案:

没有答案