2

I have a device connected in my raspi3

pi@raspberrypi:/home $ sudo bash main.sh
%s\t%s\n 0 Bus 001 Device 004: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
%s\t%s\n 1 Bus 001 Device 005: ID 1a2c:0e24 China Resource Semico Co., Ltd
%s\t%s\n 2 Bus 001 Device 006: ID 0424:7800 Standard Microsystems Corp.
%s\t%s\n 3 Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
%s\t%s\n 4 Bus 001 Device 002: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
%s\t%s\n 5 Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Target device:

%s\t%s\n 1 Bus 001 Device 005: ID 1a2c:0e24 China Resource Semico Co., Ltd
pi@raspberrypi:/home $

This is the script

#!/bin/bash

usbArray=()
while IFS= read -r line; do
    usbArray+=( "$line" )
done < <( lsusb )


for i in "${!usbArray[@]}"; do 

  echo "%s\t%s\n" "$i" "${usbArray[$i]}"

done

echo ""
echo "Target device:"
echo ""

for i in "${!usbArray[@]}"; do 

  if [[ ${usbArray[$i]} == *"China Resource Semico"* ]]; then
    echo "%s\t%s\n" "$i" "${usbArray[$i]}"
  fi

done

From the device protocol I see:

1.read master version 
sent: 5A 00 00 0d 0a 71
reply: A5 00+ "MASTER-FW:v1.0\r\n" + CS

So I have to send 5A 00 00 0d 0a 71 as data, not string, and I will receive a hex data response, I have this done in windows using cport library, but I don't know how to do this in debian(raspi3)

any idea?

  • bash is the wrong tool to communicate with serial ports. But it has no problems with sending hexstrings: printf '\x5A\x00\x00\x0d\x0a\x71' will do that. – Uncle Billy May 29 at 15:26
  • I don't get the idea how to implement that print – Martin Ocando Corleone May 29 at 15:33
  • printf is a Bash builtin and a standalone executable from the coreutils package as well. You needn't implement it. – Ferenc Wágner May 29 at 16:30
  • You're looking for something that's approximately the reverse of od -x, then? – Toby Speight May 29 at 16:45
0

We can convert the usbArray to a suitable escaped string using printf:

printf ' \\x%s' "${usbArray[@]}"

This produces \x5A\x00\x00\x0d\x0a\x71.

Then pass that as argument to another printf, to interpret those as escape codes:

printf '%b\n' "$(printf '\\x%s' "${usbArray[@]}")"

To demonstrate that we have the correct output, inspect it using od:

$ printf '%b\n' "$(printf '\\x%s' "${usbArray[@]}")" | od -t x1
0000000 5a 00 00 0d 0a 71 0a
0000007

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.