interfacing rs232 using matlab (fread command)

Status
Not open for further replies.

dark_plan

Newbie level 2
Joined
Mar 26, 2010
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Philippines
Visit site
Activity points
1,311
Hello!

I am working on a matlab project now that communicates with a device (an fpga) via rs232. The configuration is 8 data bits, no parity bits, 1 stop bit and the baud rate is 115200.

Now my device is sending 8 bits to the computer and I plot the data. The problem is that matlab reads the data differently. In the 8 bits that the fpga sends, the most significant bit is the sign bit (for negative values) while bits 6 to 0 is the value. There isn't any problem with the positive values but I'm having problems with the negative values.

For example, when the fpga sends 127, the binary input to the computer is 01111111. When data is -127, the binary input to the computer is 10000001, which matlab converts to decimal as 1. Similarly, when the fpga sends 1, binary input is 00000001 while if -1, binary input is 11111111 which matlab reads as 127.

So is there any way I could configure matlab to read the negative values correctly? I am using the fread command to read data from the rs232. I have tried specifying the precision to 'schar' as matlab help says it's a signed 8-bit character type but it seems like matlab ignores the sign bit and reads only bits 6 to 0. I have also tried 'char' and 'int8' but I get the same result as using 'schar'.

Thanks!
 

It seems that MatLab ignores the MSB or force it to be zero.
If you create serial port object with correct configuration, you can slow down the baudrate and try again.
In my project, it works fine with 8-N-1 and 9600.
 

I successfully transfered data to matlab using the following example:


Here is the full article:

Using the Serial Port with a MATLAB GUI

What are the property settings retrieved by get(serial).
Also have you made sure the data coming from the device is indeed signed byte?

Hope this helps your quest.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…