tbird20d / grabserial

Grabserial - python-based serial dump and timing program - good for embedded Linux development
GNU General Public License v2.0
195 stars 77 forks source link

Grabserial as startup service/file/bash script #26

Closed obabarin closed 5 years ago

obabarin commented 5 years ago

I made a script to capture ttyAMA0 as follows.. #! /bin/bash sleep 5 grabserial -v -d /dev/ttyAMA0 -T -b 115200 -e 3600 -Q -o "/home/pi/file-%d-%m-%Y-%H.%M.%S.txt" -a

This runs well from the terminal but I can't get it to work as a startup script in either cron job or systemd service or even when I add it to update-rc.d

On update-rc.d, it just create the file, but does not read any data into it. As a systemd service, the script keeps looping- opens, doesn't store any data, closes, opens another files; all within seconds. I think this has to do with systemd configurations. In a cron job, the job just wouldn't start.

Is it possible grabserial was not written to be suitable as a startup?

obabarin commented 5 years ago

A error report from the created service is as below: -- Logs begin at Thu 2016-11-03 18:16:42 CET, end at Tue 2018-10-16 11:50:44 CEST. -- Oct 16 11:46:02 raspberrypi systemd[1]: Started serial data logger with grabserial. Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Opening serial port /dev/ttyAMA0 Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: 115200:8N1:xonxoff=0:rtscts=0 Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Program set to restart after 3600 seconds. Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Printing absolute timing information for each line Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Saving data to '/home/pi/file-16-10-2018-11.46.09.txt' Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Keeping quiet on stdout Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Use Control-C to stop... Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Traceback (most recent call last): Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/local/bin/grabserial", line 4, in <module> Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: __import__('pkg_resources').run_script('grabserial==1.9.8', 'grabserial') Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 739, in run_script Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: self.require(requires)[0].run_script(script_name, ns) Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1501, in run_script Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: exec(script_code, namespace, namespace) Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/local/lib/python2.7/dist-packages/grabserial-1.9.8-py2.7.egg/EGG-INFO/scripts/grabserial", line 588, in <module> Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/local/lib/python2.7/dist-packages/grabserial-1.9.8-py2.7.egg/EGG-INFO/scripts/grabserial", line 500, in grab Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: TypeError: encode() argument 1 must be string, not None Oct 16 11:46:09 raspberrypi systemd[1]: pigrab.service: Main process exited, code=exited, status=1/FAILURE Oct 16 11:46:09 raspberrypi systemd[1]: pigrab.service: Unit entered failed state. Oct 16 11:46:09 raspberrypi systemd[1]: pigrab.service: Failed with result 'exit-code'.

Showing that grabserial crashes

tbird20d commented 5 years ago

This output is very confusing to me. What is accessing the EGG-INFO, and why? That's usually only used for installation.

I can't tell from the output where in grabserial the fault is occuring. It looks like maybe line 500, which is this line: out.write(msg.encode(sys.stdout.encoding)) which is about right for an error about encoding.

I suspect that this has to do with sys.stdout, when the code is run under another system, not having the same encoding attributes as when grabserial is used at the command line.

This is a suspicious line, as it's using the sys.stdout.encoding for the output file (which might have a different encoding). I've been fighting with differences between python2 and python3 encoding and string handling for the last year, and it still doesn't seem quite right.

You might try changing this line to just: out.write(msg) and see what happens. Or, if that doesn't work, you might try changing this to: out.write(msg.encode("utf8"))

Try that, and let me know if this fixes the problem.

Sorry for the issues. -- Tim

tbird20d commented 5 years ago

Also, just a comment on your script. It looks from the issue comment that the sleep and grabserial call are all on the 'shebang' line of the script. Is that right, or is this just a formatting issue with the comment?

obabarin commented 5 years ago

hi tbird20d,

The issues is really from line 500 of the source code. But I wont be changing it as suggested. I added the bash script to rc.local and all seems to go on well for now. I will continue testing, and if any other issues comes up, I will try your suggestion or reach out to you again.

Thanks for the wonderful code. It does what I needed for the time being.

NB- apologies for the formatting of the text of the output and the bash script.

tbird20d commented 5 years ago

No problem. I'm glad you found a solution.

henning-schild commented 5 years ago

@obabarin grabserial is great for debugging and measuring timing etc. if you are looking for a deamon to capture serial console output https://www.conserver.com/ might be the better solution ,will log to syslog do log rotation, probably comes with your distro including init/systemd stuff, and you can interact with the consoles ...