问题描述
我有一块 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:
为什么我会得到这些奇怪的字符?文档指出它应该返回年份和星期.
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:
希望这将在未来对其他人有所帮助.
Hopefully this will help some others in the future.
这篇关于Python:通过串口从 Velleman k8090 读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!