serial/fprintf of non-string?

1 view (last 30 days)
The documentation of serial/fprintf shows four possible modes in the representative forms, and in each case the data to be sent is shown as a string. However, the format parameter can include any of many different non-character item specifiers, and one of the examples is
fprintf(s,['ch:%d scale:%d'],[1 20e-3],'sync');
which demonstrates a case where the data to be sent is not a string.
Despite this, a user is reporting to me that in R2013a that they were told
Error using serial/fprintf (line 84)
The third input argument must be a string.
Error in Untitled (line 10)
fprintf(s, '*%d', data(:)) ;
where data is uint8 (obtained via imread)
Unfortunately I cannot test this on my own system as I do not have a serial port. Could someone confirm that non-string data can be used in serial fprintf, and could they test whether uint8 is the problem?
(Assume for the moment that the serial buffer is large enough to hold the entire output; do not test with large arrays as the default output buffer is only 512 bytes.)

Accepted Answer

Nalini Vishnoi
Nalini Vishnoi on 6 May 2015
Hi Walter,
I can reproduce the behavior on a system with a serial port. The documentation (mostly the syntax section) of serial/fprintf indicates that the data to be sent to the device needs to be a string. However in the example you mentioned the format parameter includes different non-character item specifiers (specifically %d - which is signed decimal notation). When I opened that function, I noticed that serial/fprintf function accepts only 'string' and 'double' data types. Hence the error while trying to pass an array which is of type 'uint8'. If you have not tried already, you can try these workarounds:
1. Cast the uint8 value as double:
>> fprintf(s, '*%d', double(data(:))) ;
2. Use sprintf to build the 'cmd' string which is then passed as an argument to serial/fprintf function:
>> str = sprintf('%u\n',data);
>> fprintf(s, str);
3 Use fwrite function which writes binary data (could be uint8) to the device.
>> fwrite(s, data, 'uint8');
I work with MathWorks and would communicate the ambiguity in the documentation of the function and the possibility of allowing other data types (such as 'uint8') as accepted arguments, to the respective development teams.
-Nalini
  7 Comments
shamsudheen p
shamsudheen p on 3 Jul 2015
Edited: shamsudheen p on 3 Jul 2015
I have modified the programme code as below
A = imread('download.png');
if true
s=serial('COM46');
s.InputBufferSize = 5000;
s.OutputBufferSize= 2^30;
s.Terminator= 'CR';
s.BaudRate = 9600;
fopen(s);
data = permute(A, [2, 1, 3]);
fprintf(s, '*%d', double(data(:))) ;
end
fclose(s);
delete(s);
clear s;
a time out error is araised as
Error using serial/fprintf (line 144) Unexpected Error: A timeout occurred during the write operation.
Error in tr (line 9) fprintf(s, '*%d', double(data(:))) ;
Walter Roberson
Walter Roberson on 3 Jul 2015
Edited: Walter Roberson on 3 Jul 2015
The default is 10 seconds, which would be enough to transfer 9600 text bytes at 9600 baud. You send a minimum of 2 text bytes per input element, and can send up to 4 text bytes per input element, so your transfer rate is between 240 and 480 elements per 10 seconds. That is, of course, discouraging; you should consider higher baud rates.
(However if you are sending completely over USB with no actual serial port interface, then the 9600 baud is more or less decoration -- but still a large enough file could take more than 10 seconds.)

Sign in to comment.

More Answers (0)

Categories

Find more on Code Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!