使用python来调试串口
来源:互联网 发布:刀哥swift3.0源码 编辑:程序博客网 时间:2024/05/17 00:17
先贴一个定时发送的代码:
import serialimport timeser = serial.Serial('/dev/ttyUSB0', 250000, timeout=1)print ser.isOpen()words="gggggggggggggggg"while (1): print "send 256x\""+words+"\" to remotes" startTime = time.time() times = 256 while (times): times -= 1 s = ser.write(words) endTime = time.time() print "use time: "+str(endTime-startTime) print "" time.sleep(5)ser.close()
然后是一些其它的方法:
1. 使用序号打开串口:ser = serial.Serial(0) 。but,怎么确定串口的序号???
2. 查看串口的名称,啊哈,用1的方法打开串口后,你可以产看串口的名字:print ser.portstr
3. 先例化一个实体,再打开:
>>> ser = serial.Serial()>>> ser.baudrate = 19200>>> ser.port = 0>>> serSerial<id=0xa81c10, open=False>(port='COM1', baudrate=19200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)>>> ser.open()>>> ser.isOpen()True>>> ser.close()>>> ser.isOpen()False
4. 读取数据的集中方式
>>> ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)>>> x = ser.read() # read one byte>>> s = ser.read(10) # read up to ten bytes (timeout)>>> line = ser.readline() # read a '/n' terminated line>>> ser.close()
其中,如果只是串口调试,直接ser.read(1000),这样会把读到的值直接打印到屏幕上。
5.所有参数
ser = serial.Serial(port=None, # number of device, numbering starts at# zero. if everything fails, the user# can specify a device string, note# that this isn't portable anymore# if no port is specified an unconfigured# an closed serial port object is createdbaudrate=9600, # baud ratebytesize=EIGHTBITS, # number of databitsparity=PARITY_NONE, # enable parity checkingstopbits=STOPBITS_ONE, # number of stopbitstimeout=None, # set a timeout value, None for waiting foreverxonxoff=0, # enable software flow controlrtscts=0, # enable RTS/CTS flow controlinterCharTimeout=None # Inter-character timeout, None to disable)
6. exception:serial.SerialException
另一个完整收发的例子,单片机数据以TLV(Type,Length,Value)格式发上来
#!/usr/bin/env python
# it's a program of luo, piedgogo@sina.com
import serialimport arrayimport osimport signalfrom time import sleepflag_stop = Falsedef onsignal_int(a,b): print "sigint!" global flag_stop flag_stop = Truesignal.signal(signal.SIGINT, onsignal_int)
signal.signal(signal.SIGTERM, onsignal_int)ser = serial.Serial('/dev/ttyUSB0', 9600, timeout = 0.001)print "serial.isOpen() =",ser.isOpen()cmd_send = "7b02000129cc00c80378290400640000cc7d0d0a"cmd_send = cmd_send.decode("hex")stop = "7b04047d0d0a"stop = stop.decode("hex")cmd_back = ""cmd_length = 0x00cmd_count = 0x00s = ser.write(cmd_send)while True: sleep(0.1) if flag_stop: # read data until Ctrl+c ser.write(stop) # send cmd stop before exit print "reset cmd has been sent!" sleep(0.05) break text = ser.read(1) # read one, with timout if text: # check if not timeout n = ser.inWaiting() # look if there is more to read if n: text = text + ser.read(n) #get it cmd_back = cmd_back + text text = "" if len(cmd_back) < 2: # go back if no enough data recvd continue if cmd_length == 0x00: # at the begin of new cmd cmd_length = ord(cmd_back[1]) # Type(1 byte),Length of Value(1 byte),Value print "new cmd length,",cmd_length if (cmd_length + 0x02) > len(cmd_back): # go back befor this cmd end continue # so far, we have got a full cmd hex_list = [hex(ord(i)) for i in cmd_back] # more readable than data.encode("hex") print "In buffer:",hex_list cmd_back = cmd_back[cmd_length+2:] # remove this cmd(TLV) from buffer cmd_length = 0 cmd_count += 1 print "==> %d cmds recvd."%(cmd_count) print "-------------" ser.close()
0 0
- 使用python来调试串口
- 使用python来调试串口
- 使用python来调试串口
- Python使用线程来接收串口数据
- 关于WINDOWS超级终端的使用来调试MODEM,串口.
- 解放串口调试(使用JLink SEGGER RTT来打印和输入调试信息)
- putty 串口调试使用
- 使用minicom调试串口
- 使用adb调试串口
- 使用VMware捕获Linux的串口输出来调试内核的Oops
- 使用scite和pdb来调试python程序
- 如何使用linux下gdb来调试python程序
- 在 Python 中使用 GDB 来调试 转载
- 在 Python 中使用 GDB 来调试 转载
- real6410调试串口作为普通串口使用
- S5P4418调试串口做为普通串口使用
- 使用python操作串口
- 使用python操作串口
- AutoLayout (自动布局)
- 怎样找到一个view所在的控制器(viewController)
- 数据库
- GCD 线程
- 【五校联考四】总结
- 使用python来调试串口
- POJ 3904 Sky Code(素因子分解+容斥)
- c++ primer 学习之路 指针的引用
- information retrieval
- 剑指offer—字符串的排列
- A015-布局之LinearLayout
- 关于系统运维监控规范的几点建议
- hdoj 2012 素数判定
- 天地图专题二:在天地图上循环显示标注点以及悬停显示信息窗口