amiga.org
     
iconAll times are GMT -6. The time now is 10:11 AM. | Welcome to Forum, please register to access all of our features.

» Amiga.org » Coffee House » CH / Science and Technology » C++ - sending hex bytes to a socket

CH / Science and Technology This is the correct place for open, moderated discussions relating to science and technology. Open to Registered users only.

Reply
 
Thread Tools Display Modes
Old 06-01-2008, 12:30 PM   #1
motorollin
Premium Member
Points: 32,860, Level: 100 Points: 32,860, Level: 100 Points: 32,860, Level: 100
Activity: 54% Activity: 54% Activity: 54%
 
motorollin's Avatar
 
Join Date: Nov 2005
Posts: 8,666
Default C++ - sending hex bytes to a socket

I have decided to rewrite my RFXCOM (home automation) controller in C++, as the virtual serial ports redirected to TCP/IP turned out to be a bit unstable.

I have used a Berkeley Sockets example to get a test app connected to the RFXCOM, and it works. I now want to try sending a command to it to turn something on or off. The device takes its commands in hex bytes, but I have no idea how to create an array of hex digits to pass to send().

In Applescript I converted the hex digits to decimal, then created a string of ASCII characters from the binary numbers (e.g. 0x21 = decimal 33 = ASCII char "!"). I then sent this string of ASCII to the device. Can this be done in C++ if I can't send the hex digits directly?

--
moto
__________________
Code:
10  IT'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
motorollin is offline   Reply With Quote
Old 06-02-2008, 01:01 AM   #2
motorollin
Premium Member
Points: 32,860, Level: 100 Points: 32,860, Level: 100 Points: 32,860, Level: 100
Activity: 54% Activity: 54% Activity: 54%
 
motorollin's Avatar
 
Join Date: Nov 2005
Posts: 8,666
Default Re: C++ - sending hex bytes to a socket

Sorted :-) I wanted to see what was actually being sent, so I found an example of an IP server which receives data from a client and prints it to cout. I connected my RFXCOM test app to it and noticed that the data being sent was correct, but there were a few extra bytes at the end. So I changed my send() routine to send a specific number of bytes rather than trying to calculate the size of the buffer. When I ran it, I got exactly the data I wanted coming in to the server. Here is the code which worked:

//Send data to RFXCOM
buf[0] = 0x21;
buf[1] = 0x00;
buf[2] = 0xBB;
buf[3] = 0x80;
buf[4] = 0x9B;
buf[5] = 0x00;

if (send(socketDescriptor, buf, 6, 0) < 0) {
printf("cannot send data ");
close(socketDescriptor);
exit(1);
}



The data in the buffer in that example should turn my kitchen light on. When I sent it to the RFXCOM, it worked - once. It has never worked again. This is very annoying :roll:

--
moto
__________________
Code:
10  IT'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
motorollin is offline   Reply With Quote
Old 06-02-2008, 01:31 AM   #3
motorollin
Premium Member
Points: 32,860, Level: 100 Points: 32,860, Level: 100 Points: 32,860, Level: 100
Activity: 54% Activity: 54% Activity: 54%
 
motorollin's Avatar
 
Join Date: Nov 2005
Posts: 8,666
Default Re: C++ - sending hex bytes to a socket

Ok, I think I might have found the problem with sending the data. If I send a string of hex with a 0x00 as one of the bytes, it stops receiving the rest. Well, when I say it stops receiving it, I can't see it. Here's an example. The client sends this buffer:

buf[0] = 0x21;
buf[1] = 0x00;
buf[2] = 0x21;
buf[3] = 0x21;
buf[4] = 0x21;
buf[5] = 0x21;

What I expect to see at the server end is "! !!!!" (that's one exclamation mark followed by a space followed by four exclamation marks). However, the server just prints one exclamation mark. I don't know whether that's because it stops reading after the 0x00, or just doesn't print anything after it. Here's the code which receives the data:

memset(line, 0x00, LINE_ARRAY_SIZE);
while (recv(connectSocket, line, MAX_MSG, 0) > 0) {
cout << " -- " << line << "\n";

// Convert line to upper case.
for (i = 0; line[i] != '\0'; i++)
line[i] = toupper(line[i]);

// Send converted line back to client.
if (send(connectSocket, line, strlen(line) + 1, 0) < 0)
cerr << "Error: cannot send modified data";

memset(line, 0x00, LINE_ARRAY_SIZE); // set line to all zeroes
}

I thought it might be something to do with the for loop, which seems to stop if the character it's checking is zero, but this occurs after the character is sent to cout.

Edit -

In fact I have now changed the receive routine to this:

memset(line, 0x00, LINE_ARRAY_SIZE);
while (recv(connectSocket, line, MAX_MSG, 0) > 0) {
cout << " -- " << line << "\n";
memset(line, 0x00, LINE_ARRAY_SIZE); // set line to all zeroes
}

I really can't see what is stopping the characters after 0x00 being printed, so I can only assume they're not being sent, or are being discarded by the server. Any help really, really appreciated guys! :-)

--
moto
__________________
Code:
10  IT'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
motorollin is offline   Reply With Quote
Old 06-02-2008, 04:10 AM   #4
motorollin
Premium Member
Points: 32,860, Level: 100 Points: 32,860, Level: 100 Points: 32,860, Level: 100
Activity: 54% Activity: 54% Activity: 54%
 
motorollin's Avatar
 
Join Date: Nov 2005
Posts: 8,666
Default Re: C++ - sending hex bytes to a socket

Right, sorted :-) Someone over at CodeGuru told me that 0x00 is being interpreted as a null terminator, so it stopped printing. I confirmed this by converting each byte to a hex code and printing that, and I got 0x21, 0x00, 0x21 etc.

--
moto
__________________
Code:
10  IT'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
motorollin is offline   Reply With Quote
Reply

Bookmarks

Tags
bytes , sending , hex , socket

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending an A3000D to Europe Amiduffer Amiga Hardware Issues and discussion 9 04-15-2009 12:12 PM
Sending packages >2kg abroad from the UK motorollin General chat about Amiga topics 11 12-04-2008 09:28 AM
Problem SENDING email with YAM (2.0) dvvrue95 Amiga Software Issues and Discussion 4 11-16-2005 10:28 AM
Sending accel's abroad Vincent Amiga Marketplace 4 07-13-2004 09:35 AM
Retrieving FileSize in bytes Jettah Amiga OS -- Development 11 02-03-2004 01:08 PM