We help IT Professionals succeed at work.
Get Started

How to send HEX commands in C/C++ over TCP/IP to control a relays board

392 Views
1 Endorsement
Last Modified: 2019-09-09
I have an electronics board which has 16 relays and it works over TCP/IP.
The IP address of the board is 192.168.1.4 and the port is 3000.
I would like to control it with C/C++ under Ubuntu.

There is a list of HEX commands that it is possible to use in order to remotely switch ON and OFF each relay on the board.
This is the list:
"580112000000016C",  // switch on the relay 1

"580111000000016B",  // switch off the relay 1
"580112000000026D", // switch on the relay 2
"580111000000026C", // switch off the relay 2
"580112000000036E", // so on..
"580111000000036D",
"580112000000046F",
"580111000000046E",
"5801120000000570",
"580111000000056F",
"5801120000000671",
"5801110000000670",
"5801120000000772",
"5801110000000771",
"5801120000000873",
"5801110000000872",
"5801120000000974",
"5801110000000973",
"5801120000000A75",
"5801110000000A74",
"5801120000000B76",
"5801110000000B75",
"5801120000000C77",
"5801110000000C76",
"5801120000000D78",
"5801110000000D77",
"5801120000000E79",
"5801110000000E78",
"5801120000000F7A",
"5801110000000F79",
"580112000000107B",
"580111000000107A",
"5801130000FFFF77",
"580113000000007B",
"5801100000000069"

I'm correctly able to switch on and off each relay by sending command line commands under Ubuntu:

echo '580112000000016C' | xxd -r -p | nc 192.168.1.4 3000

Open in new window

The above code correctly turns on the relay.

I would like to do the same with C/C++ code since I want to control the board from a WxWidgets application.
For the moment, I'm starting from base and I'm just using C/C++ code in order to test the tcp connection.

This is my code:

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>
#include <string>
#include <unistd.h>

using namespace std;

int main(int argc, char**argv) {
    int sockfd, n;
    struct sockaddr_in servaddr;
    char sendline[1000];
    char recvline[1000];

    std::string serveraddr = "192.168.1.4";

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(serveraddr.c_str());
    servaddr.sin_port = htons(3000);

    connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

    int number = 0x580112000000016C;

    int sendsize;
    sendsize = snprintf(sendline, sizeof(sendline), "%x", number);

    send(sockfd, sendline, sendsize * sizeof(char), 0);

}

Open in new window


How should I send the HEX commands?
When I compile this code I get this warning:

g++ tcp.c -o tcp
tcp.c: In function ‘int main(int, char**)’:
tcp.c:29:18: warning: overflow in implicit constant conversion [-Woverflow]
     int number = 0x580112000000016C;
                  ^~~~~~~~~~~~~~~~~~

and obviously the program doesn't work.

Can you help me please? I'm a beginner and I'm not understanding how to correctly send the HEX instructions.
Comment
Watch Question
Software Developer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 23 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE