Reading from Serial - only reads 1 character..

Hi guys,

I’m using python (together with a python library called pySerial) to transmit a 2 digit number over the Serial port.

Then from the arduino code I am trying to read the value of this 2 digit number using the Serial.read() However it reads data as ASCII characters by default, and only read the first ASCII character and ignores the second character.

I tried converting the number being sent from the Python side to be transmitted as a binary number, but the arduino still only reads the first digit and disregards the rest of the digits. And yes I am using a loop to try read the 8 binary digits: here’s the code:

do
{
while (!Serial.available())
readBinaryFromSerial[posCounter] = Serial.read();
} while (++posCounter < 8);

Can anybody help me please? Is it possible to read a 2 digit number over serial as an ASCII character or can it only be done in Binary, and if so how do I code the Arduino to read it?

Please help me out guys :frowning:

Thanks Matt

do
 {
   while (!Serial.available()) [glow];[/glow]  // <--- Add a semicolon here.  Even better would be to add empty braces {}.
   readBinaryFromSerial[posCounter] = Serial.read();
 } while (++posCounter < 8);

Even better:-
while (Serial.available() <= 2) ; // wait until you have two bytes to read
readBinaryFromSerial[posCounter] = 256* Serial.read(); // read most significant byte
readBinaryFromSerial[posCounter] |= Serial.read(); // OR in the least significant byte

But note you might be reading ASCII characters not binary ones. What is sending this?