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

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

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

        Python:通过串口从 Velleman k8090 读取数据

        Python: Read data via serial port from Velleman k8090(Python:通过串口从 Velleman k8090 读取数据)
          <tfoot id='l5wE6'></tfoot>
            <tbody id='l5wE6'></tbody>

        • <small id='l5wE6'></small><noframes id='l5wE6'>

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

                  本文介绍了Python:通过串口从 Velleman k8090 读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一块 Velleman k8090 继电器板,我试图从中读取一些数据.我可以很好地写到板上,但是每当我输出读取的数据时,我都会得到奇怪的字符,比如菱形或倒置的问号.这是我的代码的一部分:

                  I have a Velleman k8090 Relay Board from which I am trying to read some data. I can write to the board fine, but whenever I output the read data, I get strange characters like a diamond or an upside-down question mark. Here is part of my code:

                  import serial
                  COM_PORT = 'COM4'
                  class Velleman8090:
                      def __init__(self, port=COM_PORT):
                          self.port = port
                          self.baud_rate = 19200
                          self.data_bits = 8
                          self.parity = 'N'
                          self.stop_bits = 1
                          self.flow_control = 'N'
                  
                      def open_device(self):
                          self.talk = serial.Serial(self.port, self.baud_rate, self.data_bits, self.parity, self.stop_bits)
                  
                      def firmware_version(self):
                          data = packet(0x71, 0x00, 0x00, 0x00)
                          self.talk.write(data)
                          print self.talk.read()
                  
                      def close_device(self):
                          self.talk.close()
                  
                  def chksum(cmd,msk,p1,p2):
                      return (((~(0x04 + cmd + msk + p1 + p2)) + 0x01) & 0xff)
                  
                  def packet(cmd,msk,p1,p2):
                      return str(bytearray([0x04, cmd, msk, p1, p2, chksum(cmd, msk, p1, p2), 0x0f]))
                  
                  def main():
                      vm8090 = Velleman8090()
                      vm8090.open_device()        
                      vm8090.firmware_version()
                      vm8090.close_device()
                  

                  为什么我会得到这些奇怪的字符?文档指出它应该返回年份和星期.

                  Why am I getting these strange characters? The documentation states that it should return the year and the week.

                  经过一些搜索和故障排除后,我现在很确定它与 Python 代码或数据类型有关,而不是硬件.这个论坛包含一个从板读取的 C 示例.我从板上正确读取了该示例,因此我相当确定它与 Python 代码有关,而不是硬件本身.

                  After some searching and troubleshooting, I am now pretty sure it has something to do with the Python code or data types and not the hardware. This forum contains an example in C which reads from the board. I got that example to properly read from the board so I am fairly sure it has something to do with the Python code and not the hardware itself.

                  推荐答案

                  经过一番努力,终于找到了问题所在.这是一个 Python 编码问题.输出数据实际上被编码为我得到的奇怪字符.只需要一行代码就可以得到我期望的输出:

                  After much effort, I finally found the issue. It was a Python encoding issue. The output data was actually being encoded to the strange characters I was getting. Only one line of code was needed to get the output I was expecting:

                   output = self.talk.read(7)
                   output = output.encode('hex')  #converts the strange characters to hex
                   print output
                  

                  希望这将在未来对其他人有所帮助.

                  Hopefully this will help some others in the future.

                  这篇关于Python:通过串口从 Velleman k8090 读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                1. <tfoot id='dVFtA'></tfoot>
                      • <bdo id='dVFtA'></bdo><ul id='dVFtA'></ul>

                          <tbody id='dVFtA'></tbody>

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

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