Linux users can easily control NCD Ethernet and WiFi relay boards using Netcat. Netcat provides a convenient method for Linux users to send and receive TCP data over a network port, ideal for sensor monitoring and relay control applications. Netcat is available for free download, users may read more about it here. This tutorial will demonstrate the steps necessary to use Netcat for relay control applications.
Step 1: Finding the IP Address
First you need to know the controller’s IP address. The controller sends out a UDP broadcast on port 13000. You can listen for that broadcast through netcat by entering:
nc -u -l 13000
Once you see the UDP broadcast, you can see the IP address of the controller and it’s mac address. In our sample, we will use 192.168.1.42 and port 2101 as our default port for communications to the ethernet relay board. Note that your IP address may be different, but the port (2101) is likely to be the same.
Step 2: Sending Relay Control Commands
If you have seen our other tutorials, you have likely seen the tables we use are in decimal format, as shown below:
TX: | Function: (Decimal Version) | RX: | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
170 | 3 | 254 | 108 | 1 | 24 | Turn On Relay 1 in Bank 1 | 170 | 1 | 85 | 0 |
170 | 3 | 254 | 109 | 1 | 25 | Turn On Relay 2 in Bank 1 | 170 | 1 | 85 | 0 |
170 | 3 | 254 | 110 | 1 | 26 | Turn On Relay 3 in Bank 1 | 170 | 1 | 85 | 0 |
170 | 3 | 254 | 111 | 1 | 27 | Turn On Relay 4 in Bank 1 | 170 | 1 | 85 | 0 |
170 | 3 | 254 | 100 | 1 | 16 | Turn Off Relay 1 in Bank 1 | 170 | 1 | 85 | 0 |
170 | 3 | 254 | 101 | 1 | 17 | Turn Off Relay 2 in Bank 1 | 170 | 1 | 85 | 0 |
170 | 3 | 254 | 102 | 1 | 18 | Turn Off Relay 3 in Bank 1 | 170 | 1 | 85 | 0 |
170 | 3 | 254 | 103 | 1 | 19 | Turn Off Relay 4 in Bank 1 | 170 | 1 | 85 | 0 |
Netcat prefers working with Hex values, so we need to convert the commands from decimal to hex. Below is a Hex version of the same table:
TX: | Function: (Hex Version) | RX: | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
AA | 03 | FE | 6C | 01 | 18 | Turn On Relay 1 in Bank 1 | AA | 01 | 55 | 00 |
AA | 03 | FE | 6D | 01 | 19 | Turn On Relay 2 in Bank 1 | AA | 01 | 55 | 00 |
AA | 03 | FE | 6E | 01 | 1A | Turn On Relay 3 in Bank 1 | AA | 01 | 55 | 00 |
AA | 03 | FE | 6F | 01 | 1B | Turn On Relay 4 in Bank 1 | AA | 01 | 55 | 00 |
AA | 03 | FE | 64 | 01 | 10 | Turn Off Relay 1 in Bank 1 | AA | 01 | 55 | 00 |
AA | 03 | FE | 65 | 01 | 11 | Turn Off Relay 2 in Bank 1 | AA | 01 | 55 | 00 |
AA | 03 | FE | 66 | 01 | 12 | Turn Off Relay 3 in Bank 1 | AA | 01 | 55 | 00 |
AA | 03 | FE | 67 | 01 | 13 | Turn Off Relay 4 in Bank 1 | AA | 01 | 55 | 00 |
The commands found in the table above are now converted to Netcat commands:
echo -e ‘\xAA\x03\xFE\x6C\x01\x18’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x6D\x01\x19’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x6E\x01\x1A’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x6F\x01\x1B’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x64\x01\x10’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x65\x01\x11’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x66\x01\x12’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x67\x01\x13’ | nc 192.168.1.42 2101
If you have problems try changing `-e` to `-en`, netcat differs slightly on different systems.
Hello! Thank you for this. I was able to perform this and no errors occurred (in terms of verbose) but the relay did not actually turn on. I can turn them on and off no problem with VB.net, but from command line (Linux) nothing occurs.
Is there specific values to be set on the Lantronix device Channel 1 connection settings?
No, the default settings should work with this as long as the IP address matches and the port number hasn’t been changed.
Can you post the command that’s not working and what board you’re using it with?
I am working with tapdevcj, and I believe we are working with the Relay Controller 2-Channel General Purpose SPDT + 8 Channel ADC ProXR Lite with a Lantronix Ethernet module.
We used the commands from above except for changing the IP, and trying various forms of quotes, as the ones above seemed to have been corrupted by the font.
echo -e ‘xAAx03xFEx6Cx01x18’ | nc 192.168.37.56 2101
echo -e “xAAx03xFEx6Cx01x18” | nc 192.168.37.56 2101
echo -e ‘xAAx03xFEx6Cx01x18’ | nc 192.168.1.42 2101
Try this:
echo -e ‘\xAA\x03\xFE\x6C\x01\x18’ | nc 192.168.37.56 2101
it is just normal single quotes around it
I added a little easter egg to the footer, click on the asterisk at the bottom left of the footer, put the controller’s IP into the first pop up, and copy the decimal version of the command (starts with 170) into the second, and it will generate the netcat command for you
Still no success
we have activated it successfully with VB, so we know we are using the correct IP and the relays works, but this case we need NC to work. Using ubuntu to send the command.
echo -e ‘\xAA\x03\xFE\x6C\x01\x18’ | nc 192.168.37.56 2101
Try `echo -en`
The -en made the difference. Thank you.