0
我正在使用PySerial連接到Threaded連接。這是我簡單的第一個類繼承線程和運行串行:獲取/設置串行類屬性
from threading import Thread
import serial, inspect
class Serial1(Thread):
def __init__(self,main_queue,port = "COM1")
self.ser = serial.Serial()
self.queue = main_queue
self.port = port
def open(self):
self.ser.port = self.port
if self.ser.isOpen():
self.ser.close()
self.ser.open()
Thread.__init__(self)
self.start()
在那之後我又同級Serial2繼承主題,並使用接口Serial1:
from Queue import Queue
from threading import Thread
import serial1
class Serial2(Thread):
def __init__(self,main_queue,port = "COM2")
self.ser = serial1.Serial1()
self.queue = main_queue
self.port = port
def open(self):
self.ser = serial1.Serial1(self.queue, self.port)
if self.ser.open() == True:
Thread.__init__(self)
if self.isAlive() == False:
self.start()
做完這一切,我怎麼設置/在串口1和串口2上獲得串口的端口屬性?假設我使用接口Serial1和Serial2在腳本:
from Queue import Queue
import serial2
import serial1
q1 = Queue(maxsize = 1)
q2 = Queue(maxsize = 1)
s1 = serial1.Serial1(q1, "COM4")
s2 = serial2.Serial2(q2, "COM2")
#get port value from Serial1 and Serial2
port1 = s1.port
port2 = s2.port
#set Serial1 and Serial2 port value
s1.port = "COM3"
s2.port = "COM1"
我以前在接口Serial1使用getter/setter方法試過了,但他們導致無限遞歸:
def getport(self):
return self.port
def setport(self, port): #when first initialising self.port, it goes and get stuck here
self.port = port
port = property(getport,setport)