Sending Hex Data during a Serial Connection

 

PROBLEM DESCRIPTION or QUESTION


How can I send Hex Data during a Serial Connection? Can Xshell string transmissions be sent in a hexidecimal format?

 

RESOLUTION


Terminal communication, including serial communication, is the exchange of strings. However, this particular issue is related to whether or not the string is a value that can be entered with the keyboard.

Values ​​other than character strings present on the keyboard can be transmitted using the ASCII code number of the corresponding character. Sending via a code number is possible through the Compose Pane, Compose Bar, or via Quick Commands.

For example, the letter 'A', ASCII code number 65, can be transmitted as follows.

Send octal value: \0101
Send hexadecimal value: \x41

The following is an example of how to send text using an ASCII code number through the Compose Bar.

  1. Select ‘View → Compose → Compose Bar’ from the menu to open the Compose Bar.

  2. To send the value “F2 24 1E 00 00 00 00 AE FD 03”, we can enter the following in the Compose Bar
    \xF2\x24\x1E\x00\x00\x00\x00\xAE\xFD\x03

 

To enter values ​​directly into the terminal (instead of using the Compose Pane or Bar, you must use a Quick Command button or write a script.

Below is an example of using a JS script that takes a hexadecimal string and sends the value to the terminal when a Quick Command button is pressed.

  1. Select ‘View → Quick Commands → Quick Command Bar (or Pane/Manager)’ from the menu.

  2. Open a text editor, paste the following contents, and save it as a file with the .js extension.

    function Main() { var string = xsh.Dialog.Prompt ("Insert Hex string. Including white spaces will be OK.", "HEX sender", "", 0); if(!verify(string)){ xsh.Dialog.msgbox("Error in HEX string"); return; } chars = string.split(/[\s]{1,}/); hexStr = ""; for(var idx in chars){ thisChar = chars[idx]; for(var i=0; i<thisChar.length; i+=2){ hexStr += String.fromCharCode(Number("0x"+thisChar.substr(i, 2))); } } xsh.screen.send(hexStr); } verify = function(string){ return !(string.replace(/[^0-9A-Fa-f]/g, "").length%2) && string.search(/[^0-9A-Fa-f\s]/) == -1; }
  3. Add a button from the Quick Command Bar.

  4. In the Edit dialog box, select Run Script and select the script file you created.

    image-20240618-043641.png
  5. Launch the command button from the Quick Command Bar.

  6. Verify that the corresponding characters are displayed in the terminal.

 

For reference, Quick Command buttons can be set as a shortcut key through ''.