For RS232 devices that support our RESTful HTTP API we often get asked how to send Hexadecimal serial commands. While the exact method of sending hexadecimal via HTTP is often dependent on the language or program used to send the HTTP commands here are a few examples of sending hexadecimal commands with common languages or programs.
Postman
A collection containing the postman example commands can be found here:
https://www.getpostman.com/collections/5d4889db70befcec07cf
Import the collection by going to File->Import and pasting the above link as shown:
Change the body variable under the "Pre-request Script" to the desired hex value. Hex values should be preceded with the "\x" escape sequence as shown below.
Click Send to test the command.
Javascript
The following javascript snippet can be run through the console of your browser. Navigate to the Global Cache unit's webpage (this prevents CORS issues with the request), open the browser's inspector (in Firefox this can be done with CTRL+SHIFT+I) and paste the following in the console:
window.ip = "192.168.0.172";
window.hex_data = "000102";
window.data = new Uint8Array(hex_data.length / 2);
for (var i = 0; i < data.length; i++) data[i] = parseInt(hex_data.substr(i *2,2),16);
//Flex 21 or earlier
window.url = "http://" + ip + "/api/host/modules/1/ports/1/data";
//Global Connect of Flex fw 22 or later
//window.url = "http://" + ip + "/api/host/modules/1/serial/ports/1/data";
window.post = function(url, data) {
return fetch(url, {method: "POST", headers: {'Content-Type': 'text/plain; charset=UTF-8'}, body: data});
}
post(url, data);
Click Run to execute the script.
Edit 09/09/22: The above script has been modified to use a integer array which avoids javascript encoding issues with certain hex values.
Note: Modify the window.ip variable to match your unit's IP address and the window.hexdata variable to match the hex data that needs to be sent.
Python
The following python code can be run with Python3.
pip install requests
The code requires the requests module which can be installed via pip.
import requests
unit_ip = '192.168.0.172'
hex_data = '\x00\x01\x02'
# Flex firmware 21 or earlier
url = 'http://' + unit_ip + '/api/host/modules/1/ports/1/data'
# Global Connect or Flex firmware 22 or later
#url = 'http://' + unit_ip + '/api/host/modules/1/serial/ports/1/data'
#Execute request
requests.post(url,data=hex_data,headers={'Content-Type': 'text/plain; charset=UTF-8'})
Note: The script is also attached as a .py file [here].
Modify the "unit_ip" and "hex_data" variables with the correct IP address and hex data you want to send to your unit.
1 Comments