Need help reading in input values to matlab!

Hello everyone. I am desperately trying to read in data from a channel 5 set to input. The channel is giving a full range of values using the pololu terminal. I am also setting values to servos from matlab with no problem.

The problem comes in trying to read into matlab. code is below. I converted the hex mini protocol 0x90 to decimal “144” to read the channel “5”. On the next commend if you were to do an fread you will get a timeout error and have to reset both matlab and pololu to restart the program. Any thoughts?

s = serial('COM4',...
'Baudrate',9600, ...
'DataBits',8, ...
'Parity','none', ...
'InputBufferSize',16384)

s.ReadAsyncMode='manual'

fopen(s)
% fwrite(s,[255,5,setting],'uint8')
% fwrite(s,[132,0,0, setting],'uint8')
fwrite(s,[144,5],'uint8')
% [a,b]=fread(s,2,'uint16')
% readasync(s)
% fread(s,1,'uint8')

fclose(s)
delete(s);
clear('s');

So I’ve learned that the problem is timing;

If you close the system too fast, it will never read values.
If you wait to long to read values, the buffer will clear (?? - this did not prove true even up to 10 seconds, so it only stores what it reads not empty)

by inserting an empty for loop which measures at Elapsed time is 0.039179 seconds, i can now see 2 input values were dumped into the read buffer!

The threshold at which this stops working about 50% of the time is nearly, Elapsed time is 0.011278 seconds.
This would mean that some function (i.e. baud rate, timing) only operates at about 100 HZ. It is also interesting that it is on/off, meaning that I didn’t ever capture one bit.

This seems like a sloppy way to address the issue. At least now i can 1) time it and 2) check the buffer before reading so it doesn’t crash. Its also important to realize that Fread doesn’t read from the serial port, it reads the buffer. So if you already closed the serial port, you’ll never get anything! wow.

Hello.

It is hard to tell what you are doing because all of your code for actually reading is commented out, but I did notice one thing:

[a,b]=fread(s,2,'uint16')

The line above probably reads in two uint16s, for a total of 4 bytes of input. It’s trying to read too much data (4 instead of 2 bytes) so it would probably tend to timeout.

If you need more help, please post the simplest code that works and the simplest code that doesn’t work.

–David