简体   繁体   English

如何在python中获取单个pyserial字节的整数值

[英]how to get the integer value of a single pyserial byte in python

I'm using pyserial in python 2.7.5 which according to the docs : 我在python 2.7.5中使用pyserial,根据文档

read(size=1) Parameters: size – Number of bytes to read. read(size = 1)参数:size - 要读取的字节数。 Returns: 返回:
Bytes read from the port. 从端口读取的字节数。 Read size bytes from the serial port. 从串口读取大小字节。 If a timeout is set it may return less characters as requested. 如果设置了超时,则可能会根据请求返回较少的字符。 With no timeout it will block until the requested number of bytes is read. 没有超时,它将阻塞,直到读取所请求的字节数。

Changed in version 2.5: Returns an instance of bytes when available (Python 2.6 and newer) and str otherwise. 在版本2.5中更改:返回可用时的字节实例(Python 2.6和更新版本),否则返回str。

Mostly I want to use the values as hex values and so when I use them I use the following code: 大多数情况下,我想将值用作十六进制值,因此当我使用它们时,我使用以下代码:

ch = ser.read()
print ch.encode('hex')

This works no problem. 这没问题。

But now I'm trying to read just ONE value as an integer, because it's read in as a string from serial.read, I'm encountering error after error as I try to get an integer value. 但是现在我试图将一个值作为整数读取,因为它是从serial.read读入的一个字符串,我在尝试获取整数值时遇到错误。

For example: 例如:

print ch

prints nothing because it's an invisible character (in this case chr(0x02)). 什么都不打印,因为它是一个不可见的字符(在这种情况下是chr(0x02))。

print int(ch)

raises an error 引发错误

ValueError: invalid literal for int() with base 10: '\x02'

trying print int(ch,16) , ch.decode() , ch.encode('dec') , ord(ch) , unichr(ch) all give errors (or nothing). 尝试print int(ch,16)ch.decode()ch.encode('dec')ord(ch)unichr(ch)都给出错误(或什么都没有)。

In fact, the only way I have got it to work is converting it to hex, and then back to an integer: 事实上,我让它工作的唯一方法是将其转换为十六进制,然后再转换为整数:

print int(ch.encode('hex'),16)

this returns the expected 2 , but I know I am doing it the wrong way. 这会返回预期的2 ,但我知道我这样做是错误的。 How do I convert aa chr(0x02) value to a 2 more simply? 如何将aa chr(0x02)值更简单地转换为2?

Believe me, I have searched and am finding ways to do this in python 3, and work-arounds using imported modules. 相信我,我已经搜索过,并且正在寻找在python 3中实现此目的的方法,以及使用导入模块的解决方法。 Is there a native way to do this without resorting to importing something to get one value? 是否有一种本地方式来做到这一点,而不需要导入一些东西来获得一个值?

edit: I have tried ord(ch) but it is returning 90 and I KNOW the value is 2 , 1) because that's what I'm expecting, and 2) because when I get an error, it tells me (as above) 编辑:我已经尝试过ord(ch)但是它返回90并且我知道值是2 ,因为那是我期待的,2)因为当我得到一个错误时,它告诉我(如上所述)

Here is the code I am using that generates 90 这是我使用的代码生成90

count = ser.read(1)
print "count:",ord(ch)

the output is count: 90 输出count: 90

and as soon as I cut and pasted that code above I saw the error count != ch !!!! 当我剪切并粘贴上面的代码时,我看到了错误count != ch !!!!

Thanks 谢谢

Use the ord function. 使用ord功能。 What you have in your input is a chr(2) (which, as a constant, can also be expressed as '\\x02'). 您输入的内容是chr(2) (作为常量,也可以表示为'\\ x02')。

i= ord( chr(2) )
i= ord( '\x02' )

would both store the integer 2 in variable i . 都将整数2存储在变量i

I think you want to use ord() 我想你想用ord()

ch = '\x02'
print ord(ch)
=> 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM