From f7d0e8755dbb284fa970643c3999a9e3a6c77530 Mon Sep 17 00:00:00 2001 From: jinggaoyang-68 Date: Tue, 10 Dec 2024 12:55:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9linux=E5=92=8Cwindow=E4=B8=8B?= =?UTF-8?q?=E7=9A=84ch341=E5=85=BC=E5=AE=B9=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cap_read.py | 16 ++++++---- class_ch341.py | 85 ++++++++++++++++++++++++-------------------------- sensorPara.py | 1 + 3 files changed, 52 insertions(+), 50 deletions(-) diff --git a/cap_read.py b/cap_read.py index ac23804..62462ee 100644 --- a/cap_read.py +++ b/cap_read.py @@ -46,6 +46,16 @@ class ClassCapRead: self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_INIT + self.connectDebug() + + def __del__(self): + if self.vofaClient: + self.vofaClient.close() + self.ch341.disconnect() + print("ch341释放") + pass + + def connectDebug(self): #连接到调试的服务器 self.vofaClient = socket.socket() addr = ('127.0.0.1', 1347) @@ -57,12 +67,6 @@ class ClassCapRead: except Exception as e: self.socketConnected = 0 print('连接服务器失败') - def __del__(self): - if self.vofaClient: - self.vofaClient.close() - self.ch341.disconnect() - print("ch341释放") - pass def set_sensor_enable(self, idx): _pack = list() diff --git a/class_ch341.py b/class_ch341.py index a079371..f022bcc 100644 --- a/class_ch341.py +++ b/class_ch341.py @@ -32,28 +32,41 @@ class ClassCh341: pass def init(self): - #print('ch341初始化') - - addr = os.path.dirname(sys.argv[0]) + r'/lib/ch341/CH341DLLA64.DLL' - print(addr) - dllExist = os.path.exists(addr) + if os.name == 'nt': # Windows 环境 + libPath = os.path.dirname(sys.argv[0]) + r'/lib/ch341/CH341DLLA64.DLL' + elif os.name == 'posix': + libPath = './lib/ch341/libch347.so' + dllExist = os.path.exists(libPath) if not dllExist: print('未找到库文件') return False else: try: - self.ic = windll.LoadLibrary(addr) # ch341接口 + if os.name == 'nt': # Windows 环境 + self.ic = windll.LoadLibrary(libPath) # ch341接口 + + self.ch341GetInput = self.ic.CH341GetInput + self.ch341CloseDevice = self.ic.CH341CloseDevice + self.ch341WriteData = self.ic.CH341WriteData + self.ch341WriteRead = self.ic.CH341WriteRead + self.ch341SetOutput = self.ic.CH341SetOutput + self.ch341SetStream = self.ic.CH341SetStream + + elif os.name == 'posix': + self.ic = cdll.LoadLibrary(addr) # ch341接口 + + self.ch341GetInput = self.ic.CH34xGetInput + self.ch341CloseDevice = self.ic.CH34xCloseDevice + self.ch341WriteData = self.ic.CH34xWriteData + self.ch341WriteRead = self.ic.CH34xWriteRead + self.ch341SetOutput = self.ic.CH34xSetOutput + self.ch341SetStream = self.ic.CH34xSetStream + print("ch341加载成功") return True except Exception as e: print("ch341加载失败") return False - - def connectCheck(self): - if True == self.ic.CH341GetInput(self.fd, ctypes.byref(self.deviceID)): - return True - else: - return False # 判断ch341是否插入 # return:0未插入,1插入 @@ -65,6 +78,7 @@ class ClassCh341: print("CH341 device open failed on Windows.") return False else: + self.fd = 0 #todo 改成扫描端口 print("CH341 device opened successfully on Windows.") return True except Exception as e: @@ -93,33 +107,16 @@ class ClassCh341: else: print("Unsupported operating system.") return False - def open1(self): - # 查找匹配的设备路径 - self.fd = self.ic.CH341OpenDevice(0) - if self.fd == -1: - print("ch341 open false") - return False - else: - print("ch341 open true") - return True - # devices = glob.glob('/dev/ch34x_pis*') - # if devices: - # device_path = devices[0].encode() # 选择第一个找到的设备路径 - # self.fd = self.ic.CH34xOpenDevice(device_path)#.encode()"/dev/ch34x_pis1".encode() - # if self.fd == -1: - # print("ch341 open false") - # return False - # else: - # print("ch341 open true") - # return True - # else: - # print("No CH34x device found.") - # return False - - + def disconnect(self): - self.ic.CH341CloseDevice(0) + self.ch341CloseDevice(self.fd) + def connectCheck(self): + if True == self.ch341GetInput(self.fd, ctypes.byref(self.deviceID)): + return True + else: + return False + # iic写数据 # addr:iic从机地址 # data:要写的数据列表 @@ -150,7 +147,7 @@ class ClassCh341: sendBuf[j] = pack[j] sendLen = (c_byte * 1)() sendLen[0] = len(pack) - if not self.ic.CH341WriteData(0, sendBuf, sendLen): + if not self.ch341WriteData(self.fd, sendBuf, sendLen): return 0 if sendLen == 0: return 0 @@ -166,7 +163,7 @@ class ClassCh341: sendBuf[j] = pack[j] sendLen = (c_byte * 1)() sendLen[0] = len(pack) - if not self.ic.CH341WriteData(0, sendBuf, sendLen): + if not self.ch341WriteData(self.fd, sendBuf, sendLen): return 0 if sendLen == 0: return 0 @@ -204,7 +201,7 @@ class ClassCh341: sendBuf[j] = pack[j] recLen = (c_byte * 1)() recBuf = (c_byte * self._mCH341A_CMD_I2C_STM_MAX)() - if not self.ic.CH341WriteRead(0, len(pack), sendBuf, self._mCH341A_CMD_I2C_STM_MAX, 1, recLen, recBuf): + if not self.ch341WriteRead(self.fd, len(pack), sendBuf, self._mCH341A_CMD_I2C_STM_MAX, 1, recLen, recBuf): return 0 if recLen == 0: return 0 @@ -223,7 +220,7 @@ class ClassCh341: sendBuf[j] = pack[j] recLen = (c_byte * 1)() recBuf = (c_byte * self._mCH341A_CMD_I2C_STM_MAX)() - if not self.ic.CH341WriteRead(0, len(pack), sendBuf, self._mCH341A_CMD_I2C_STM_MAX, 1, recLen, recBuf): + if not self.ch341WriteRead(self.fd, len(pack), sendBuf, self._mCH341A_CMD_I2C_STM_MAX, 1, recLen, recBuf): return 0 if recLen[0] == 0: return 0 @@ -242,9 +239,9 @@ class ClassCh341: self.ic.CH341GetInput(0, status) time.sleep(0.01) if lvl: - self.ic.CH341SetOutput(0, 0x03, 0xFF00, status[0] | self._mStateBitINT) + self.ch341SetOutput(self.fd, 0x03, 0xFF00, status[0] | self._mStateBitINT) else: - self.ic.CH341SetOutput(0, 0x03, 0xFF00, status[0] & (~self._mStateBitINT)) + self.ch341SetOutput(self.fd, 0x03, 0xFF00, status[0] & (~self._mStateBitINT)) # 读取int引脚状态 # 返回:高低电平 @@ -258,7 +255,7 @@ class ClassCh341: def set_speed(self, speed): if speed != self.IIC_SPEED_20 and speed != self.IIC_SPEED_100 and speed != self.IIC_SPEED_400 and speed != self.IIC_SPEED_750: return False - if False == self.ic.CH341SetStream(0, speed | 0): + if False == self.ch341SetStream(self.fd, speed | 0): print("speed err") return False else: diff --git a/sensorPara.py b/sensorPara.py index 05b0c8f..5b76521 100644 --- a/sensorPara.py +++ b/sensorPara.py @@ -3,6 +3,7 @@ from ctypes import Structure, sizeof, c_float, c_uint32, c_uint16 class DynamicYddsComTs(Structure): + _pack_ = 1 # 按 1 字节对齐 _fields_ = [ ("nf", c_float), ("nfCap", c_uint32),