python笔记
来源:互联网 发布:300451 创业软件 编辑:程序博客网 时间:2024/05/16 02:50
各进制之间的转换
http://blog.csdn.net/u012063703/article/details/42609833串口通讯
http://blog.csdn.net/xhao014/article/details/7640568#
显示16进制字符串
串口通讯的一个例子
http://blog.csdn.net/u012063703/article/details/42609833
http://blog.csdn.net/xhao014/article/details/7640568#
Python调用C库
http://www.cricode.com/359.html
chr()、unichr()和ord()
chr()函数用一个范围在range(256)内的(就是0~255)整数作参数,返回一个对应的字符。unichr()跟它一样,只不过返回的是Unicode字符。
ord()函数是chr()函数(对于8位的ASCII字符串)或unichr()函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返回对应的ASCII数值,或者Unicode数值,如果所给的Unicode字符超出了你的Python定义范围,则会引发一个TypeError的异常。
例子:
十六进制字符串中特定位做位移运算
1.str2='\xaa\x55\x06\x01\x04\x00\x00\x00\x00\x03'
2.print (ord(str2[3])&0xff)|((ord(str2[4]) << 8)&0xff00)1
2.print (ord(str2[3])&0xff)|((ord(str2[4]) << 8)&0xff00)1
int类型的10进制转化为两个16进制
1.a=-100
2.print hex((a)&0xff),hex((a >> 8)&0xff)1
2.print hex((a)&0xff),hex((a >> 8)&0xff)1
显示16进制字符串
1.str1='\xaa\x55\x06\x01\x04\x00\x00\x00\x00\x03'
2.def hexShow(argv):
3. result = ''
4. hLen = len(argv)
5. for i in xrange(hLen):
6. hvol = ord(argv[i])
7. hhex = '%02x'%hvol
8. result += hhex+' '
9. print 'hexShow:',result
2.def hexShow(argv):
3. result = ''
4. hLen = len(argv)
5. for i in xrange(hLen):
6. hvol = ord(argv[i])
7. hhex = '%02x'%hvol
8. result += hhex+' '
9. print 'hexShow:',result
10.hexShow(str1)
查找子串在父串中的位置
1.sStr1= 'abcdefg'
2.sStr = 'cde'
4.print sStr1.find(sStr)1
2.sStr = 'cde'
4.print sStr1.find(sStr)1
串口通讯的一个例子
#!/usr/bin/env python
from serial.serialutil import SerialException
from serial import Serial
import thread
import time
import sys, traceback
import os
import binascii
from serial.serialutil import SerialException
from serial import Serial
import thread
import time
import sys, traceback
import os
import binascii
def hexShow(argv):
result = ''
hLen = len(argv)
for i in xrange(hLen):
hvol = ord(argv[i])
hhex = '%02x'%hvol
result += hhex+' '
print 'hexShow:',result
result = ''
hLen = len(argv)
for i in xrange(hLen):
hvol = ord(argv[i])
hhex = '%02x'%hvol
result += hhex+' '
print 'hexShow:',result
class MySerial:
def __init__(self, port="/dev/ttyUSB0", baudrate=115200, timeout=0.5):
self.port = port
self.baudrate = baudrate
self.timeout = timeout
self.encoder_count = 0
self.writeTimeout = timeout
self.interCharTimeout = timeout / 30.
def __init__(self, port="/dev/ttyUSB0", baudrate=115200, timeout=0.5):
self.port = port
self.baudrate = baudrate
self.timeout = timeout
self.encoder_count = 0
self.writeTimeout = timeout
self.interCharTimeout = timeout / 30.
# Keep things thread safe
self.mutex = thread.allocate_lock()
def connect(self):
try:
print "Connecting to MySerial on port", self.port, "..."
self.port = Serial(port=self.port, baudrate=self.baudrate, timeout=self.timeout, writeTimeout=self.writeTimeout)
# The next line is necessary to give the firmware time to wake up.
time.sleep(1)
print "connect success!"
self.mutex = thread.allocate_lock()
def connect(self):
try:
print "Connecting to MySerial on port", self.port, "..."
self.port = Serial(port=self.port, baudrate=self.baudrate, timeout=self.timeout, writeTimeout=self.writeTimeout)
# The next line is necessary to give the firmware time to wake up.
time.sleep(1)
print "connect success!"
except SerialException:
print "Serial Exception:"
print sys.exc_info()
print "Traceback follows:"
traceback.print_exc(file=sys.stdout)
print "Cannot connect to Device!"
os._exit(1)
print "Serial Exception:"
print sys.exc_info()
print "Traceback follows:"
traceback.print_exc(file=sys.stdout)
print "Cannot connect to Device!"
os._exit(1)
def open(self):
''' Open the serial port.
'''
self.port.open()
''' Open the serial port.
'''
self.port.open()
def close(self):
''' Close the serial port.
'''
self.port.close()
''' Close the serial port.
'''
self.port.close()
def send(self):
a=100
str1='\xaa\xbb\xcc\x01\x02\x01\x02\x01\x02\x01\x02'
print hexShow(str3)
self.port.write(str4)
def recv(self):
pack=''
c = self.port.read(16)
a=100
str1='\xaa\xbb\xcc\x01\x02\x01\x02\x01\x02\x01\x02'
print hexShow(str3)
self.port.write(str4)
def recv(self):
pack=''
c = self.port.read(16)
index=c.find('\xaa\xbb')
if (index!=0):
d = self.port.read(index)
pack = c+d
else:
pack = c
if (index!=0):
d = self.port.read(index)
pack = c+d
else:
pack = c
print hexShow(pack)
return (ord(c[index+3])&0xff)|((ord(c[index+4]) << 8)&0xff00), (ord(c[index+5])&0xff)|((ord(c[index+6]) << 8)&0xff00)
return (ord(c[index+3])&0xff)|((ord(c[index+4]) << 8)&0xff00), (ord(c[index+5])&0xff)|((ord(c[index+6]) << 8)&0xff00)
if __name__ == '__main__':
controller = MySerial()
controller.connect()
controller.send()
time.sleep(20)
val1, val2 = controller.recv()
controller.close()
controller = MySerial()
controller.connect()
controller.send()
time.sleep(20)
val1, val2 = controller.recv()
controller.close()
0 0
- Python笔记
- python笔记
- python笔记
- python笔记
- python笔记
- PYTHON笔记
- python笔记
- python笔记
- python笔记
- Python笔记
- Python笔记
- Python笔记
- PYTHON笔记
- Python笔记
- python笔记-------------
- python笔记
- Python笔记
- python 笔记
- 面试之路---进程的互斥和同步
- 运动目标的背景建模-混合高斯背景建模和KNN模型建模的OpenCV代码实现
- 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
- HDOJ 2824 The Euler function (欧拉函数)
- 人生如棋歌词
- python笔记
- 编写一个程序,用于接收用户输入的数,然后显示从0到该数为止表示的所有字符,询问用户是否愿意再次继续同样的过程
- python 抓取ASP.NET平台信息 __VIEWSTATE
- Lucene学习笔记之(六)lucene的中文查询
- TCP的三次握手和四次挥手详解
- 《我的第一本互联网消防书》目录
- GeekBand 第十二周笔记
- springmvc——数据类型转换
- 9.Linux的定时任务