简体   繁体   中英

Matlab -> Arduino Serial commnication

I'm trying to send some data from Matlab to Arduino,but I'm not receiving what I expect, when I send a '5' in a double-variable, I receive the character 'e' at the Arduino Terminal.

The Arduino code is:

void setup()
{
  Serial.begin(9600);
}

void loop()
    {
        if (Serial.available())
        {
           Serial.write(Serial.read());
        }
}

And the matlab code is:

data=5
PS=serial('COM11','BaudRate',9600);

fopen(PS);

fwrite(PS,data)

fclose(PS);
delete(PS);
clear PS;
  Serial.write(Serial.read()); 

this sends back exactly what you receive, so it's not really helpful to find out what you received.

  • Do you have the possibility to send it somewhere else ?
    Serial1, Softserial, lcd ?
  • Serial.read() returns an int, so you might use Serial.println(Serial.read())
    to see the code of what you got
  • ... and the number of bytes you received
  • sending/reveiving text instead of binary sometimes makes life easier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM