Search code examples
serial-portcommunicationgnu-screen

Is it possible to send file contents to a GNU Screen session?


I'm connecting to a development board over a serial port like so...

screen /dev/ttyUSB0 9600

I'm connected to a bootloader now, and it is asking me to send a file in SREC format. I have the file, but how can I send it over the Screen session?


Solution

  • GNU Screen has a command called readreg which you can use to read a file into a register. After a register is filled with data you can paste that data using the paste command.

    Inside Screen

    Inside Screen, you press Ctrl + A and then : to execute a command. Then you simply write and press Enter:

    readreg p /path/to/thefile
    

    After you have executed the command, you should see a message saying Slurped X character into buffer.

    You can then paste the data in that buffer by again pressing Ctrl + A and then :, then write and press Enter:

    paste p
    

    Note: p is the name of the register

    And you're done.

    Outside Screen

    You could also execute the commands outside the Screen session using the -X option. If you have a Screen session named "ucontroller" which is attached to your serial port, you can send the commands by executing:

    screen -S ucontroller -X readreg p /path/to/thefile
    screen -S ucontroller -X paste p
    

    More resources

    The information I have provided is taken directly from the man pages of screen(1). Here is the relevant part of the man page:

    readreg [-e encoding] [register [filename]]

    Does one of two things, dependent on number of arguments: with zero or one arguments it it duplicates the paste buffer contents into the register specified or entered at the prompt.

    With two arguments it reads the contents of the named file into the register, just as readbuf reads the screen-exchange file into the paste buffer. You can tell screen the encoding of the file via the -e option. The following example will paste the system's password file into the screen window (using register p, where a copy remains):

    C-a : readreg p /etc/passwd

    C-a : paste p