Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
reading from serial device

Subject: reading from serial device

From: Dave Brackett

Date: 26 Sep, 2008 21:00:20

Message: 1 of 3

Hi I have a custom made datalogger that I can access using Excel with VBA code using a USB-serial converter.

I am trying to use matlab instead but am having difficulties as fopen(s) doesn't return anything.

When running the serial('COM10') function the number of bytes available is 0. Should that be the case? My settings for the port are correct apart from that I am unsure what the terminator is required to be. Do you think that would be causing the problem?

The working VBA code is beneath the matlab code. sorry for the long post.

function data_log()
% Create a serial port object
instrreset
delete(instrfindall)
s=serial('COM10','BaudRate',115200,'Parity','none','DataBits',8,'StopBits',1,'Terminator','LF/CR','TimeOut',5); % create serial object (port dependent)

% Connect to the device
fopen(s) % open the serial port for reading/writing

% Set logging options
no_samples=30;
no_channels=1;
current_sample=zeros(1,no_samples); % preallocation
tic

% Write and read data
% 1) read data from the device and store in the input buffer
for i=1:no_samples-1
    i
    current_sample(i)=i;
    fprintf(s,'%d',1); % send a decimal 1 (not ascii code 1), prompts the logger to start sending data
    if i==1
        buffer=[];
    end
    [in,count,msg]=fread(s);
    buffer=buffer+in;
    while length(buffer)<8 %checks when the number of bits in buffer =8
        j=1;
        for n=1:no_channels
            hi=buffer(j,j:j+1);
            lo=buffer(j+1,j+1:j+2);
            % work out whether positive or negative number,
            % if greater than 128 then its positive
            if hi<128
                adc_value=hi*256+lo;
            else
                adc_value=(65536-(hi*256+lo))*-1;
            end
            j=j+2;
            % outputs as voltage
            voltage_output(i)=round(adc_value/3276.8)
        end
    end
    clear buffer
    sample_time=toc;
end

% Disconnect and clean up
fclose(s)
delete(s)

VBA Code that works:
Sub Button10_Click()
Dim buffer As String
Dim rs As New MSComm
Dim adc(4) As Variant

Call Macro1 'clear worksheet
Cells(2, 1) = "scan" 'create title
Cells(2, 2) = "Time" 'create title
For n = 3 To 6
Cells(2, n) = "ADC" & Str(n - 2) 'create title
Next n

rs.CommPort = 10 'port device is attached to
rs.Settings = "115200,n,8,1" ' 1) Baud rate, 2) parity none, 3)no bits, 4)number of stop bits
rs.InputLen = 0 'allows the buffer to continuously fill
rs.PortOpen = True 'open the port

t = Timer
x$ = rs.Input 'x is a string, rs.Input is reading the serial COM port, effectively clearing the buffer

For samps = 1 To Cells(1, 2) 'for samples 1 to maximum specified in cell(1,2) of sheet
    rs.Output = Chr$(1) 'outputs the number '1' to the datalogger as trigger
    buffer = "" 'clears the string buffer
    Do
    DoEvents
    buffer = buffer + rs.Input
    Loop While Len(buffer) < 8 'checks when the no-bits =8
        Cells(samps + 2, 1) = samps 'current sample no
        Cells(samps + 2, 2) = Timer - t 'current time
        j = 1
        For n = 1 To 4 'for all 4 channels
            hi = Asc(Mid(buffer, j, 1)) 'finding the jth digit +next digit
            lo = Asc(Mid(buffer, j + 1, 1))
            If hi < 128 Then 'works out whether positive or negative number, if high is greater than 128 then its positive
                adc_value = hi * 256 + lo
            Else
                adc_value = (65536 - (hi * 256 + lo)) * -1
            End If
            j = j + 2
            Cells(samps + 2, n + 2) = Round(adc_value / 3276.8, 4) 'outputs voltage for chan 1 to third column in sheet
        Next n 'carry on for next channel
Next samps 'carry on for next sample

rs.PortOpen = False 'close port

End Sub

Subject: reading from serial device

From: ehab ibrahim

Date: 27 Sep, 2008 01:03:02

Message: 2 of 3

Hi dave

I had the same problems with the serial port

my problem is that the matlab only sees the port (com10) just for one run
and I have to close the matlab and open it again in order the matlab recognizes the port again.

I didn`t figure out the problem, so if you figure it out please tell me, and this aslo applies on me.

Subject: reading from serial device

From: Trent Jarvi

Date: 29 Sep, 2008 14:49:25

Message: 3 of 3


"ehab ibrahim" <ehabr9@yahoo.com> wrote in message
news:gbk0o6$51b$1@fred.mathworks.com...
> Hi dave
>
> I had the same problems with the serial port
>
> my problem is that the matlab only sees the port (com10) just for one run
> and I have to close the matlab and open it again in order the matlab
> recognizes the port again.
>
> I didn`t figure out the problem, so if you figure it out please tell me,
> and this aslo applies on me.
>
>

Hi Ehab

If you are using a traditional serial port, this should not be a problem.
After you close the serial port in your M-script, make sure you delete the
serial object. To clean up even if you did not delete the serial port, you
can do the following:

delete(instrfindall)

I know that some interfaces like bluetooth with serial like drivers can be
problematic. If the serial ports are vanishing and reappearing as
connections are lost, You will run into the behavior your described. Check
to make sure you have the latest driver.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us