Using Serial (or SoftwareSerial) to Communicate with another device

I'm a bit confused on how to send a serial command from my arduino to a third device, then have it read out the response. I have the arduino RX pin connected to the device's TX pin and the arduino TX pin connected to the device's RX pin. Assuming that I have initialized the serial port and set the baud rate, I'm looking for how to send the command and then listen back for the answer and send the answer back to my computer.

(i) For sending the serial command (RVAL?) from the arduino to the device, would I simply use

Serial.println(RVAL?)

or does Serial.write(RVAL?) send it in the same way?

(ii) Then, to obtain the response and send it to my computer do I use something along the lines of:

if (Serial.available() > 0) {
responseVariable = Serial.read();
Serial.print(responseVariable);
}

?


I'm unclear about where Serial.println() or Serial.write() sends the command, and then also once I use Serial.read() to get the data from the RX port, how I send that back to my computer?

Google has soooo many hits on this topic.

If you have a Mega, you have several Serial ports.

There is also 'software serial' available when you need two serial ports.

The Arduino reference section will explain the use of println, write, read, etc.

println calls write :wink: write allows you to send binary data while print/println will always send text.

println adds a carriage return and a line feed character; if you don't that, use print instead of println.

If your communication is text based, use print/println. You will need to put RVAL? between double quotes to keep the compiler happy.

Your approach needs some refinements.

Set a flag to indicate that you're waiting for a reply.

if flag is not set, send command and set flag
else wait for complete reply and once complete reply is received, clear flag

Read Robin's Updated serial input basics thread to get ideas for the receive side of things

njswimm:
I'm unclear about where Serial.println() or Serial.write() sends the command, and then also once I use Serial.read() to get the data from the RX port, how I send that back to my computer?

1. Serial Monitor, Computer Monitor, LCD device, and the like are human-friendly display devices. They are technically called 'ASCII type' devices. To visualize the character A (for example) of the English Language on these devices, we need to send to them the ASCII code of A which is 01000001. We may execute one of the following codes (except the last one) to see A on an ASCII device:

Serial.print('A');  //shows: A
Serial.write(0x41);          //shows: A
Serial.print((char)0x41);  //shows: A; we are forcing to show A
 not this one Serial.print(0x41);  //shows: 65 ; Serial.print(0x41); ---> Serial.print(0x41, DEC);

Rememeber that it is the Serial.write(); which actually puts data (binary pattern) on the UART Port. So, Serial.print(0x41, DEC); is converted into two consecutive instructions: Serial.write(0x36); and Serial.write(0x35); in order to show 65 (it is the decimal value of 0x41 = 4x16 + 1x1) on the Serial Monitor.

2. If we view A as a digit of the hexadecimal symbols set (0 - 9, A - F), then we uses this bit pattern 00001010 (0x0A) for its representation. Now to see A on the Serial Monitor, we have to convert it into the ASCII code of A (which is 0x41) by adding 0x37 (byte x = 0x0A + 0x37 = 0x41). Next, let us execute this instruction: Serial.write(x); to present A on the Serial Monitor.

Remember that Serial.write(); puts binary value on the UART Port that goes to the ASCII devices. If the said binary value coincides with an ASCII code, then the corresponding character/digit will appear on the ASCII Monitor; else, artifacts will appear.

As mentioned in Reply #2 have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

If you are using an Uno or Nano then use SoftwareSerial to create a separate serial port on two other pins (not Pins 0 and 1) so that Serial is kept free for messages to the PC.

…R