Python導入整個模塊,python串口模塊找不到_有沒有python的串口庫

 2023-12-10 阅读 29 评论 0

摘要:展開全部串口模塊的波特率比較32313133353236313431303231363533e4b893e5b19e31333363396463特別,找了幾個串口工具都不支持。。。所以,干脆用python自己來寫了,其實已經好奇好久了,別人的工具各種不順手。需要pyserial的支持,兼容各種平臺

展開全部

串口模塊的波特率比較32313133353236313431303231363533e4b893e5b19e31333363396463特別,找了幾個串口工具都不支持。。。所以,干脆用python自己來寫了,其實已經好奇好久了,別人的工具各種不順手。

需要pyserial的支持,兼容各種平臺,不需要新編譯二進制文件。

Python導入整個模塊。先貼一個定時發送的代碼:

import serialimport time

ser = serial.Serial('/dev/ttyUSB0', 250000, timeout=1)print ser.isOpen()

words="gggggggggggggggg"while (1): ? ? ? ?print "send 256x\""+words+"\" to remotes"

python通過串口控制單片機?startTime = time.time()

times = 256 ? ? ? ?while (times):

times -= 1

s = ser.write(words)

python找不到指定模塊怎么辦?endTime = time.time() ? ? ? ?print "use time: "+str(endTime-startTime) ? ? ? ?print ""

time.sleep(5)

ser.close()

然后是一些其它的方法:

pycharm找不到模塊,1. 使用序號打開串口:ser?=?serial.Serial(0) 。but,怎么確定串口的序號???

2. 查看串口的名稱,啊哈,用1的方法打開串口后,你可以產看串口的名字:print?ser.portstr

3. 先例化一個實體,再打開:

>>> ser = serial.Serial()>>> ser.baudrate = 19200

python串口數據采集并發送,>>> ser.port = 0>>> ser

Serial(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 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 sleep

flag_stop = Falsedef 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 = 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: ? ? ? ? ? ? ? ? ? ? ? ?# 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()

——————

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/194256.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息