0
我正在爲我的項目使用VB-8200振動計。它以16位數字格式提供振動測量。例如。 000001 ???? 0004這意味着振動值是0.4毫米/秒。我想寫一個python代碼,在收到文本文件後發送0.4到文本文件。 我寫的代碼如下:以十進制格式將傳感器數據寫入文本文件的Python代碼
import string
import time
import serial
ser = serial.Serial(port='/dev/ttyS3', baudrate=9600, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=0.0005)try:
ser.isOpen()
print("serial port is open")
except:
print("error")
exit()
if(ser.isOpen()):
try:
while(1):
x=ser.readline().rstrip('\0')
print x
y= x[11:]
textdata = str(y)/10
f = open('/home/root/sensor_data.txt','a')
f.write(textdata+'\n')
time.sleep(1)
f.close()
except Exception:
print("error")
else:
print("cannot open serial terminal")
但是它無法正常工作。你能幫我糾正這個問題嗎?當我不在/ 10中時,textdata = str(y)/ 10,它正在工作。但我需要將整數格式的數據發送到文本文件,以便我可以將它繪製在事物中。
請澄清問題。這是什麼意思'000001 0004'?問題是什麼,解釋最後兩句話 –