Pyserial发送多个字节 [英] Pyserial Sending More than One Byte

查看:319
本文介绍了Pyserial发送多个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次张贴.

在我开始之前,我只想说我是一个初学者,所以请多多包涵,但我仍然可以很好地遵循.

我有一个称为Pololu Wixel的无线设备,可以无线发送和接收数据.我正在使用其中的两个.一发送和一接收.它是USB,因此可以直接插入我的Raspberry Pi或PC,因此我所要做的就是通过一个终端连接到COM端口以对其进行读写数据.它带有一个测试终端程序,该程序可以让我发送1-16字节的信息.我已经做到了,并且已经毫无问题地发送和接收了2个字节(这是我所需要的).

现在这是我的问题:当我打开Ubuntu终端并使用Pyserial连接到正确的发送Wixel COM端口并写入一个大于255的值时,我的接收COM端口也连接到了另一个Terminal实例, Pyserial,无法读取正确的值,因此,我认为我无法读取和写入两个字节,而只能读取和写入一个字节.在pyserial文档中进行在线阅读之后,我相信,不知道,Pyserial一次只能读取和写入5、6、7或8位.

我希望我的问题现在很明显.我该如何才能将2字节的信息写入我的设备的COM端口,然后将其发送到需要通过pyserial读取这2字节的另一设备?

我希望一切都有意义,我将非常感谢您的帮助.

谢谢

更新 好吧,我想我已经知道了.所以我做到了:

import serial

s=serial.Serial(3) //device #1 at COM Port 4 (sending)
r=serial.Serial(4) //device #4 at COM Port 5 (receiving)

s.timeout=1
r.timeout=1

s.write('0x80')
r.readline()
  //Output said: '0x80'

s.write('hh')
r.readline()
  //Output said: 'hh'

老实说,我认为这解决了我的问题.也许从一开始就没有问题.也许我可以从程序中获取我的16位二进制数据,例如"1101101011010101",然后将其转换为字符(在我以为是之前我已经看过一种叫做char()的东西)

然后使用s.write('WHATEVER') 然后使用r.readline()并转换回二进制

解决方案

您可能需要将数字分成多个字节,然后按小端或大端顺序发送.

EG:

low_byte = number % 256
high_byte = number // 256

那应该使您的数量达到65535.您可以使用high_byte * 256 + low_byte在另一侧重建数字.

First time poster.

Before I start, I just want to say that I am a beginner programmer so bear with me, but I can still follow along quite well.

I have a wireless device called a Pololu Wixel, which can send and receive data wirelessly. I'm using two of them. One to send and one to receive. It's USB so it can plug straight into my Raspberry Pi or PC, so all I have to do is connect to the COM port through a terminal to read and write data to it. It comes with a testing terminal program that allows me to send 1-16 bytes of info. I've done this and I've sent and received 2 bytes (which is what I need) with no problem.

Now here's my problem: When I open up the Ubuntu terminal and use Pyserial to connect to the correct sending Wixel COM Port and write a value larger than 255, my receiving COM port, which is connected to another instance of Terminal also using Pyserial, doesn't read the right value, hence I think I'm not being able to read and write two bytes, but only one. After doing something reading online in the pyserial documentation, I believe, not know, that Pyserial can only read and write 5,6,7, or 8 bits at a time.

I hope my problem is obvious now. How the heck can I write 2 bytes worth of info to the COM port to my device and send it to the other device which needs to read those 2 bytes through, all using pyserial?

I hope this all makes sense, and I would greatly appreciate any help.

Thanks

UPDATE Okay, I think I've got this going now. So I did:

import serial

s=serial.Serial(3) //device #1 at COM Port 4 (sending)
r=serial.Serial(4) //device #4 at COM Port 5 (receiving)

s.timeout=1
r.timeout=1

s.write('0x80')
r.readline()
  //Output said: '0x80'

s.write('hh')
r.readline()
  //Output said: 'hh'

Honestly, I think this solves my problem. Maybe there never was a problem to begin with. Maybe I can take my 16bit binary data from the program, example "1101101011010101", turn it into characters (I've seen something called char() before I think that's it)

then use s.write('WHATEVER') then use r.readline() and convert back to binary

解决方案

You'll likely need to pull your number apart into multiple bytes, and send the pieces in little endian or big endian order.

EG:

low_byte = number % 256
high_byte = number // 256

That should get you up to 65535. You can reconstruct the number on the other side with high_byte * 256 + low_byte.

这篇关于Pyserial发送多个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆