Python Arduino忽略第一条消息,只响应后续消息

Python Arduino忽略第一条消息,只响应后续消息,python,arduino,serial-port,pyserial,Python,Arduino,Serial Port,Pyserial,我使用下面的代码使用PySerial与Arduino通信 def test_without_thread(port, msg): ser = serial.Serial(port, baudrate=115200, timeout=1.) for i in range(10): ser.write(msg) print i, ser.readline().strip("\r\n") 和你一起跑的时候 msg = "q02i15i21" port

我使用下面的代码使用PySerial与Arduino通信

def test_without_thread(port, msg):
    ser = serial.Serial(port, baudrate=115200, timeout=1.)

    for i in range(10):
        ser.write(msg)
        print i, ser.readline().strip("\r\n")
和你一起跑的时候

msg = "q02i15i21"
port = "/dev/ttyACM1"
test_without_thread(port, msg)
提供以下输出:

0 
1 oi1+1023003+i2+103+
2 oi1+1023003+i2+103+
3 oi1+1023003+i2+103+
4 oi1+1023003+i2+103+
5 oi1+1023003+i2+103+
6 oi1+1023003+i2+103+
7 oi1+1023003+i2+103+
8 oi1+1023003+i2+103+
9 oi1+1023003+i2+103+
如果我在第一次写入之前睡觉(就在循环之前),所有请求都会超时。在我的Arduino代码中的
serial.Begin()
之后,我确实睡了10毫秒

我的Arduino代码(相关部分)如下:

volatile unsigned int interlock_state = OPEN;

float get_ext_ilk(){
  return (float) interlock_state;
}

void dummyFunc(float set_value){
  Serial.println("Called the dummy function!");
}

void setup() {
  Serial.begin(115200);
  delay(10);
  com.add_channel(mist1::Channel("EXT_ILK", 'x', 1, &dummyFunc, &get_ext_ilk));  //To read interlock status
}

void loop() {
  if (Serial.available()) {
    com.respond_to_input_message();
  }  
}
void mist1::Communication::respond_to_input_message() {
  char inputMessage[128];
  get_serial_data(inputMessage);

  char keyword = inputMessage[0];

  if (keyword == 'c') {
    Serial.println(get_all_channel_names());
  } else if (keyword == 'i') {
    Serial.println(get_all_channel_identifiers());
  } else if (keyword == 'n') {
    Serial.println(_com_name);
  } else if (keyword == 'q') {
    // Query.

    // Find what the user is querying for.
    int numberOfChannels = get_number_of_channels_queried(inputMessage);

    // Read input message.
    unsigned precisions[numberOfChannels];
    char channelIdentifiers[numberOfChannels];
    unsigned channelNumbers[numberOfChannels];

    String messageToReturn = "o";

    for (unsigned channelIndex = 0; channelIndex < numberOfChannels; channelIndex++) {
      for (unsigned i=0; i < _number_of_channels; i++) {
        if ((char)_all_channels[i].get_channel_identifier() == channelIdentifiers[channelIndex]){
          if (_all_channels[i].get_channel_number() == channelNumbers[channelIndex]){
            float valueToOutput = _all_channels[i].call_get_func();
            char * buff = float2s(valueToOutput, precisions[channelIndex]);
            char valueToPrint[1 + 1 + precisions[channelIndex] + 1 + 1 + 1]; // sign(1) + digit(1) + precision + exponent(1) + sign(1) + termination character(1).
            memset(valueToPrint, '\0', (1 + 1 + precisions[channelIndex] + 1 + 1 + 1));
            convert_scientific_notation_to_mist1(buff, valueToPrint, precisions[channelIndex]);
            String new_addition = (String) channelIdentifiers[channelIndex] + (String) channelNumbers[channelIndex] + valueToPrint;
            messageToReturn += new_addition;
            break;
          }     
        }
      }
    }
    messageToReturn += "\r\n";

    Serial.print(messageToReturn);
}
respond\u to\u input\u message()
方法如下:

volatile unsigned int interlock_state = OPEN;

float get_ext_ilk(){
  return (float) interlock_state;
}

void dummyFunc(float set_value){
  Serial.println("Called the dummy function!");
}

void setup() {
  Serial.begin(115200);
  delay(10);
  com.add_channel(mist1::Channel("EXT_ILK", 'x', 1, &dummyFunc, &get_ext_ilk));  //To read interlock status
}

void loop() {
  if (Serial.available()) {
    com.respond_to_input_message();
  }  
}
void mist1::Communication::respond_to_input_message() {
  char inputMessage[128];
  get_serial_data(inputMessage);

  char keyword = inputMessage[0];

  if (keyword == 'c') {
    Serial.println(get_all_channel_names());
  } else if (keyword == 'i') {
    Serial.println(get_all_channel_identifiers());
  } else if (keyword == 'n') {
    Serial.println(_com_name);
  } else if (keyword == 'q') {
    // Query.

    // Find what the user is querying for.
    int numberOfChannels = get_number_of_channels_queried(inputMessage);

    // Read input message.
    unsigned precisions[numberOfChannels];
    char channelIdentifiers[numberOfChannels];
    unsigned channelNumbers[numberOfChannels];

    String messageToReturn = "o";

    for (unsigned channelIndex = 0; channelIndex < numberOfChannels; channelIndex++) {
      for (unsigned i=0; i < _number_of_channels; i++) {
        if ((char)_all_channels[i].get_channel_identifier() == channelIdentifiers[channelIndex]){
          if (_all_channels[i].get_channel_number() == channelNumbers[channelIndex]){
            float valueToOutput = _all_channels[i].call_get_func();
            char * buff = float2s(valueToOutput, precisions[channelIndex]);
            char valueToPrint[1 + 1 + precisions[channelIndex] + 1 + 1 + 1]; // sign(1) + digit(1) + precision + exponent(1) + sign(1) + termination character(1).
            memset(valueToPrint, '\0', (1 + 1 + precisions[channelIndex] + 1 + 1 + 1));
            convert_scientific_notation_to_mist1(buff, valueToPrint, precisions[channelIndex]);
            String new_addition = (String) channelIdentifiers[channelIndex] + (String) channelNumbers[channelIndex] + valueToPrint;
            messageToReturn += new_addition;
            break;
          }     
        }
      }
    }
    messageToReturn += "\r\n";

    Serial.print(messageToReturn);
}
void mist1::通信::响应输入消息(){
字符输入消息[128];
获取串行数据(输入消息);
char关键字=输入消息[0];
如果(关键字=='c'){
Serial.println(获取所有通道名称());
}else if(关键字=='i'){
Serial.println(获取所有通道标识符());
}else if(关键字='n'){
Serial.println(_com_name);
}else if(关键字=='q'){
//询问。
//查找用户正在查询的内容。
int numberOfChannels=获取所查询通道的数量(inputMessage);
//读取输入消息。
无符号精度[通道数];
字符通道标识符[numberOfChannels];
无符号信道号[信道数];
字符串messageToReturn=“o”;
for(无符号channelIndex=0;channelIndex
我已经用多个Arduinos、电缆和USB端口进行了测试


这是怎么回事?为什么Arduino会忽略第一条消息,而只对后续消息做出响应?

当您打开Arduino序列时,它会重新启动。因此,您必须等待重新启动结束(睡眠2秒)。在此期间写入的所有内容都将丢失

请注意,重新启动完成后,串行缓冲区中可能有垃圾

在你的函数
respond\u to\u input\u message()
我看不到响应如果消息无法识别,你应该有一个调试响应

我提到了我遇到的最后一个问题;对于我的(中文=CH340 USB/串行)nano,我无法可靠地使用比pySerial更高的9600波特的速度


祝你好运。

idk,但我经常在串行端口代码中看到“一次性”的第一项……一次性的第一项是什么?就像在我们不关心的消息中一样?没错。空的是常见的。