python笔记

来源:互联网 发布:300451 创业软件 编辑:程序博客网 时间:2024/05/16 02:50
各进制之间的转换
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



    int类型的10进制转化为两个16进制

    1.a=-100
    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

    10.hexShow(str1)



    查找子串在父串中的位置

    1.sStr1= 'abcdefg'  
    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

    def hexShow(argv): 
        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.

            # 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!"

            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)

        def open(self):
            ''' Open the serial port.
            '''
            self.port.open()

        def close(self):
            ''' 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)

            index=c.find('\xaa\xbb')
            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)

    if __name__ == '__main__':
          controller = MySerial()
          controller.connect()
          controller.send()
          time.sleep(20)
          val1, val2 = controller.recv()
          controller.close()

  • 0 0
    原创粉丝点击