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:
matlab serial/fgets doesn't work with non ascii chars

Subject: matlab serial/fgets doesn't work with non ascii chars

From: francesc oller

Date: 4 Feb, 2013 10:09:08

Message: 1 of 3

executing the script:

in = serial('/dev/ttyS0', 'BaudRate', 9600, 'DataBits', 8, ...
    'Parity', 'none', 'StopBits', 1, 'Terminator', 'LF');
fopen(in);
line = fgets(in);

and input data 'ñ\n' (hexa C3 B1 0A), fgets hangs and timeouts, i,e, doesn't recognize LF. This only happens when inputting extended non-ascii strings. I'm using Matlab 7.12 R2011a, can somebody test with R2012?. Thanks

Subject: matlab serial/fgets doesn't work with non ascii chars

From: Michael

Date: 4 Feb, 2013 21:31:13

Message: 2 of 3

"francesc oller" wrote in message <keo1c4$4ph$1@newscl01ah.mathworks.com>...
> executing the script:
>
> in = serial('/dev/ttyS0', 'BaudRate', 9600, 'DataBits', 8, ...
> 'Parity', 'none', 'StopBits', 1, 'Terminator', 'LF');
> fopen(in);
> line = fgets(in);
>
> and input data 'ñ\n' (hexa C3 B1 0A), fgets hangs and timeouts, i,e, doesn't recognize LF. This only happens when inputting extended non-ascii strings. I'm using Matlab 7.12 R2011a, can somebody test with R2012?. Thanks

Characters in the extended ASCII set are not supported by MATLAB fscanf (and I would presume fgets as well). I spoke with MATHWORKS support earlier this year when I encountered the same issue. There is a feature request from last year about this but the more people that ask for it, the more likely it will be implemented.

You may have better luck using fread instead of fgets but I have not tried that.

Subject: matlab serial/fgets doesn't work with non ascii chars

From: francesc oller

Date: 4 Feb, 2013 22:36:10

Message: 3 of 3

"Michael" wrote in message <kep9b1$i1t$1@newscl01ah.mathworks.com>...
> "francesc oller" wrote in message <keo1c4$4ph$1@newscl01ah.mathworks.com>...
> > executing the script:
> >
> > in = serial('/dev/ttyS0', 'BaudRate', 9600, 'DataBits', 8, ...
> > 'Parity', 'none', 'StopBits', 1, 'Terminator', 'LF');
> > fopen(in);
> > line = fgets(in);
> >
> > and input data 'ñ\n' (hexa C3 B1 0A), fgets hangs and timeouts, i,e, doesn't recognize LF. This only happens when inputting extended non-ascii strings. I'm using Matlab 7.12 R2011a, can somebody test with R2012?. Thanks
>
> Characters in the extended ASCII set are not supported by MATLAB fscanf (and I would presume fgets as well). I spoke with MATHWORKS support earlier this year when I encountered the same issue. There is a feature request from last year about this but the more people that ask for it, the more likely it will be implemented.
>
> You may have better luck using fread instead of fgets but I have not tried that.

Thanks Michael. Programming an fgets replacement in terms of fread is the obvious choice and it works well

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