You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:通过PySerial实现串口HTTP请求的代码调试问题

串口转发HTTP请求代码故障排查求助

我希望通过中间设备,仅发送URL(无需请求头)实现HTTP请求,网络拓扑为:仅带COM口的PC ↔ 带COM口和网络的PC ↔ 互联网。我编写了两段测试代码但无法正常运行,恳请协助解决。

预期流程

  • 中间机等待COM口发来以“http”开头的消息;
  • 客户端输入URL后将其发送给中间机;
  • 中间机收到URL后向该地址发起HTTP请求;
  • 下载完成后将响应内容通过串口返回;
  • 客户端收到内容后保存至response.bin文件。

中间机代码

import serial
import requests

# Open the serial port for reading and writing
ser = serial.Serial('COM5', baudrate=9600, timeout=1)

while True:
 # Read a message from the serial port
 message = ser.readline().decode('utf-8').strip()
 # If the message is a URL, download the page or file at the URL
 if message.startswith('http'):
  response = requests.get(message)
  data = response.content
  # Send the file back through the serial port
  ser.write(data)

客户端(仅带COM口的PC)代码

import serial

# Open the serial port for reading and writing
ser = serial.Serial('COM6', baudrate=9600, timeout=1)

# Prompt the user to enter a URL
url = input('Enter a URL: ')

# Send the URL over the serial port
ser.write(url.encode())

# Wait for the response from the server
response = ser.read_all()

# Save the response to a file
with open('response.bin', 'wb') as f:
 f.write(response)

内容的提问来源于stack exchange,提问作者Naomi Bennett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 11:05:21