0
votes

I have a CyberGlove connected to Matlab via serial port. I am trying to write a command requesting a data sample, and then reading the sample back.

I have successfully connected the glove with the following code:

s = serial('/dev/ttyS0')
set(s,'BaudRate',115200)
fopen(s)

I can then write/read to get the sensor information either in binary or ascii.

In binary I am doing:

fwrite(s,'G')
fread(s)

fread always times out and then spits out a column vector of seemingly random length (1-100+) containing meaningless integers.

With ASCII, the commands are:

fprintf(s,'g')
fscanf(s)

This gives an empty string as the read-out value. I know that the glove is getting and at least somewhat processing the commands, though, because if I give it an invalid command, I get back the error message e?.

Here's the part that really confuses me, though: I accidentally discovered a way to get a correct reading from the glove.

fread(s) (which times out and gives the seemingly random output)

fprintf(s,'g')

fscanf(s)

I then get the string output 'g 1 76 93 113 89 42 20 77 98 106 117 81 62 23 52 60 34 68 57 254 92 26', which is correct.

My questions are:

(1) Why does that last part produce a correct response?

(2) How can I get a reading from the binary command? This is what I actually want to acquire.

1
This sounds like its more a problem with the communication protocol of the device and not a Matlab problem.A. Donda
So, it seems I should probably test it in something else, then? Is there a simple command line method I could use for this?Julia Ebert
Good question... Not sure whether this is the best way, but I guess you could use cat /dev/ttyS0 for reading and printf ... > /dev/ttyS0 for writing. printf is a bash built-in. But first thing I'd recommend is to check the documentation of the device and see whether you understand everything.A. Donda

1 Answers

1
votes

Figured out the problem: The timeout was occurring because it wasn't getting back as many bytes as it expected, and it was expecting the incorrect number. By default, get(s, 'InputBufferSize') returned 512, while the actual data was 24 bytes. Before calling fopen(s), I just ran set(s, 'InputBufferSize', 24); and got the correct 24 8-bit integers from the binary read/write that I was expecting, without a timeout.