Linux shell: save input line from Serial Port each

2019-09-20 04:51发布

I have Arduino connected to computer over RS-232 (only TxD, RxD and GND).

Arduino only send data to computer and computer receive it. Computer do not transmit anything to Arduino.

Computer is WiFi router with OpenWrt linux with 16MB RAM and 4MB flash for system. I do not have free enough space for "good tool" like python (I have the same working program on x86 PC written in python).

Arduino send data to PC +- each 60 seconds. Data has following format:

SENSOR1;12.34;95.47
ABC245;34.5;75.1

2 sensors each have 2 values. Line is ended using <CR><LF>. I can modify this "protocol" to for example one line like (or any other):

SENSOR1;12.34;95.47|ABC245;34.5;75.1

so on wifi router I need little program which read this string every minute and save it to variable. This variable insert to curl and send to remote server. Can I send data to server without curl (with less ram/flash usage)?

I would like to use pure bysybox sh (bash is to big).

I found Bash script: save stream from Serial Port (/dev/ttyUSB0) to file until a specific input (e.g. eof) appears :

#!/bin/bash
while read line; do
     if [ "$line" != "EOF" ]; then
          echo "$line" >> file.txt
     else
          break
     fi
done < /dev/ttyUSB0

awk `
/EOF/ {exit;} 
 {print;}` < /dev/ttyUSB0 > file.txt

is it good choice to use/modify these script? Is there any other better solution?

1条回答
虎瘦雄心在
2楼-- · 2019-09-20 05:11

Why not give a try to ser2net pakage?
This will allow forward the serial port to the server. It work ok on OpenWrt

Lua is buid in. A script in Lua ca read also from the serial port , but you neet to set the port parameters first with stty

stty 9600 raw < /dev/ttyUSB0
lua myscript < /dev/ttyUSB0
查看更多
登录 后发表回答