Using the TX and RX pins for RS232 communication

I am a bit new to this and trying to find out if my problem is in my code or hardware setup. I am trying to send a command like “q200” over the TX line from the Photon to a TTL to RS232 adapter which hooks up to my machine. My machine should then send back something like “23” or “VF2D”

On my machine I have the following parameters to set for configuring serial communication:
Baud Rate
Parity Select
Stop Bit
Synchronization
RS-232 Data Bits
Leader To Punch
EOB Pattern
Add Spaces on RS232 Out

I have read that the Photon uses 8-n-1 so I have been trying that but I am wondering if some of the other settings could be wrong? I can connect, send and receive commands using a USB to RS-232 adapter and CoolTerm (by setting the additional parameters).

I thought something simple like this should start to show some info:

void setup() {
Serial.begin(9600);
Serial1.begin(38400);
}
void loop() {
delay(5000);
Serial1.print(“q102”);
Serial.print(Serial1.read());
}

Any help is appreciated!

Does your serial-connected machine require an end-of-line character after the command “q102”? The print method only sends those four characters. The println method will add a carriage return and line feed. You could also use \r and/or \n in the string passed to print() to add CR or LF if you needed a different line termination.

Ah, I believe that is the EOB Pattern setting, it was set to CR LF. So I added in the println like you suggested but still am getting nothing but “-1” back.

The read() call is non-blocking and returns -1 if there is no data yet, which almost certainly is the case. You probably want something like:

	unsigned long start = millis();
	while(millis() - start < 5000) {
		int c = Serial1.read();
		if (c >= 0) {
			Serial.write(c);
		}
	}

That will print anything that comes back in next 5 seconds.

That worked! Thanks so much.

1 Like