2

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(" | ");
}
}
  • 2
    Because you have a serial baud rate mismatch? Because you have a pointless delay() in your serial receiving code? Try showing all your Arduino code - it might help us diagnose it. – Majenko Jan 19 '15 at 10:19
  • @Majenko, I have Tripple Checked the baud rate, and the "pointless delay()" is there from testing in frustration - But surely has no effect on the comms. The C# code is everything, below is the complete Arduino code, with a little added, to show you the reason for the delay - I added a blinking LED out of frustration, thiking that maybe because I was talking to the Serial PINs and had the USB connected for the Serial Monitor at the same time, it could cause an Issue (I need to add here that I am a Newbie to Arduino) – BFG Jan 19 '15 at 12:32
  • Added Code mentioned to the Original (Edited) post – BFG Jan 19 '15 at 12:33
  • @Majenko The examples above work if I send from C# to Arduino through the COM3 usb port (USB on PC to USB on Arduino) but does not work when sending to the Arduino Nano through the Serial PINs from the PC's USB to Serial converter. – BFG Jan 19 '15 at 12:52
  • 2
    Ah well, there you go then. Firstly, you may have killed the Arduino's serial pins, and secondly that cable is not suitable for direct connection to a microcontroller. You need an RS-232 to TTL converter chip like a MAX232, or the right kind of cable (USB to TTL UART), like an FT232 or PL2303 cable. – Majenko Jan 19 '15 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.