How to send an ASCII code via matlab : fwrite or fprintf ?

How to send an ASCII code via matlab : fwrite or fprintf ? For example the code 'A30005". Do I need to add something like %s : fprintf(com1,'%s","A30005') Best regards Stanislas

 Accepted Answer

You an use either call. fprintf() like Thorsten shows, or
fwrite(com1, 'A30005')
If you need to send a terminator (CR or LF) then fprintf() is usually easier
fprintf(com1, '%s\n', 'A30005')

4 Comments

What does "terminator (CR or LF)" mean pleas ? and what does the "/n" after the %s (that I know already). Thanks for your quick response by the way.
CR is Carriage Return, the 13'th ASCII character.
LF is Linefeed, the 10'th ASCII character.
Different systems mark the ends of lines in different ways. Some want just CR, some want just LF, some want CR followed by LF. There is no "right" way; you just have to match what the hardware demands.
In formats, \n (backslash, not forward slash) stands for "line terminator". It will be converted in to whatever character or character pair has been configured for the Terminator property associated with the serial port, which is the same sequence that fgetl() or fscanf() would look for to mark the end of a line when it is input from the serial port.
Okay, I have from my communication settings, a terminator in LF, so linefeed. And the terminator associated seems to be in wikipedia U+000A. So I have to use fprintf with %s\n and "my text", and after I have to send "U+000A" ? Best regards Stan
If you use %s\n as your fprintf() format, then the U+000A will automatically be sent. The \n means to send the linefeed, which is U+000A

Sign in to comment.

More Answers (1)

Just make sure to get the marks right:
fprintf(com1, '%s', 'A30005')

Tags

Community Treasure Hunt

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

Start Hunting!