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.

[MOVED] Reading binary data over the serial port

Status
Not open for further replies.

isamel85

Newbie level 6
Newbie level 6
Joined
Nov 6, 2011
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,416
Hello

In fact, I want to acquire a 8-bit binary data on serial port using Matlab (eg 00001100 in binary or 12 decimal). I make a test loop back just sending that data over the port and receive on the same port, knowing that sending code works fine and I checked it by HyperTerminal.

I tried this code for the acquisition in Matlab script:


Code dot - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
s=serial('COM1'); % Create a serial port object
%Configure property values
s.InputBufferSize=50000;
s.BaudRate=9600;
fopen(s); %open the port
% Here the beginning of the reading binary data -----------------------------------------------------------
index = 1;
nb_bytes = get(s,'BytesAvailable');
while (index < get(s,'inputBuffersize'))
if nb_bytes
out= fread(s, nb_bytes,'uint8');
index = index + nb_bytes;
end;
pause(0.2);
end;
%End of reading code-------------------------------------------------------------------------------------------
%Disconnect and clean up
fclose(s);
delete(s);
clear s;



But I get nothing. Maybe the code missed something.

Please can someone help me to solve this problem?

Thanks in advance for any help.

Best regards,
Isamel
 
Last edited by a moderator:

Re: Reading binary data over the serial port

I don't see anywhere where you send data to the serial port. If you are in loopback test, you should send the data from the same port. Somewhere after fopen, try send data on serial port and check for 'ByteAvailable' on the port.

If the data was sent previously by an other application. See the comment about ByteAvailable from Matlab.
"BytesAvailable indicates the number of bytes currently available to be read from the input buffer. The property value is continuously updated as the input buffer is filled, and is set to 0 after the fopen function is issued."

it seems that after a fopen, the input buffer is flushed.
 

Re: Reading binary data over the serial port

The last code missed the writing data; I put only the opening and closing of the port and the reception of the data; now you can check the entire code:

Code:
s=serial('COM1'); % Create a serial port object

%Configure property values
s.InputBufferSize=50000;
s.OutputBufferSize=50000;
s.BaudRate=9600;

fopen(s); %open the port

%flush the input buffer
flushinput(s);
pause(0.01);

% Here the beginning of the sending binary data -----------------------------------------------------------
binary_data_in_8bits=00001100;%12 in decimal
decimal_data=bin2dec(binary_data_in_8bits)% conversion of the data from binary to decimal
fwrite(s,decimal_data,'uint8','async');
%End of sending code-------------------------------------------------------------------------------------

% Here the beginning of the reading binary data -----------------------------------------------------------
index = 1;
nb_bytes = get(s,'BytesAvailable');
while (index < get(s,'inputBuffersize'))
if nb_bytes
out= fread(s, nb_bytes,'uint8');
index = index + nb_bytes;
end;
pause(0.2);
end;
%End of reading code-------------------------------------------------------------------------------------------

%Disconnect and clean up
fclose(s);
delete(s);
clear s;
Please help me to solve this problem?

Thanks in advance for your help.

Best regards,
Isamel
 
Last edited by a moderator:

Re: Reading binary data over the serial port

I guess that the while loop cause the problem. nb_bytes should be 1 and you will execute the loop 'inputBuffersize' time. Out will be overwritten on each loop.

Remove your while loop for a test and check what you get.

Also add a pause after the fwrite to let the serial port do its job. Try stepping your code to see where is the problem.

Good luck
 

Re: Reading binary data over the serial port

Basically do standard debugging steps. In this case, something like: Get known to work example serial script from your favorite matlab example script repository. Pay attention to the fact that serial communication example will be different for windows vs linux. Verify that example works. Use working example as template/inspiration for your code.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top