Automate an RS232 Relay Board: Exact Bytes, Timeouts and Safe Reconnection
A desktop script that sends a relay command successfully once is a useful start. A dependable controller must also cope with a missing reply, an unplugged USB adaptor and a computer restart without unexpectedly repeating an action.
This guide explains how to build that behaviour around a documented RS232 relay board. The emphasis is on the software transaction and recovery process, using harmless low-voltage test loads while the interface is commissioned.
Collect the board's actual command specification
The two-channel RS232 relay board in PMD Way's RS232 relay board collection is listed for a 12 V DC supply through a 2.1 mm DC socket, with instructions supplied. Confirm input polarity from those instructions or the board marking; socket diameter does not establish polarity.
The public listing does not specify command bytes, serial framing or reply format. Obtain those details before writing a control script. A generic terminal program's default settings and commands from another relay board are not substitutes for the correct protocol.
Record baud rate, data bits, parity, stop bits, flow control, command format and any inter-command interval. Also establish whether the protocol supports explicit on/off requests, toggles, timed pulses or status queries. These differences determine what recovery is possible.
Verify the electrical link and cable
Use an RS232 interface at the board's RS232 connection. A USB-to-TTL UART adaptor does not produce the same electrical signalling. Texas Instruments' RS232 interface design notes explain the standard's driver and receiver characteristics and signal conventions.
Check the board and adaptor pinouts to determine the cable wiring. Do not choose straight-through or crossed wiring solely from connector gender. Confirm the required transmit, receive and signal-reference connections, plus any handshaking specified by the board.
Keep the relay contact circuits disconnected during these tests. The control-board supply and the loads switched by its contacts are separate parts of the project. A serial cable should not be treated as a power source unless the exact equipment explicitly documents that arrangement.
Capture one known working transaction
Use a terminal or test utility that can send the required representation. An ASCII command containing the characters “01” consists of two text bytes. A binary value written as hexadecimal 01 is one byte. Confusing the two can produce silence even when the baud rate and cable are correct.
Record the outgoing bytes in hexadecimal, the expected response and the observed time to completion. Include terminators such as carriage return or line feed only when the protocol calls for them. Do not add an Enter key automatically to a binary packet.
Verify the result at the intended contact pair with external load power removed. Keep the terminal settings and byte capture as a reference. The first automated test should reproduce this exact transaction before introducing loops, graphical controls or reconnect logic.
Identify the intended port explicitly
A USB adaptor's port name can change after reconnection or when other adaptors are installed. Selecting the first available port risks sending relay commands to unrelated equipment.
The pySerial port-listing tools can report device names and, where the platform and device expose them, USB identifiers, serial numbers and locations. A useful discovery command is:
python -m serial.tools.list_ports -v
Build selection around the available identity information and require an unambiguous match. If two indistinguishable adaptors are present, use a deliberate operator selection or a documented physical-port arrangement. Port enumeration identifies an interface; it does not prove that the expected relay board is attached at its far end.
Model a command as a transaction
Separate the stages: validate the requested action, construct the documented bytes, transmit them, collect the response, validate it and report the outcome. Keep only one transaction in progress unless the board's protocol explicitly supports another pattern.
The pySerial API documentation distinguishes read timeouts from write timeouts. A read with a timeout can return fewer bytes than requested, so a short reply must not be accepted as a complete packet. Likewise, a completed write reports host-side transmission progress, not proof that the relay contacts moved.
Use the documented packet length, delimiter or framing rules to determine completion. Validate checksums and response fields when provided. Avoid using one arbitrary delay followed by “read whatever is there”, which can mix a late response with the next command.
Define what a timeout means
If the reply is missing, the command may have failed to arrive, or it may have executed while the reply was lost. Those possibilities are different from a confirmed rejection. Represent the result as uncertain until the protocol or external observation resolves it.
| Command type | Retry concern |
|---|---|
| Explicitly set channel on | Repeating may be acceptable if the documented operation has no additional side effects |
| Toggle channel | Repeating can reverse a successful first action |
| Produce a timed pulse | Repeating can produce an unintended second pulse |
| Read status | A fresh documented status query may help resolve uncertainty |
Do not implement unlimited automatic retries. Define a maximum attempt count and an operator-visible fault state. If the board has no trustworthy status query, the software should admit that it cannot infer physical contact state from a missing reply.
Reconnect without replaying stale intent
When the port disappears, stop issuing commands and mark pending actions unresolved. On reconnection, confirm the adaptor identity and board communication before enabling controls. Discard old queued requests unless the application has an explicit reason and mechanism to revalidate them.
For example, a user may press “pulse channel 1” just as the cable is removed. Replaying that request several minutes later when the cable returns is usually surprising. Require a fresh request for such time-sensitive actions instead of treating a recovered connection as permission to empty the queue.
Record whether closing or opening the port changes any control lines relevant to the equipment. Test startup and shutdown using the intended adaptor and driver. A clean software exit alone is not a guarantee that relay outputs return to an off state.
Test before attaching the final load
- Verify each channel independently and confirm that another channel remains unchanged.
- Send an invalid request through the application's validation path and confirm that no serial command is emitted.
- Disconnect the cable during a transaction and inspect the reported uncertainty.
- Reconnect and confirm that no old pulse or toggle is replayed automatically.
- Restart the computer and relay board separately, recording the initial contact states.
Then qualify the actual load, terminals and contact ratings as a separate step. Omron's relay precautions explain why load conditions affect contact performance. Use suitable protection and an enclosure appropriate to the complete project.
Save the protocol sheet, known working byte capture and recovery-test results with the script. They provide a clear way to distinguish a changed serial interface from a software or relay fault.