Electronics > Microcontrollers

Arduino Serial Read issue.

(1/7) > >>

PeterG:
Hi all,
   i am probably over looking something here but here goes.

I have hooked up my Arduino board to my PC and am sending data to the arduino in 2 byte blocks. The first byte appears to be being read ok. However the second byte appears to be trimmed down to 6 bits in length.

Is there some reason this would happen, i am looking to read a value from 0 to 190. But anything over 127 reverts back to 63.

Regards

Psi:
Few thoughts...

1) Have a look at the variable you're using to store the data.

If it's a 'signed' byte then it can only store between negative 128  and positive 127

You want an 'unsigned' byte, which can store between 0 and 255

2) Are you waiting for the byte to be fully received before reading it out.  Im not sure how Arduino do this but there should be some sort of a wait or loop to check that a byte is ready to be read.

3) Check that you're initializing the serial port correctly and to the same settings that your PC is using.
etc.  number of bits, stop bits, start bits, parity

PeterG:
This is the code i am using at the moment.




--- Code: ---if (Serial.available() >1) {

       byte inByte = Serial.read();  //According to the Aruino docs, a byte is unsigned. 0-255
       byte DataIn = 0;
          if (inByte = 255) {            // This does trigger when the first byte is 255.
            DataIn = Serial.read();  // Read the second byte.
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("Read Value is: ");
            lcd.print(DataIn );         // LCD displays correct for values from 0 to 127.
          }
      }
--- End code ---

Regards

amyk:

--- Quote from: Psi on March 23, 2012, 08:39:55 am ---3) Check that you're initializing the serial port correctly and to the same settings that your PC is using.
etc.  number of bits, stop bits, start bits, parity

--- End quote ---
I second this, given the documentation doesn't say anything about the port parameters other than baud rate despite the MCU's serial port having the capability for that.

Psi:
how does the lcd.print() command know to display a number (0-255) or to display the ascii character for that number?

Maybe the LCD only supports a 0-127 character set?


Try an IF/ELSE statement that checks if the received data is the value expected.
Then does either lcd.print('correct') or lcd.print('wrong')

That way you can prove or eliminate the lcd as a cause of the issue.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version