python - 通过python代码的串行监视器(arduino)不起作用
问题描述
我为连接到 arduino 的 A4998 驱动器的步进电机编写了代码:
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int cycle = 200*8;
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
char val = Serial.read();
if (val == '1') {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int i=0; i<=cycle/10; i++){
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}
// delay(2000);
}
if (val == '0') {
digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int i=0; i<=cycle/10; i++){
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}
// delay(2000);
}
}
}
当我在 arduino 程序中使用串行监视器时,这运行良好,但是当我试图从我的 python 代码启动电机时:
import serial
ser = serial.Serial('COM8', baudrate=9600, timeout=1)
ser.write(b'1')
ser.close()
它不起作用,什么也没有发生。
我错过了什么?
谢谢你。
解决方案
推荐阅读
- php - 日期映射器不接受发送的数据,尽管它似乎是可接受的格式
- string - 批处理脚本 - 用从变量接收的另一个字符串替换字符串
- php - 如何将 cloudways CDN 集成到我的 Codeigniter 项目中
- r - R - 如何更改单个条形图的堆积条形图顺序?
- python - python减去键不匹配的字典
- excel - 如何使用电源查询来抓取 CME。?
- javascript - reject() 和 return 不会终止执行
- python - 如何避免分页Flask中随机显示的元素?
- reactjs - 检查用户是否在会话期间访问过路线
- perl - 连接来自“keytool -list”输出的证书行