Linux shell: save input line from Serial Port each minute and send to remote server

倖福魔咒の 提交于 2019-12-13 08:46:12

问题


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:


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


来源:https://stackoverflow.com/questions/26538847/linux-shell-save-input-line-from-serial-port-each-minute-and-send-to-remote-ser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!