python串口通信_Python 串口通信的实现

串口通信是指外设和计算机间,通过数据信号线 、地线、控制线等,按位进行传输数据的一种通讯方式。这种通信方式使用的数据线少,在远距离通信中可以节约通信成本,但其传输速度比并行传输低。串口是计算机上一种非常通用的设备通信协议。pyserial模块封装了python对串口的访问,为多平台的使用提供了统一的接口。

安装:

1

pip3install pyserial

测试:

两个CH340 (TTL转串口模块)接入到PC串口上,通过Python进行数据交互:

1-20093000140H45.png

简单串口程序实现:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

import serial#导入模块

try:

#端口,GNU / Linux上的/ dev / ttyUSB0 等 或 Windows上的 COM3 等

portx="COM3"

#波特率,标准值之一:50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200

bps=115200

#超时设置,None:永远等待操作,0为立即返回请求结果,其他值为等待超时时间(单位为秒)

timex=5

# 打开串口,并得到串口对象

ser=serial.Serial(portx,bps,timeout=timex)

# 写数据

result=ser.write("我是东小东".encode("gbk"))

print("写总字节数:",result)

ser.close()#关闭串口

except Exception as e:

print("---异常---:",e)

获取可用串口列表:

1

2

3

4

5

6

7

8

9

10

import serial#导入模块

import serial.tools.list_ports

port_list= list(serial.tools.list_ports.comports())

print(port_list)

if len(port_list)== 0:

print('无可用串口')

else:

for iin range(0,len(port_list)):

print(port_list[i])

十六进制处理:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

import serial#导入模块

try:

portx="COM3"

bps=115200

#超时设置,None:永远等待操作,0为立即返回请求结果,其他值为等待超时时间(单位为秒)

timex=None

ser=serial.Serial(portx,bps,timeout=timex)

print("串口详情参数:", ser)

#十六进制的发送

result=ser.write(chr(0x06).encode("utf-8"))#写数据

print("写总字节数:",result)

#十六进制的读取

print(ser.read().hex())#读一个字节

print("---------------")

ser.close()#关闭串口

except Exception as e:

print("---异常---:",e)

其他细节补充:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

import serial#导入模块

try:

#端口,GNU / Linux上的/ dev / ttyUSB0 等 或 Windows上的 COM3 等

portx="COM3"

#波特率,标准值之一:50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200

bps=115200

#超时设置,None:永远等待操作,0为立即返回请求结果,其他值为等待超时时间(单位为秒)

timex=5

# 打开串口,并得到串口对象

ser=serial.Serial(portx,bps,timeout=timex)

print("串口详情参数:", ser)

print(ser.port)#获取到当前打开的串口名

print(ser.baudrate)#获取波特率

result=ser.write("我是东小东".encode("gbk"))#写数据

print("写总字节数:",result)

#print(ser.read())#读一个字节

# print(ser.read(10).decode("gbk"))#读十个字节

#print(ser.readline().decode("gbk"))#读一行

#print(ser.readlines())#读取多行,返回列表,必须匹配超时(timeout)使用

#print(ser.in_waiting)#获取输入缓冲区的剩余字节数

#print(ser.out_waiting)#获取输出缓冲区的字节数

#循环接收数据,此为死循环,可用线程实现

while True:

if ser.in_waiting:

str=ser.read(ser.in_waiting ).decode("gbk")

if(str=="exit"):#退出标志

break

else:

print("收到数据:",str)

print("---------------")

ser.close()#关闭串口

except Exception as e:

print("---异常---:",e)

部分封装:

其中读数据的封装方法并不是很好用,使用的话又得循环接收,这样反而更加复杂了

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

import serial#导入模块

import threading

STRGLO=""#读取的数据

BOOL=True #读取标志位

#读数代码本体实现

def ReadData(ser):

global STRGLO,BOOL

# 循环接收数据,此为死循环,可用线程实现

while BOOL:

if ser.in_waiting:

STRGLO= ser.read(ser.in_waiting).decode("gbk")

print(STRGLO)

#打开串口

# 端口,GNU / Linux上的/ dev / ttyUSB0 等 或 Windows上的 COM3 等

# 波特率,标准值之一:50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200

# 超时设置,None:永远等待操作,0为立即返回请求结果,其他值为等待超时时间(单位为秒)

def DOpenPort(portx,bps,timeout):

ret=False

try:

# 打开串口,并得到串口对象

ser= serial.Serial(portx, bps, timeout=timeout)

#判断是否打开成功

if(ser.is_open):

ret=True

threading.Thread(target=ReadData, args=(ser,)).start()

except Exception as e:

print("---异常---:", e)

return ser,ret

#关闭串口

def DColsePort(ser):

global BOOL

BOOL=False

ser.close()

#写数据

def DWritePort(ser,text):

result= ser.write(text.encode("gbk"))# 写数据

return result

#读数据

def DReadPort():

global STRGLO

str=STRGLO

STRGLO=""#清空当次读取

return str

if __name__=="__main__":

ser,ret=DOpenPort("COM6",115200,None)

if(ret==True):#判断串口是否成功打开

count=DWritePort(ser,"我是东小东,哈哈")

print("写入字节数:",count)

#DReadPort() #读串口数据

#DColsePort(ser) #关闭串口

查看所有串口

1

2

3

4

5

6

7

import serial.tools.list_ports

port_list= list(serial.tools.list_ports.comports())

if len(port_list)== 0:

print('找不到串口')

else:

for iin range(0,len(port_list)):

print(port_list[i])

参考:

以上就是Python 串口通信的实现的详细内容,更多关于python 串口通信的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/dongxiaodong/p/9992083.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值