Apologies if I have missed a relevant post somewhere, but have search for days to try figure this out...
I have written some C# code to send a Byte Array over Serial from my PC to an Arduino Nano board (Through a USB to Serial Converter)
Why would I not receive the same HEX Values (in the Arduino serial monitor) as the Values I sent?
(The Values should be the HEX for numbers 0 - 9)
C# Code:
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 Code:
void loop()
{
while (Serial.available()) {
delay(250);
byte thisByte = Serial.read();
Serial.print(thisByte,HEX);
Serial.print(" | ");
}
}
Values received in Arduino Serial Monitor:
D6 | B6 | 96 | 76 | 56 | 36 | 16 | F6 | 8D | 0 |
COMPLETE ARDUINO CODE WITH BLINKING LED TEST CODE
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
}
void loop()
{
while (Serial.available()) {
delay(250);
// get the new byte:
byte thisByte = Serial.read();
// add it to the inputString:
//inputString += char(intChar);
if(thisByte == 0x30 || thisByte == 0x39)
{
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
}
Serial.print(thisByte,HEX);
Serial.print(" | ");
}
}