代码拉取完成,页面将自动刷新
import serial
import os
import time
from serial.tools import list_ports
import socket
s = socket.socket() # 创建 socket 对象
host = "127.0.0.1" # 获取本地主机名
port = 7000 # 设置端口号
s.connect((host, port))
#获取端口列表
plist = list(serial.tools.list_ports.comports())
for port in plist:
print('端口号:' + port[0] + ' 端口名:' + port[1])
#打开端口
port = serial.Serial(port = '/dev/ttyAMA0', baudrate = 420000, bytesize = 8
, parity = 'N', stopbits = 1 , timeout= 1.0)
#校验初始化
lut = []
poly = 0xD5
idx = 0
for CNT in range(256):
lut.append(0)
while idx<256:
crc = idx
shift = 0
while shift<8:
shift += 1
if crc & 0x80:
crc_temp = poly
else:
crc_temp = 0
crc = (crc << 1) ^ crc_temp
lut[idx] = crc & 0xff
idx += 1
#主循环
while(True):
time.sleep(0.01)
#读端口
data = port.read_all()
if(len(data)>7):
#校验
len_data = data[1]
data_idx = 2
try:
inCrc = data[2+len_data-1]
except Exception as e:
# 捕获其他类型的异常
# print(f"An unexpected error occurred: {e}")
continue
crc = 0
while len_data-1:
try:
crc = lut[crc ^ data[data_idx]]
data_idx += 1
len_data -= 1
except Exception as e:
# 捕获其他类型的异常
# print(f"An unexpected error occurred: {e}")
break
if (data[2] == 22) & (len(data) >= (len_data+2)) & (inCrc == crc):
#解析,从地四个字节开始,每个字节占用11位
ch1 = ((data[3]>>0) | (data[4]<<8)) & 0x07FF
ch2 = ((data[4]>>3) | (data[5]<<5)) & 0x07FF
ch3 = ((data[5]>>6) | (data[6]<<2) | (data[7]<<10)) & 0x07FF
ch4 = ((data[7]>>1) | (data[8]<<7)) & 0x07FF
ch5 = ((data[8]>>4) | (data[9]<<4)) & 0x07FF
#结果显示(可不看)
#清屏
os.system('clear')
#通道数值
content = ""
print("ch2->"+ str(ch2))
if (ch2>950 and ch2<1050) :
print("ch2中间")
content += "24@23@18@1@"
content += "20@16@12@1"
elif(ch2 >1050 ):
print("ch2前进")
content += "24@23@18@"
content += str(ch2-949)
content += "@"
content += "20@16@12@"
content += str(ch2-949)
elif(ch2 < 950):
print("ch3后退")
content += "23@24@18@"
content += str(1124-ch2)
content += "@"
content += "16@20@12@"
content += str(1124-ch2)
print("ch3->"+ str(ch3))
content += "@"
if (ch3>950 and ch3<1080) :
print("ch3中间")
content += "27@17@13@1@"
content += "26@21@19@1"
elif(ch3 >1080 ):
print("ch3前进")
content += "27@17@13@"
content += str(ch3-979)
content += "@"
content += "26@21@19@"
content += str(ch3-979)
elif(ch3<910):
print("ch2后退")
content += "17@27@13@"
content += str(1124-ch3)
content += "@"
content += "21@26@19@"
content += str(1124-ch3)
print(content)
s.send(content.encode('utf-8'))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。