Serial Communication In Arduino (with unprintable characters).

I’m trying to communicate with a digital device that needs a particular command before it responds.

This is the command I am supposed to deliver it:
<22><2>N0C0 G A<3><13><10>.

I was curious how to print this to a serial port I’ve made in Arduino script.

I have it working in MATLAB with the following script, but haven’t gotten it to work in Arduino.

s = serial(‘COM4’,‘BaudRate’,9600);
fopen(s)
set(s,‘Terminator’,{3,’’})
fprintf(s,’%s’,[char(22) char(2) ‘N0C0 G A’ char(3) char(10) char(13)]);
data_char = fgets(s);

Any help is appreciated!

This is the command I am supposed to deliver it:
<22><2>N0C0 G A<3><13><10>.

That is NOT what

fprintf(s,'%s',[char(22) char(2) 'N0C0 G A' char(3) char(10) char(13)]);

does.

   Serial.write(22);
   Serial.write(2);
   Serial.print("N0C0 G A");
   Serial.write(3);
   Serial.write(10);
   Serial.write(13);

Thank you for the quick response.

I tried the code you provided and I don't seem to be getting a response from the digital device. The MATLAB code does trigger the device to respond though so I am confused as to what serial command it the digital device needs.

In MATLAB I had to correct the terminator byte. Do you have to do this in Arduino?

abartoli: In MATLAB I had to correct the terminator byte. Do you have to do this in Arduino?

How did you need to correct it?

...R

I had to use the following line of code:

set(s,'Terminator',{3,''})

Not sure if this makes a big deal or not. Wasn't sure if there was an equivalent in Arduino.

I am using Matlab to verify that I can communicate back and forth with the Arduino in Serial mode ... it just appears that something is wrong with the message I am sending the digital device.

Thanks!

You say you need to send <13><10> but then the matlab code you posted sends <10><13>

Maybe just reversing the two last Serial.write() statements in the proposed Arduino code fixes the issue.

If you needed to set the terminator character to " , you should send that terminator and the end of transmission.

So add at the end of every transmission:

Serial.write(34);

34 is the ascii code for "

BTW, <10><13> is a carriage return followed by a line feed. (old teletype stuff from days gone by).

Your device may be fussy about the sequence, whether CR/LF or LF/CR.
Some devices don’t care.