嵌入式Linux环境下python编程实践之pyserial初步

13 篇文章 1 订阅
2 篇文章 0 订阅

import serial

import serial
import time
Com = 'com9'
Baudrate = 38400
Parity = serial.PARITY_EVEN
Timeout = 0.5
#Parity = serial.PARITY_NONE
Bytesize = serial.EIGHTBITS
serialFd = serial.Serial(port = Com,baudrate=Baudrate,parity=Parity)
print(serialFd.name)
print(serialFd.port)
print(serialFd.baudrate)
print(serialFd.bytesize)
print(serialFd.parity)
print(serialFd.stopbits)
print(serialFd.timeout)
#可以通过这样实现读取和定义波特率,停止位等等

PC端程序

import serial
import time
import threading

lock = threading.Lock()
Com = 'com9'
Baudrate = 38400
Parity = serial.PARITY_EVEN
Timeout = 0.5
#Parity = serial.PARITY_NONE
Bytesize = serial.EIGHTBITS
serialFd = serial.Serial(port = Com,baudrate=Baudrate,parity=Parity)

def ReadUART():
    try:
        while True:
            count = serialFd.inWaiting()
            if count >0:
                lock.acquire()
                data = serialFd.read(count)#.decode(encoding='ascii')
                print("receive data is",data)
                print("the number of receive %d" % count)
                lock.release()
    except KeyboardInterrupt:
        if serial != None:
            serial.close()

def WriteUART(mm):
    try:
        serialFd.write(mm.encode('utf-8'))
        #serialFd.write(mm)
        time.sleep(0.5)
        print("write is ", mm, type(mm.encode('utf-8')))
    except:
        print("can't open the port")

ReadUARTThread = threading.Thread(target=ReadUART)
ReadUARTThread.setDaemon(True)  #设置为守护线程   在结束后立马结束
ReadUARTThread.start()
i = 0
while i<20:
    mm = 'lcy'
    WriteUARTThread = threading.Thread(target = WriteUART,args=(mm,))
    WriteUARTThread.start()
    time.sleep(0.5)
    i += 1
print('-'*10)


ARM端程序:

import serial
import time
import threading
lock = threading.Lock()
Com = '/dev/ttymxc4'
#Com = 'com9'
Baudrate = 38400
Parity = serial.PARITY_EVEN
Timeout = 0.5
#Parity = serial.PARITY_NONE
Bytesize = serial.EIGHTBITS
serialFd = serial.Serial(port = Com,baudrate=Baudrate,parity=Parity)

def ReadUART():
    try:
        while True:
            count = serialFd.inWaiting()
            if count >0:
                lock.acquire()
                data = serialFd.read(count)#.decode(encoding='ascii')
                print("receive data is",data)
                print("the number of receive %d" % count)
                try:
                    WriteUART(data.decode(encoding='utf-8'))
                except Exception as e:
                    print(e)
                lock.release()
    except KeyboardInterrupt:
        if serial != None:
            serial.close()

def WriteUART(mm):
    try:
        serialFd.write(mm.encode('utf-8'))
        #serialFd.write(mm)
        time.sleep(0.5)
        print("write is ", mm, type(mm.encode('utf-8')))
    except:
        print("can't open the port")
#WriteUARTThread = threading.Thread(target=WriteUART,args=())
ReadUARTThread = threading.Thread(target=ReadUART)
#ReadUARTThread.setDaemon(True)  #设置为守护线程   在结束后立马结束
ReadUARTThread.start()

print('-'*10)

运行时需要先运行ARM端的程序,一直在读,当读到PC端发来字符时就将字符转回去,原理很简单

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值