Reputation: 75
I try to send some hex values (mixed with asci chars).
what i like to have: send_string = {0x2d, 0x2e, 0x01,..., "Hallo"}
what i am able to get: send_string= "0x2d0x2e0x10...Hallo"
#!/bin/bash
#USB device path
USBDEV=/dev/ttyUSB0
#disable DTR
stty -F $USBDEV -hupcl
#set baud
stty -F $USBDEV 9600
pattern1=0x2d
pattern2=0x2e
adr=0x01
cmd=0x21
nl=0x0a
end=0x00
txt1="Hallo"
txt2="Bye"
string=$(echo ${pattern1}${pattern2}${adr}${cmd}"test"${nl}"haha"${end})
#string=$(echo \0x2e\0x2d\0x01\0x21"test"\0x0a"haha"\0x00)
echo $string
echo $string >> $USBDEV
exit 0
0x2d0x2e0x010x21test0x0ahaha0x00
The hex values of vars pattern1
, pattern2
, adr
, ... should be stored in the string as not ASCI charackters.
If the string would be prepered as i need to, the output must have non asci characters. But it has not.
i tried some ways but all gained confusion only.
Upvotes: 0
Views: 827
Reputation: 532448
I recommend using printf
directly for binary data; you can't store arbitrary binary data (namely, a null byte) in a shell variable.
#!/bin/bash
#USB device path
USBDEV=/dev/ttyUSB0
#disable DTR
stty -F $USBDEV -hupcl
#set baud
stty -F $USBDEV 9600
printf '.-\001!%s\n%s\000' "$txt1" "$txt2" > "$USBDEV"
exit 0
Upvotes: 0
Reputation: 242383
You can use the $''
special quotes.
pattern1=$'\x2d'
This doesn't work for \x00
, though. The only possible way to print it I know is
printf '\x00'
but you can't assign it to a variable. You need to switch to a more advanced scripting language to be able to do it. For example, in Perl
$end = "\x00";
works fine.
Upvotes: 1