Follow

FAQ: How do I send hex data via HTTP?

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.

Example_HexData_JS_browser.gif

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.

 

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

1 Comments

  • 0
    Avatar
    Dinesh Pannu

    C#:

    private readonly byte[] STX = new byte[] { 0x02 };
    private readonly byte[] RTX = new byte[] { 0x03 };

    using (var httpClient = new HttpClient()
    {
    BaseAddress = new Uri(config.FlexAddress)
    })
    {
    var serialCommand = "PON";
      var payload = $"{STX}{serialCommand}{RTX}";

      using (var content = new StringContent(payload, Encoding.Default, "text/plain; charset=UTF-8"))
      {
      using (var response = httpClient.PostAsync($"api/host/modules/{config.ModuleNumber}/ports/{config.ComPortNumber}/data", content))
          {
          response.Wait();

              var dataTask = response.Result.Content.ReadAsStringAsync();
              dataTask.Wait();

              string responseData = dataTask.Result;
          }
      }
    }

     

Please sign in to leave a comment.
Powered by Zendesk