python与arduino进行参数传递_使用Python脚本将串行数据发送到Arduino

I am facing a peculiar issue while writing characters to Arduino using python serial communication on macOS 10.14.

The Arduino is programmed to read a string, parse it and take PWM action to run a car.

Ardiuno's serial communication channel is configured to receive the strings in the format < A, B, C, D > where ABCD are numbers which denote car direction, speed, steering direction and steering position.

The problem is, when I send a string from the serial monitor or through the Python Development environment the string is received, parsed properly and command executed successfully.

However if I write a simple program in a file write.py and execute it from the command line, nothing happens.

import serial

ser = serial.Serial('/dev/cu.usbmodem14301', 9600)

data = '<1,150,0,0>'

ser.write(data.encode())

If I run this script from the macOS terminal using the command:

python write.py

nothing happens. What am I missing here?

解决方案

A new USB connection with ser=serial.Serial('/dev/cu.usbmodem14301',9600) resets the Arduino. The data sent right after connection are lost because the Arduino boots.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值