Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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
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:

Receiving Data
Although MATLAB is supposed to support both writing and reading data from the serial port, reading data seems to either produce no result, generate an error, or crash MATLAB. To remedy the situation GetSerialData() has been written. This function will allow you to get vectors of data from the serial port buffer.

Setting Up GetSerialData.cpp
You can download a copy of GetSerialdata.dll and skip this step. If you wish to modify the code for GetSerialData.cpp to handle other serial port protocols (such as handshaking and other features) you can use this to help you re-make the code.

Files you will need:
•GetSerialData.cpp
•stdafx.h
To compile the code, change to the directory (in MATLAB) with GetSerialData.cpp. Type the command:


mex GetSerialData.cpp
MATLAB may ask you to set up a compiler. Choose the MATLAB compiler (It is usually one of the first options and has the word MATLAB somewhere in its path). After doing this, repeat the 'mex' command on the code. Note: This code will only work with Windows (only been tested on XP).

Compiling the C code produces a .dll file. The file at this stage is similar to a .m file in that it adds custom functionality to MATLAB. To use the file, place it in the directory where you will use the GetSerialData function.

Using GetSerialData with the DSP
GetSerialData should work with both the assembly and C implementations of outputting data to the serial port. Sometimes a DSP will not output any serial port data. Often times this means this feature is broken on the DSP, but occasionally you can get the serial port to output data if you halt your program, press the red button a couple of times, flip the switch twice, and reload your program. To test the port for incoming data, load up the program 'Hyperterm' (StartMenu:Accessories:Communications:Hyperterm). Connect to com2 with data rate 38400 and look for ascii characters. It is suggested that you test for data first with the terminal and not MATLAB because if there is no data coming into MATLAB, it will stall until the function times out.

Note: You do not need to worry about opening or closing the com2 port because the function does it all under the hood. The port must be available for usage (so if MATLAB was writing to the port earlier, it has to be closed).
Once the DSP is running code that outputs data to the serial port, it continuously sends the data. GetSerialData simply 'captures' the output from the buffer and records it to a MATLAB row vector of specified size. The calling format is:


y = GetSerialData('port', baud, size);
•'port' is the serial port to which the DSP is connected. For our use it will be 'com2'. The port name must be entered in quotes.
•baud is the speed at which we transfer data. For the DSPs in lab we use 38400.
•size is the length of the row vector you want to acquire.
•y is the output vector.
After calling the function, it will not return until it has receive size bytes from the serial port. If it never receives the bytes, it will eventually time out. Since the serial port only outputs single bytes at a time, the max integer that can be acquired is 255 and the min is 0. If you wish to use signed numbers, a fourth parameter can be entered into the function to specify. To see notes on usage and other baud rates, ports, signed data, etc type:

GetSerialData('help');
This will bring up a help screen with the full set of options.

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top