James Archer
James Archer

Reputation: 421

Sending integers via serial port and printing to LCD

I am trying to send integers via the a USB serial port from MATLAB to an Ardunio Uno and then subsequently display them on an LCD. I have a problem with numbers from 128 to 159 displaying as 63 on the Arduino display.

Here is my MATLAB code:

q = serial('COM4'); % set com port location
set(q,'BaudRate',9600); % set baud rate
fopen(q); % open the port
fprintf(q,a_number) % send the integer

Here is my Arduino code:

int incomingByte = 0; // storage for integer
void serialRead () // 
{
   incomingByte = Serial.read(); // read the serial port 
   if (Serial.available() > 0) // if there is data print it to LCD
   {
      lcd.setCursor(0,1);  // set the cursor to column 0, line 1 
      lcd.print("Boot:     %");
      delay(500);
      lcd.setCursor(6,1);
      lcd.print(incomingByte,DEC); // print the Integer to the LCD
      delay(500);
   }
}

All numbers from 0 to 255 are displayed correctly apart from numbers 128 to 159 which are displayed as the value 63.

Update: I tested the serial port on my from my computer using a serial analyzer and it looks like MATLAB is responsible for sending the data incorrectly. I tested the Arduino code separately and it works perfectly.

Upvotes: 2

Views: 977

Answers (1)

James Archer
James Archer

Reputation: 421

Solved the problem added the following line in my MATLAB code in place of the fprintf line:

fwrite(q,a_number,'uint16','sync');

Upvotes: 1

Related Questions