C-通过串行端口发送整数并打印到LCD



我试图通过从MATLAB到Ardunio Uno的A USB串行端口发送整数,然后随后在LCD上显示它们。我的数字从128到159的数字有问题,在Arduino显示器上显示为63。

这是我的MATLAB代码:

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

这是我的arduino代码:

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);
   }
}

所有0到255的数字均在数字128到159中正确显示,这些数字显示为63。

更新:我使用串行分析仪从计算机上测试了串行端口,看来MATLAB负责错误地发送数据。我分别测试了Arduino代码,它运行得很好。

解决了问题,在我的matlab代码中添加了以下行,代替了fprintf行:

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

最新更新