0

I have a host connected to a Linux target over serial. The target is using the serial port for shell I/O. I need to save a text file on the target with contents from the host.

I thought I could get away with doing:

ser.write("cat > file.txt\n")
ser.write([contents I need to add to the file])
ser.write(chr(4))
ser.write(chr(4))

But the 's I'm sending aren't closing the file. I've tried a few variations of chr(4)... \x04, print, str =, and a few others, but they all fail the same way.

If I simulate this with minicom, and follow up sending the [contents...] with uploading a 2 byte file that holds 0x04 0x04, the file closes as expected.

I haven't tried opening the "EOF" file in python and sending it yet. I'll do it, I'm about out of options. But I'm new to python, so I must be doing something wrong.

Any obvious newb-fixing answer to this one?

Thanks.

2
  • first, make sure your [contents...] is terminated with a newline. Then maybe try adding ser.flush() at the end. Commented Aug 14, 2013 at 15:05
  • Did both of those (and also tried Ctrl-D Ctrl-D instead of newline, that should work too), but it's not closing the file. I need to go through minicom and type Ctrl-D to close it. Commented Aug 15, 2013 at 15:21

1 Answer 1

1

As a workaround, could you use a heredoc ?

ser.write("cat > file.txt << END_OF_FILE\n")
ser.write([contents I need to add to the file])
ser.write("\nEND_OF_FILE\n");

For a more robust solution, you should probably have to look at some file transfer protocol over serial line, like Kermit.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I'm using that work-around now. It's likely that I could come up with an end-string that's unlikely to appear in the data I'm transferring, but I was still hoping that I could just simulate a couple Ctrl-D's somehow.
@user2679165 Even if there is a "magic" sequence of character, I don't think this is a viable solution anyway: maybe it would works for this particular file, but more generally, whatever is that sequence, the probability it appears in the transferred file is not nul. For a more robust approach, you will have to look a some protocol like the venerable Kermit. I edited my answer accordingly.
Unfortunately I can't assume kermit, ymodem, or any other protocol is available on the remote side. I don't have control over it. I can assume it has a file system I can write to, and cat, redirection, and pipes are available. Those are enough for me to do what I need to with minicom, I just need to figure out how in python.
@user2679165 Do you have an oscilloscope at hand? Is it possible that Minicom simulate the end-of-file by lowering down/raising up some control lines?
That would be pretty cool. But it also works correctly when I upload a file that has a couple Ctrl-D's (0x04 0x04) in it, and I don't think it's parsing file contents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.