Re: reading serial port data



Hi Trent.

I'm fairly certain that the entry point into the data IS random, which leaves me with a fairly large problem. If I don't know which of the 1's is the first bit of the first byte, then I cannot possibly get the correct order. The c-code I have should sort this out, but I can't get it to compile in Matlab. It is:-

#include <stdafx.h>
#include<windows.h>
#include<stdio.h>
#include <serial.h>

int main_higher_res_4byte(int*sPort)
{
int bytes_expected;
int result;
unsigned char c[10];
short int x,y;

setBaud(*sPort, 57600);

bytes_expected = 4;
while(1) {
result = sPortRead(*sPort, c, bytes_expected);

if(result!=-1) {

x = (c[1] & 0x7f) | ((c[3] & 0xf) << 7);
if(x>0x3ff) x = x - 0x7ff;
y = (c[2] & 0x7f) | ((c[0] & 0xf) << 7);
if(y>0x3ff) y = y - 0x7ff;
printf("%02x %02x %02x %02x [%d] [%d]\n", c[0],c[1],c[2],c[3], x, y);
}
}
return 1;
}

When I try to compile this as a mex-file I get error messages about the functions "setBaud" and "sPortRead". Do you have any suggestions about which commands might be the correct ones? If I could make this program work then most of the other problems would simply go away, and I could concentrate on trying to interface this program with the other packages I am using.
(Incidentally, all the 'include *.h' files at the start are just guesses on my part. I was hoping that one of them would contain the protocol for the commands that Matlab isn't happy about. I had no luck going down that road).

Gordon.




Thanks for the quick reply. I have managed to get my code to at least be
stable (rather than hanging up and failing to access the Com port every
time I get an error), but I still don't know how to extract the data I
need.
The structure of the data packet is:-

byte d7 d6 d5 d4 d3 d2 d1 d0
1 1 lb mb rb y10 y9 y8 y7
2 0 x6 x5 x4 x3 x2 x1 x0
3 0 y6 y5 y4 y3 y2 y1 y0
4 0 0 0 0 x10 x9 x8 x7

where lb mb and rb are the buttons on the joystick and x10..x0 is the
eleven bit word for the x co-ordinate. My stripped down code to read this
is:

s = serial('COM1');
set(s,'BaudRate',57600,'DataBits',8,'Parity','none','StopBits',1);
s.InputBufferSize = 4;
s.ByteOrder = 'bigEndian';
%inspect(s)
fopen(s);
for index = 1:12
if s.BytesAvailable>=4
joy = fread(s,4);
disp(joy)
x_temp = dec2bin(joy);
disp(x_temp)
end
end

fclose(s)
delete(s)
clear s

Using this, with the joystick sitting idle, at what should be position
(0,0) or very close to it, the output I get is like this:

test
192
136
138
192

11000000
10001000
10001010
11000000
136
137
192
136

10001000
10001001
11000000
10001000

None of these bytes remotely resemble the actual structure I am expecting
to see. I am assuming that the integers are in int8 form as a default, but
I don't know to change them to get the correct binary data to extract my
co-ordinate data. Any suggestions would be welcome. I have read the
documentation on the typecast function and I suspect that it is what I
need to use, but I don't know where to begin.

Hi Gordon

I could picture the bitorder being reversed. In that case the second packet
may have a start bit in the second byte and your entry point was random.
The first packet suggests one of a few issues. It could be a serial com
setting is not matching the device resulting in bit errors. There may be
other information besides the packets being sent. There could be a physical
wire issue.

The dec2bin output is what I'd focus on and try to get the first and last
bit to make sense. One side should be comming in 1,0,0,0.


.



Relevant Pages