Python3 串口模块移植并使用。

  • 想通过 Python去控制串口模块,直接上层就使用一门语言,这样虽然执行效率低一些,但是开发速度加快

  • 通过 buildroot 先移植 Python-serial 模块

      x Symbol: BR2_PACKAGE_PYTHON_SERIAL [=y]                                      x
      x Type  : boolean                                                             x
      x Prompt: python-serial                                                       x
      x   Location:                                                                 x
      x     -> Target packages                                                      x
      x       -> Interpreter languages and scripting                                x
      x         -> External python modules 
  • 代码编写

    // vim serial_test.py
      1 #!/usr/bin/python3
      2
      3 import json
      4 import serial
      5
      6 class serial_port():
      7     __configure_file_path = "serial/config.json"
      8
      9     def __init__(self):
     10         json_data = open(self.__configure_file_path);
     11         self.config = json.load(json_data)
     12
     13     def print_msg(self):
     14         print(self.config)
     15         print(self.config["port"])
     16         print(self.config["baudrate"])
     17         print(self.config["bytesize"])
     18         print(self.config["stopbits"])
     19         print(self.config["parity"])
     20         print(self.config["timeout"])
     21
     22
     23 if __name__ == '__main__':
     24     ser_config = serial_port();
     25     ser_config.print_msg();
     26
     27     ser = serial.Serial(ser_config.config["port"], ser_config.config["baudrate"]    , timeout = ser_config.config["timeout"])
     28
     29     ser.write(("hello").encode());
     30     ser.close();
     31
     32     pass;
    
    // vim config.json
       {
        "port" : "/dev/ttyO1",
        "baudrate" : 115200,
        "timeout" : 0.5,
        "bytesize" : 8,
        "stopbits" : 1,
        "parity" : "N"
    } 

猜你喜欢

转载自www.cnblogs.com/chenfulin5/p/8940124.html