串口模块的波特率比较特别,找了几个串口工具都不支持。。。所以,干脆用python自己来写了,其实已经好奇好久了,别人的工具各种不顺手。

需要pyserial的支持,兼容各种平台,不需要新编译二进制文件。

先贴一个定时发送的代码:

import serial
import time ser = 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
>>> ser
Serial<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

好多人问 windows 下面提示设备不存在!亲,windows 下面的串口设备名不一样的啊,上面的代码里面 COM1 就是 windows 下面的设备。

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 created
baudrate=9600, # baud rate
bytesize=EIGHTBITS, # number of databits
parity=PARITY_NONE, # enable parity checking
stopbits=STOPBITS_ONE, # number of stopbits
timeout=None, # set a timeout value, None for waiting forever
xonxoff=0, # enable software flow control
rtscts=0, # enable RTS/CTS flow control
interCharTimeout=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 serial
import array
import os
import signal
from time import sleep flag_stop = False def onsignal_int(a,b):
print "sigint!"
global flag_stop
flag_stop = True signal.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 = 0x00
cmd_count = 0x00 s = 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: # new loop
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): # do nothing until all bytes is recvd
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()

可用的串口列表,可以用下面的方法获取:

python -c "import serial.tools.list_ports;print [port for port in serial.tools.list_ports.comports() if port[2] != 'n/a']"

因为获取的列表里可能有“假冒”的,所以,上面的代码还检查了设备的 hwid。comports() 返回的是一个 list,元素是一个对象,查看其 type() 结果如下 :

type(port)

class 'serial.tools.list_ports_linux.SysFS'

查看文档 pydoc serial.tools.list_ports_linux.SysFS,上面的 port[2] 是这个 port 的 hwid,另外还有 port[0] 是串口名称,port[1]是 description 。

使用python来调试串口的更多相关文章

  1. imx6 关闭调试串口

    需要关闭imx6调试串口,用作普通的串口使用. 参考链接 http://blog.csdn.net/neiloid/article/details/7585876 http://www.cnblogs ...

  2. python基础——调试

    python基础——调试 程序能一次写完并正常运行的概率很小,基本不超过1%.总会有各种各样的bug需要修正.有的bug很简单,看看错误信息就知道,有的bug很复杂,我们需要知道出错时,哪些变量的值是 ...

  3. am335x 更改调试串口

    /********************************************************************* * am335x 更改调试串口 * * am335x的调试 ...

  4. 将JZ2440的调试串口换成com2

    众所周知JZ2440 V3很小巧,精致.今天单就JZ2440的串口来讨论一些问题.我们在用串口进行调试的时候,需要用JZ2440自带的一根USB线连接电脑USB口和开发板的USB-com1口.先来看一 ...

  5. 在ubunut下使用pycharm和eclipse进行python远程调试

    我比较喜欢Pycharm,因为这个是JetBrains公司出的python IDE工具,该公司下的java IDE工具--IDEA,无论从界面还是操作上都甩eclipse几条街,但项目组里有些人使用e ...

  6. FAQ:Python 断点调试

    Python程序调试:断点调试是必须有的功能,以Pycharm开发工具为例: 一.理论知识: 1. step into(F7)就是单步执行,遇到子函数就进入并且继续单步执行: 2  step over ...

  7. python pdb调试以及sublime3快捷键设置

    python pdb调试以及sublime3快捷键设置 pdb调试 如果对gdb比较熟悉的话,pdb就很容易上手.以一个demo快速了解常用的调试命令. def test(a): while True ...

  8. python的调试

    调试 程序能一次写完并正常执行的概率很小.总会有各种各样的bug需要修正. 有的bug很简单,看看错误信息就知道,有的bug很复杂,我们需要知道出错时 哪些变量的值是正确的,哪些变量的值是错误的,因此 ...

  9. python --端点调试

    python端点调试 左边三角:快速跳到下一个端点 下箭头:单不调试 斜向下箭头:跳到函数内部执行代码

随机推荐

  1. Android Studio系列教程六--Gradle多渠道打包

    Android Studio系列教程六--Gradle多渠道打包 2015 年 01 月 15 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处!http://stormzh ...

  2. 观察器observes与对象初始化

    Demo.Person2 = Ember.Object.extend({ init: function() { alert('lljsd'); this.set('salutation', " ...

  3. javascript中的队列结构

    1.概念 队列和栈结构不同,栈是一种后进先出的结构,而队列是一种先进先出的结构.队列也是一种表结构,不同的是队列只能在队尾插入元素,在队首删除元素,可以将队列想象成一个在超时等待排队付钱的队伍,或者在 ...

  4. RDLC系列之一 简单示例

    参照文章:http://www.cnblogs.com/waxdoll/archive/2006/07/24/458409.html#!comments 一.效果图

  5. 你会在C#的类库中添加web service引用吗?

    本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...

  6. 【转】【MySQL】SQLSTATE详解

    根据 X/Open 和 SQL Access Group SQL CAE 规范 (1992) 所进行的定义,SQLERROR 返回 SQLSTATE 值.SQLSTATE 值是包含五个字符的字符串 . ...

  7. [tools]google神器浏览器下载

    google神器下载 这是一款优化了的google浏览器 http://www.ccav1.me/chromegae.html

  8. Java compiler level does not match the version of the installed Java project facet.(转)

    Java compiler level does not match解决方法 从别的地方导入一个项目的时候,经常会遇到eclipse/Myeclipse报Description  Resource P ...

  9. sys.stdin的三种方式

    1. for line in sys.stdin: import sys sys.stdout.write('根据两点坐标计算直线斜率k,截距b:\n') for line in sys.stdin: ...

  10. 基于IHttpAsyncHandler的实时大文件传送器

    在日常工作中,有时候需要到远程服务器上部署新版本的系统,由于远程服务器出于外网,所以每次都要开QQ连接,非常麻烦.索性就研究了下IHttpasyncHandler,并结合Juqery Progress ...