Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to make a fairly simple bash script to pull bytes one at a time from a binary file, send it out a serial port, and then wait for a byte to come back before I send the next one. This is effectively for an EEPROM programmer, where an off the shelf solution won't work.

I'm mostly stuck at pulling the bytes out of the file into a variable, beyond that I know I can echo the values down the serial port and read them back with dd. (Is there a better way to do that also?)

Thanks for the help!

share|improve this question
    
will the return byte definitely always arrive singly? As in, will you receive only one byte per byte sent? –  mikeserv Sep 11 '14 at 17:31
    
You can work with hex values, like using the command xxd -ps and xxd -r -ps. It would be safer. –  LatinSuD Sep 11 '14 at 22:01
    
IIRC bash can't cope with null bytes, so it's unsuitable to deal with binary data. Most shells aren't designed to deal with binary data (zsh is an exception). Is this an embedded system? If so how restricted is your tool set? –  Gilles Sep 12 '14 at 15:00
    
Yeah, the device on the far end should relay the same byte that is sent out, after it has gone through a bit of processing on the device end. –  Minifig666 Sep 12 '14 at 21:37
    
This script is just going on a lubuntu laptop, so I have no real restrictions on the tools I can use. It would be nice to send straight binary values down the serial port, but I could potentially send two ASCII hex characters per byte, and then edit the code on the device end to cope. –  Minifig666 Sep 12 '14 at 21:41

2 Answers 2

I’m not sure I understand the big picture of what you’re asking for, but I have a couple of approaches that you might want to consider:

Use dd to read your input file

exec 3 < input_file
while true
do
    dd bs=1 count=1 <&3 > this_byte
    if ! [ -s this_byte ]       # If size is zero, we’re done.
    then
        break
    fi
    code using the this_byte file
          ︙
done

Use od to digest your input file

od -bv input_file | while read a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16
do                              # Skip $a0; it’s the address.
    for byte_val in "$a1" "$a2" "$a3" "$a4" "$a5" "$a6" "$a7" "$a8" "$a9" "$a10" \
                                "$a11" "$a12" "$a13" "$a14" "$a15" "$a16"
    do
        if [ "$byte_val" = "" ]
        then
            break
        fi
        code using the $byte_val value
        for example: echo -e "\0$byte_val\c"
              ︙
    done
done
share|improve this answer

You can use perl instead:

$ printf '%s%s%s' 'ăâé' | LC_ALL=C.UTF-8 perl -Mopen=locale -ne '
    BEGIN { $/ = \1 }
    printf "%s\n", $_;
'
ă
â
é
share|improve this answer
    
You're just printing the same byte back? As I understand it the asker wants to know how to read a byte from one input, print it to output, and then to wait for another byte from a different input before continuing the loop. Unless that's already what you do and I don't get it - which is entirely possible. –  mikeserv Sep 11 '14 at 19:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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