从UART挣扎的阅读数据
我刚刚开始在Python进行编程,但是我已经介绍了我的第一期,那就是:
uart.readline()
我正在通过UART发送多条数据以控制家里的LED灯,所以我的想法是发送类似的内容。 “,“ G255”,“ B255”,“ BR5”。要发送的数据的格式对我来说并不重要,重要的是要在0-255之间获得红色,绿色和蓝色值,而亮度值在1-10和某种程度上以某种方式解码并处理Python中的数据。顺便提一句。我正在从我的Android应用程序中发送数据,格式和该目的的所有内容都可以。
但要解决问题。我的代码如下:
uart = UART(0,9600)
while True:
if uart.any():
data = uart.readline()
print(data)
这是为红色值返回这样的东西:
b'r'
b'2'
b'5'
b'5'
对我来说很好,我理解为什么会发生这种情况。我可能可以弄清楚如何将数据放在一起,以便它们变得有意义并与它们一起工作,但是问题是每行实际上都在经历另一个周期,因此我在同时循环中实际上无法进行任何操作以将数据集中在一起,还是可以?
如果我尝试这样的事情:
while True:
if uart.any():
data = uart.readline()
data2 = uart.readline()
data3 = uart.readline()
print(data)
print(data2)
print(data3)
我得到:
b'r'
None
None
解决这个问题的方法是什么?我想念一些很明显的东西吗?为什么我不能在该循环中阅读多个线路?为什么循环总是必须重复阅读另一行?我该如何处理以将数据聚集在一起?我已经尝试了uart.read(),但它总是返回其他内容,例如b'red2'b'55'或b'red255'或b'red'b'255',所以它更加混乱,对我来说太多了那里发生了什么。
I've started programming in Python just recently but I've come over my first issue and that is:
uart.readline()
I'm sending multiple lines of data over UART to control my LED lights at home, so my idea is to send something like "r255", "g255", "b255", "br5". The format of the data being sent is not important to me, important is to get red, green, and blue values between 0-255 and a brightness value between 1-10 and somehow decode and process the data in Python. Btw. I'm sending the data from my Android App, the formatting and everything on that end should be fine.
But to the problem. I have a code as follows:
uart = UART(0,9600)
while True:
if uart.any():
data = uart.readline()
print(data)
This returns something like this for the red value:
b'r'
b'2'
b'5'
b'5'
That's fine to me and I understand why that is happening. I can probably work out how to put the data together so they make sense and work with them but the problem is that each line is actually going through another cycle so I can't really do any operations in the while loop to get the data together, or can I?
If I try something like this:
while True:
if uart.any():
data = uart.readline()
data2 = uart.readline()
data3 = uart.readline()
print(data)
print(data2)
print(data3)
I get:
b'r'
None
None
What is a solution to this problem? Am I missing something really obvious? Why can't I read more than one line in that loop? Why does the loop always have to repeat to read another line? How do I approach this in order to get the data together? I've tried uart.read() but it always returns something else like b'red2' b'55' or b'red255' or b'red' b'255' so it's even more mess and too much for me to understand what's happening there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于明显的原因,我无法对此进行测试,但这应该纠正在每个
uart.any()
uart.rart.readline()之前不调用uart.any()
。我定义了一个单独的功能来执行此操作并多次调用。I can't test this for obvious reasons, but this should correct the problem of not calling
uart.any()
before eachuart.readline()
. I defined a separate function to do that and call it multiple times.