2
votes

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.

1
Your last statement is somewhat contradictory: "All numbers from 0 to 255 display on the LDC correctly, I have no idea why numbers 128 to 159 incorrectly display as 63."Jabberwocky
I don't know anything about Arduino, but a brief RTFM reveals that Serial.read() returns -1 when there is no data available. You don't check this, so how do you know there is data available?Lundin
@Michael Walz I have edited my question accordingly. I was trying to say that numbers 128 to 159 display incorrectly as the value 63, all other numbers from 0 to 255 display as their correct value.James Archer
@Lundin thanks for pointing that out. I added an if statement to only display a value when there is data available, but this doesn't solve the problem unfortunately.James Archer
And what happens when you also add if (Serial.available() > 0) before reading serial data?Lundin

1 Answers

1
votes

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

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