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 Interface

Subject: Reading from Serial Interface

From: Philipp

Date: 11 Feb, 2009 13:11:01

Message: 1 of 2

Hi

I have a very basic question. I have 8 bytes in my serial port object and I try to read them as follows:

[ dummy, count ] = fread(s, s.BytesAvailable);

and it outputs:

dummy = 48
    48
    48
    48
    51
    48
    51
    57

This is basically an integer in hex representation 0x00003039. Is there any way I could easily transform this into the corresponding integer value of 12345?

I tried to do the following:

fscanf(s,'%x',8)
ans = 12345

Then I get the write answer but I get a timeout warning as the terminator has not been reached. Makes sense as I just have 8 data bytes in serial port object.

Is there now any good solution for this problem?

Thanks

Subject: Reading from Serial Interface

From: Trent Jarvi

Date: 11 Feb, 2009 15:00:25

Message: 2 of 2


"Philipp " <Patrick.Bateman23@gmx.at> wrote in message
news:gmuip5$brn$1@fred.mathworks.com...
> Hi
>
> I have a very basic question. I have 8 bytes in my serial port object and
> I try to read them as follows:
>
> [ dummy, count ] = fread(s, s.BytesAvailable);
>
> and it outputs:
>
> dummy = 48
> 48
> 48
> 48
> 51
> 48
> 51
> 57
>
> This is basically an integer in hex representation 0x00003039. Is there
> any way I could easily transform this into the corresponding integer value
> of 12345?
>
> I tried to do the following:
>
> fscanf(s,'%x',8)
> ans = 12345
>
> Then I get the write answer but I get a timeout warning as the terminator
> has not been reached. Makes sense as I just have 8 data bytes in serial
> port object.
>
> Is there now any good solution for this problem?
>
> Thanks

Hi Philipp

There is a bit of code in serial/fscanf (around line 148) that looks
incorrect at first glance.

>> edit serial/fscanf
% Call the fscanf java method.

try

    if (length(size) == 1) && (size ~= 0) && strcmp(format, '%c')

                             DELETE THIS ^^^^^^^^^^^^^^^^^^^^^^^

        out = fscanf(igetfield(obj, 'jobject'), totalSize);

    else

        out = fscanf(igetfield(obj, 'jobject'), 0);

    end

catch aException

    error('MATLAB:serial:fscanf:opfailed', aException.message);

end



I'll file a bug report so we investigate why the strcmp was in there.

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