USBデータロギングUbuntu MATE

USBデータロギングUbuntu MATE

Ubuntuメイト、ラズベリーパイを使用しています。私は初めてプログラミングに触れ、初めてbashに触れました。簡単に言えば、USBデータをCSVに変換すると、スクリプトなしで何でも試すことができます。

編集:デバイスがループ内にあるため、データの受信を停止することはできません。リアルタイムデータフィルタリングにより、実際のデータのみが受信されます。

私の他の同様の質問については:Linux端末はファイルに出力されますが、フィルタリングされますか?

RFレシーバーを作成する会社によって提供されたPythonスクリプトがあります(最後のスクリプト)。これはユーザーエラーである可能性が高いです。

私が端末に書いた内容は次のとおりです。

python3 'home/#USER/Desktop/python/script.py' /dev/ttyUSB0 | filter.pl    >> output.csv
[1+] Stopped
Filter.pl: command not found

sed端末の使用:

python3 'home/#USER/Desktop/python/script.py' /dev/ttyUSB0 | sed -n "s/^b';[0-9]\{3\}];\(.\+\);'/\1/p" >> output_file

結果:フラッシュタイプの後にCTRL + Cが続きます。

Exception ignored in: <_io.TextIOrapper name='<stdout>' mode='w' ecpmdomg='UTF-8'>  
BrokenPipeError:[Errno32] Broken pipe.

注:私はもっと長い部分が欲しいです。そこでさらに分析し、AT&T M2Xオンラインサービスに送ります。

b';"Anything from 1-9999";'
b';311;'

b';312;'

b';312;00000000;036;552;1014f49;3020;2659;6294;1049;2659;S;'

b';313;'

script.py:

#! / Usr / bin / python
# Coding: UTF-8

################################################## #########################
# (C) Tokyo Cosmos Electric, Inc. (TOCOS) - all rights reserved.
# Terms and Conditions:
# - This source code, as long as the Tokyo Cosmos Electric Co., Ltd.         copyright separately there is no source code license description
# We will not hold.
# - This source code is no warranty-free support. Any damage which the  present source code and product
Tokyo Cosmos Electric Co., Ltd. does not guarantee also #. Report of trouble etc. will be welcome.
# - This source code is published on the premise that run with TWE series  Tokyo Cosmos Electric Co., Ltd. to sell
#     doing.
################################################## #########################

### Script to read the TWE-Lite standard application
# ? this script is read-only, to do the reading and writing both will require processing by multiple threads.

from serial import *
from sys import stdout, stdin, stderr, exit

Confirmation of # parameter
# First argument: serial port name
! if len (sys.argv) = 2:
    print "% s {serial port name}"% sys.argv [0]
    exit (1)

# Open a serial port
try:
ser = Serial (sys.argv [1], 115200)
print "open serial port:% s"% sys.argv [1]
except:
    print "can not open serial port:% s"% sys.argv [1]
exit (1)

# Display of other messages (output the payload)
def printPayload (l):
    if len (l) <3: return False # check of data size

print "command = 0x% 02x (other)"% l [1]
print "src = 0x% 02x"% l [0]

# Directly outputs the payload
print "payload =",
for c in l [2:]:
    print "% 02x"% c,
print "(hex)"
return True

# 0x81 interpretation of the message
def printPayload_0x81 (l):
    if len (l) = 23:! return False # check of data size

ladr = l [5] << 24 | l [6] << 16 | l [7] << 8 | l [8]
print "command = 0x% 02x (data arrival)"% l [1]
print "src = 0x% 02x"% l [0]
print "src long = 0x% 08x"% ladr
print "dst = 0x% 02x"% l [9]
print "pktid = 0x% 02x"% l [2]
print "prtcl ver = 0x% 02x"% l [3]
print "LQI =% d /% .2f [dbm]"% (l [4], (7 * l [4] -1970) / 20.)
ts = l [10] << 8 | l [11]
print "time stmp =% .3f [s]"% (ts / 64.0)
print "relay flg =% d"% l [12]
vlt = l [13] << 8 | l [14]
print "volt =% 04d [mV]"% vlt

Data of # DI1..4
dibm = l [16]
dibm_chg = l [17]
di = {} # of the current state
di_chg = {} # 1 Once in Lo (1) at least once
for i in range (1,5):
    di [i] = 0 if (dibm & 0x1) == 0 else 1
    di_chg [i] = 0 if (dibm_chg & 0x1) == 0 else 1
    dibm >> = 1
    dibm_chg >> = 1
    pass

print "DI1 =% d /% d DI2 =% d /% d DI3 =% d /% d DI4 =% d /% d"% (di [1], di_chg [1], di [2], di_chg [ 2], di [3], di_chg [3], di [4], di_chg [4])

Data of # AD1..4
ad = {}
er = l [22]
for i in range (1,5):
    av = l [i + 18 - 1]
    if av == 0xFF:
        # AD and if the port is unused treatment (approximately 2V or more) -1
        ad [i] = -1
    else:
        # Calculation, including the correction bits
        ad [i] = ((av * 4) + (er & 0x3)) * 4
    er >> = 2
print "AD1 =% 04d AD2 =% 04d AD3 =% 04d AD4 =% 04d [mV]"% (ad [1], ad [2], ad [3], ad [4])

return True

# Interpret the data line by line
while True:
    line = ser.readline (). rstrip () # to read in one line units, and remove the trailing line feed code (blocking read)

if len (line)> 0 and line [0] == ':':
    print "\ n% s"% line
else:
    continue

try:
    lst = map (ord, line [1:]. decode ('hex')) # convert the HEX string after decoding the string, to each ord and () the list
    csum = sum (lst) & 0xff # checksum if 0 by adding a total of 8bit calculation OK
    lst.pop () remove the # checksum from the list
    if csum == 0:
        if lst [1] == 0x81:
            printPayload_0x81 (lst) # reception of IO-related data
        else:
            printPayload (lst) # Other data reception
    else:
        print "checksum ng"
except:
    print "skip" # when an error

ベストアンサー1

最初のサンプルでは

Filter.pl: command not found

シェルが filter.pl スクリプトを見つけられませんでした。

スクリプトのフルパスを指定する必要があります。現在のディレクトリにある場合は、./filter.pl次のように使用します。

/home/USER/Desktop/python/script.py /dev/ttyUSB0 | ./filter.pl >> output.csv

おすすめ記事