Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SCmd.readSerial()和Serial.read()之间的差异_Python_Arduino - Fatal编程技术网

Python SCmd.readSerial()和Serial.read()之间的差异

Python SCmd.readSerial()和Serial.read()之间的差异,python,arduino,Python,Arduino,目前,我正试图通过串行端口将信息从python发送到Arduino 我只使用一个字母Serial.read()'p'来管理它,并使用以下代码对我的Arduino执行操作 Arduino代码: #define arduinoLED 12 // Arduino LED on board void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(arduinoLED, OU

目前,我正试图通过串行端口将信息从python发送到Arduino

我只使用一个字母
Serial.read()
'p'来管理它,并使用以下代码对我的Arduino执行操作

Arduino代码:

#define arduinoLED 12   // Arduino LED on board

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(arduinoLED, OUTPUT);     // Configure the onboard LED for output
  digitalWrite(arduinoLED, LOW);   // default to LED off
 }

void loop() {
// put your main code here, to run repeatedly:
//delay (20000);
//char comein=Serial.read();
//Serial.println(comein);
char *arg = "hello";
if (Serial.read()== 'P'){


digitalWrite(arduinoLED, HIGH);
delay(5000);
}
else {
digitalWrite(arduinoLED, LOW);
Serial.println("Hello World");
}
}
 #include <SerialCommand.h>

 #define arduinoLED 12   // Arduino LED on board

 SerialCommand SCmd;   // The demo SerialCommand object

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(arduinoLED, OUTPUT);     // Configure the onboard LED for output
  digitalWrite(arduinoLED, LOW);   // default to LED off


  SCmd.addCommand("P1", process_command1); // Converts two arguments to integers and echos them back
  SCmd.addCommand("P", relay1_on);       // Turns Relay1 on
  SCmd.addDefaultHandler(unrecognized);  // Handler for command that isn't matched  (says "What?")
  Serial.println("Ready");
  }

  void loop() {
  // put your main code here, to run repeatedly:

  SCmd.readSerial();     // We don't do much, just process serial commands


 }

 void relay1_on()
{
  digitalWrite(12, HIGH);
  Serial.println(3000);
  delay(3000);
  digitalWrite(12, LOW);
 }

void process_command1()
{
  int aNumber = 5;
  char *arg = "hello";

  Serial.println("We're in process_command");
  arg = SCmd.next();
  int OhmPosition = atoi(arg); //will return only numbers
  arg = SCmd.next();
  int relay = atoi(arg); //will return only numbers
  arg = SCmd.next();
  int opentime = atoi(arg); //will return only numbers

  Serial.println(OhmPosition);
  Serial.println(relay);
  Serial.println(opentime);
  }
Python代码:

ser.open()
ser.is_open

my_string='P'
my_string_as_bytes=str.encode(my_string)
print(my_string_as_bytes)
ser.write(my_string_as_bytes)
my_string6 = 'P1'+str(actions_time_[0][0] )+' '+str(actions_time_[0][1])+' '+str(actions_time_[0][2]))
my_string_as_bytes=str.encode(my_string6)
print(my_string_as_bytes)
ser.write(my_string_as_bytes)
output looks like this=> b'P1 150.0 5.0 2000.0 '
这很好,可以打开我的LED,但我如何才能管理多个命令字母,例如“P1 2018”以打开LED

但我真正的问题是,我尝试做完全相同的事情,使用相同的Python代码,但使用
SCmd.readSerial()
来读取Arduino中的信息,例如:

Arduino代码:

#define arduinoLED 12   // Arduino LED on board

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(arduinoLED, OUTPUT);     // Configure the onboard LED for output
  digitalWrite(arduinoLED, LOW);   // default to LED off
 }

void loop() {
// put your main code here, to run repeatedly:
//delay (20000);
//char comein=Serial.read();
//Serial.println(comein);
char *arg = "hello";
if (Serial.read()== 'P'){


digitalWrite(arduinoLED, HIGH);
delay(5000);
}
else {
digitalWrite(arduinoLED, LOW);
Serial.println("Hello World");
}
}
 #include <SerialCommand.h>

 #define arduinoLED 12   // Arduino LED on board

 SerialCommand SCmd;   // The demo SerialCommand object

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(arduinoLED, OUTPUT);     // Configure the onboard LED for output
  digitalWrite(arduinoLED, LOW);   // default to LED off


  SCmd.addCommand("P1", process_command1); // Converts two arguments to integers and echos them back
  SCmd.addCommand("P", relay1_on);       // Turns Relay1 on
  SCmd.addDefaultHandler(unrecognized);  // Handler for command that isn't matched  (says "What?")
  Serial.println("Ready");
  }

  void loop() {
  // put your main code here, to run repeatedly:

  SCmd.readSerial();     // We don't do much, just process serial commands


 }

 void relay1_on()
{
  digitalWrite(12, HIGH);
  Serial.println(3000);
  delay(3000);
  digitalWrite(12, LOW);
 }

void process_command1()
{
  int aNumber = 5;
  char *arg = "hello";

  Serial.println("We're in process_command");
  arg = SCmd.next();
  int OhmPosition = atoi(arg); //will return only numbers
  arg = SCmd.next();
  int relay = atoi(arg); //will return only numbers
  arg = SCmd.next();
  int opentime = atoi(arg); //will return only numbers

  Serial.println(OhmPosition);
  Serial.println(relay);
  Serial.println(opentime);
  }
为了使我能够启动P1命令并发送值,这些值将保存在
OhmPosition
继电器中,时间将由一个空格分隔,因为目标是引导一个小型电气系统


如果您能支持这对相互关联的点,我将非常高兴。

您的程序如何区分接收“P1”命令和接收“p”命令后随机出现的“1”命令


您的代码似乎依赖的库不是Arduino安装的标准部分。您应该提供一个指向SerialCommand类的链接

嗨,Delta_G,用于SCmd命令的库如下所示:程序重新识别'p'或'P1',因为它是在void setup()中arduino代码的SCmd.addCommand部分中定义的。嗨,大家好,多亏了Skarab,我找到了答案,主要问题是我的命令末尾没有“\r\n”。