• <i id='Vth5J'><tr id='Vth5J'><dt id='Vth5J'><q id='Vth5J'><span id='Vth5J'><b id='Vth5J'><form id='Vth5J'><ins id='Vth5J'></ins><ul id='Vth5J'></ul><sub id='Vth5J'></sub></form><legend id='Vth5J'></legend><bdo id='Vth5J'><pre id='Vth5J'><center id='Vth5J'></center></pre></bdo></b><th id='Vth5J'></th></span></q></dt></tr></i><div id='Vth5J'><tfoot id='Vth5J'></tfoot><dl id='Vth5J'><fieldset id='Vth5J'></fieldset></dl></div>
      <bdo id='Vth5J'></bdo><ul id='Vth5J'></ul>

  • <legend id='Vth5J'><style id='Vth5J'><dir id='Vth5J'><q id='Vth5J'></q></dir></style></legend>

      <small id='Vth5J'></small><noframes id='Vth5J'>

    1. <tfoot id='Vth5J'></tfoot>
      1. pySerial write() 在 Python 解释器中工作正常,但不是 Python 脚本

        时间:2023-07-23

            <tfoot id='VGcfX'></tfoot>

            <small id='VGcfX'></small><noframes id='VGcfX'>

            <legend id='VGcfX'><style id='VGcfX'><dir id='VGcfX'><q id='VGcfX'></q></dir></style></legend>
            • <bdo id='VGcfX'></bdo><ul id='VGcfX'></ul>

              • <i id='VGcfX'><tr id='VGcfX'><dt id='VGcfX'><q id='VGcfX'><span id='VGcfX'><b id='VGcfX'><form id='VGcfX'><ins id='VGcfX'></ins><ul id='VGcfX'></ul><sub id='VGcfX'></sub></form><legend id='VGcfX'></legend><bdo id='VGcfX'><pre id='VGcfX'><center id='VGcfX'></center></pre></bdo></b><th id='VGcfX'></th></span></q></dt></tr></i><div id='VGcfX'><tfoot id='VGcfX'></tfoot><dl id='VGcfX'><fieldset id='VGcfX'></fieldset></dl></div>
                    <tbody id='VGcfX'></tbody>
                1. 本文介绍了pySerial write() 在 Python 解释器中工作正常,但不是 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  最近,我正在尝试在 Arduino 上进行某种灯光控制".我使用 Raspberry Pi 通过串行端口(USB 电缆)发送控制消息.这是 Arduino 代码:

                  Recently, I am trying to make sort of "light control" on Arduino. I use Raspberry Pi to send the control message via serial port (USB cable).Here is the Arduino code :

                  int redled = 12;
                  int whiteled = 48;
                  
                  void setup()
                  {
                      Serial.begin(9600);
                      pinMode(redled,OUTPUT);
                      pinMode(whiteled,OUTPUT);
                  }
                  
                  void loop()
                  {
                      if(Serial.available())
                      {
                          char cmd = Serial.read();
                          switch(cmd)
                          {
                              case'r':
                              digitalWrite(redled,HIGH);
                              delay(2000);
                              digitalWrite(redled,LOW);
                              break;
                  
                              case'w':
                              digitalWrite(whiteled,HIGH);
                              delay(2000);
                              digitalWrite(whiteled,LOW);
                              break;
                          }
                      }
                      else
                      {
                          Serial.println("hello pi");
                          delay(1000);
                      }
                  
                  }
                  

                  之后,我使用 Python 解释器中的 pySerial 来控制引脚,一切正常.这是一段解释器输出:

                  After that, I used pySerial from Python interpreter to control the pins, and everything was working fine. Here is a piece of interpreter output:

                  Python 2.7.3 (default, Mar 18 2014, 05:13:23)
                  [GCC 4.6.3] on linux2
                  Type "help", "copyright", "credits" or "license" for more information.
                  >>> import serial
                  >>> ser = serial.Serial('/dev/ttyACM0',9600)
                  >>> x = ser.read(10)
                  >>> print 'x = ',x
                  x =  hellhello
                  >>> ser.write('w') #white led turn on and off
                  1
                  >>> ser.close()
                  >>>
                  

                  一切正常,led 确实打开和关闭,所以我决定编写一个简单的 Python 脚本来做同样的事情:

                  Everything worked fine and led did turn on and off, so I decided to write a simple Python script to do the same:

                  import serial
                  import time
                  ser = serial.Serial('/dev/ttyACM0',9600)
                  x = ser.read(10)
                  print 'x = ',x
                  
                  time.sleep(2)
                  ser.write('w')
                  
                  ser.close()
                  

                  以下是执行命令及结果:

                  The following is the execution command and result:

                  pi@raspberrypi ~ $ python serialtest.py
                  x =  helello pi
                  

                  它只出现了来自Arduino的字符串,但根本没有打开led.看起来一切都应该没问题,所以我不知道问题出在哪里.我已经搜索了一些文章并在ser.write()"之前添加了time.sleep(2)",但它仍然无法正常工作.我将不胜感激,提前非常感谢!

                  It only appeared the string from Arduino, but no led turn on at all. It looks like everything should be fine, so I don't know what the problem can be. I already search some articles and add "time.sleep(2)" before "ser.write()", but it still couldn't work.I would appreciate any help, many thanks in advance!

                  更新:我让控制器将它接收的数据发回给我,当我运行脚本时它看起来没有收到任何东西,但是当我从解释器发送数据时它接收到了所有东西.arduino 代码的代码现在如下所示:

                  UPDATE : I made the controller send me back the data it was receiving and it looks like it isn't receiving anything when I am running the script, but receives everything when I send the data from the interpreter. The code of the arduino code now looks like this:

                  int redled = 12;
                  int whiteled = 48;
                  
                  void setup()
                  {
                      Serial.begin(9600);
                      pinMode(redled,OUTPUT);
                      pinMode(whiteled,OUTPUT);
                  }
                  
                  void loop()
                  {
                      if(Serial.available())
                      {
                          char cmd = Serial.read();
                          switch(cmd)
                          {
                              case'r':
                              digitalWrite(redled,HIGH);
                              delay(2000);
                              digitalWrite(redled,LOW);
                              Serial.println("Cmd received");
                              break;
                  
                              case'w':
                              digitalWrite(whiteled,HIGH);
                              delay(2000);
                              digitalWrite(whiteled,LOW);
                              Serial.println("Cmd received");
                              break;
                          }
                      }   
                  }
                  

                  推荐答案

                  问题是启动端口需要一些时间.在 ser = serial.Serial() 之后立即添加 5 秒的睡眠

                  The problem is that it takes some time to initiate the port. add a sleep of 5 seconds immediately after ser = serial.Serial()

                  time.sleep(5)
                  

                  这篇关于pySerial write() 在 Python 解释器中工作正常,但不是 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:尝试在 WinXP 上使用 pyserial 打开串行端口 -&gt;“拒绝访问" 下一篇:python:如何检测串行 COM 上的设备名称/ID

                  相关文章

                    <bdo id='AUYAf'></bdo><ul id='AUYAf'></ul>

                  1. <small id='AUYAf'></small><noframes id='AUYAf'>

                    <legend id='AUYAf'><style id='AUYAf'><dir id='AUYAf'><q id='AUYAf'></q></dir></style></legend>

                    <tfoot id='AUYAf'></tfoot>
                    1. <i id='AUYAf'><tr id='AUYAf'><dt id='AUYAf'><q id='AUYAf'><span id='AUYAf'><b id='AUYAf'><form id='AUYAf'><ins id='AUYAf'></ins><ul id='AUYAf'></ul><sub id='AUYAf'></sub></form><legend id='AUYAf'></legend><bdo id='AUYAf'><pre id='AUYAf'><center id='AUYAf'></center></pre></bdo></b><th id='AUYAf'></th></span></q></dt></tr></i><div id='AUYAf'><tfoot id='AUYAf'></tfoot><dl id='AUYAf'><fieldset id='AUYAf'></fieldset></dl></div>