2 years ago
#71461
ndasusers
Wondering how to send HEX buffer over HTTP with only text option to send
I am trying to send a Hexadecimal command to a TV RS232 through a Global Cache HTTP to RS232 device. Global cache can also operate device with RS232 over TCP/UDP port 4999.
Global Cache has a URL on the device that can take raw data and pass it on to the RS232 device or we can POST to the URL port 80 from file containing the HEX command but in the field, our sending device will need to create the HEX command without accessing a file and must use the HTTP POST rather than sending direct to port 4999.
Here is examples that work:
Command line:
echo -n -e "\x7F\x08\x99\xA2\xB3\xC4\x02\xFF\x01\x07\xCF" | nc 192.168.1.222 4999
Node JS TCP
hexString = "7F0899A2B3C402FF0107CF";
rawHex = Buffer.from(hexString, 'hex');
console.log(rawHex);
client.connect(4999, '192.168.1.222', () => {
console.log("Connected");
client.write(rawHex); //This will send the byte buffer over TCP
client.end();
})
Node JS HTTP
hexString = "7F0899A2B3C402FF0138CF";
rawHex = Buffer.from(hexString, 'hex');
console.log(rawHex);
client = net.createConnection({
host: '192.168.1.222',
port: 80,
path: '/api/v1/serialports/1/sendserial/'
}, () => {
console.log("Connected");
client.write(rawHex); //This will send the byte buffer over HTTP
client.end();
}
)
Insominia
First write the command line output to a file:
echo -n -e "\x7F\x08\x99\xA2\xB3\xC4\x02\xFF\x01\x07\xCF" > /tmp/hexcommand
Send that file as data with HTTP POST
> POST /api/v1/serialports/1/sendserial HTTP/1.1
> Host: 192.168.1.222
> User-Agent: insomnia/2021.7.2
> Content-Type: application/octet-stream
> Accept: */*
> Content-Length: 11
| �����8�
As mentioned, the device that will send this command strings cannot access a file and does not have node.js. So we need to get the POST data HEX buffer as a text data and send it with a HTTP POST.
FAILS:
Trying some javascript encoded HEX or other web url style hex encoder do not work. If I process the ASCII representation with some javascript examples and send the result, it is not affecting the device.
Example:
str = "7F0899A2B3C402FF0107CF";
function convertString(){
return(str.split("").reduce((hex,c)=>hex+=c.charCodeAt(0).toString(16).padStart(2,"0"),""))
}
Result of the function is more like web compatible hex. Notice the "text/plain" type in following POST example.
GlobalCache document specifies text/plain is required, so we are not sure why the application stream worked on the previous working example.
> POST /api/v1/serialports/1/sendserial HTTP/1.1
> Host: 192.168.1.222
> User-Agent: insomnia/2021.7.2
> Content-Type: text/plain
> Accept: */*
> Content-Length: 11
| 37463038393941324233433430324646303130374346
Also, sending the ASCII representation with the same POST method fails on all cases.
7F0899A2B3C402FF0107CF
7f0899a2b3c402ff0107cf
7F 08 99 A2 B3 C4 02 FF 01 07 CF
\x7F\x08\x99\xA2\xB3\xC4\x02\xFF\x01\x07\xCF
None of above are equivalent to the working examples that produce the usable HEX output.
Thanks for any advice how to change that ASCII text into a string that may go working across the HTTP Post
javascript
node.js
post
hex
0 Answers
Your Answer