简体   繁体   中英

Matlab Serial buffer issue

I am attempting to communicate from my Simblee Rfduino to Matlab, with the following Arduino code:

char testing[] = {1,2,3,4,5,6,7,8,'\0'};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(6,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(6,LOW);
  Serial.flush();
  testing(EMG);
  Serial.flush();
  digitalWrite(6,HIGH);
}

In Python I am able to correctly read in the 1-8 in the correct order consistently.

However in Matlab it continuously changes order with no consistency, with the following code:

function serial()
global ser 
ser = serial('COM5', 'BaudRate', 9600, 'FlowControl', 'hardware');
fopen(ser);

end 
function serial_callback(~, ~)
    global ser
    time = tic;
    fread(ser,1) % pull in data from serial port
    toc(time);    
end

I think there could be some issue in the Serial buffer. Could you please provide some guidance as to how to consistently get Matlab to read in the data in order? Have others been able to get Matlab to reliably read from a serial port? Thank you!

You could set the FlowControl to software (xOn and xOff flags). Hardware is only possible if you have the "hardware ressources".

The input buffer of matlab usually works with fifo principle.

After fopen() you have to wait for one second. Because some microcontrollers (like arduino uno ...) restarts after initialization of the uart interface.

--> pause(1);

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