本帖最后由 walden 于 2023-4-22 17:03 编辑
[Python] 纯文本查看 复制代码 # !/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import datetime
import serial.tools.list_ports
def request_500K():
ser = serial.Serial(port="COM_PORT", baudrate=460800, bytesize=serial.EIGHTBITS, stopbits=serial.STOPBITS_ONE,timeout=60)
if ser.isOpen():
print("打开串口成功, 串口号: %s" % ser.name)
else:
print("打开串口失败")
# 串口发送数据
data = "AT+QIOPEN=1,0,\"TCP\",ip,port,0,2\r\n\r\n"
data1 = "GET /test_500k.txt HTTP/1.1\r\nHost: ip\r\nConnection: close\r\n\r\n"
print("发送数据: %s" % data)
write_len = ser.write(data.encode('utf-8'))
print("等待2S")
time.sleep(2)
print("发送数据: %s" % data1)
write_len1 = ser.write(data1.encode('utf-8'))
com_input = ser.read(513000)
print(str(com_input))
with open("log.txt", "a") as f:
f.write(str(com_input) + "\r\n")
# 关闭串口
ser.close()
if ser.isOpen():
print("串口未关闭")
else:
print("串口已关闭")
i = 0
while i <= 100:
time1 = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
time2 = str(time1)
print(time2)
with open("log.txt", "a") as f:
f.write(time2)
f.write("\r\n\r\n")
request_500K()
print("等待5S")
time.sleep(5)
i += 1 |