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"
    } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值