Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support linux system #3

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 85 additions & 6 deletions ch341_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import os
import sys
import time
from ctypes import *
from flash import *


class ch341_class(object):
class ch341_class_windows(object):

def __init__(self):
self.dll = None
self.dev_connected = 0
self.dll_path = './dll/CH341DLL.DLL'
self.dll_path = './dll/windows/x64/CH341DLL.DLL'
self.iobuffer = create_string_buffer(65544)
self.ilength = 0
self.iIndex = 0
Expand All @@ -32,7 +33,7 @@ def __init__(self):
a = 1 # print("Error: Can't find"+self.dll_path)

def open_device(self):
return self.dll.CH341OpenDevice(0)
return self.dll.CH341OpenDevice(self.iIndex)

def close_device(self):
self.dll.CH341CloseDevice(self.iIndex)
Expand Down Expand Up @@ -74,6 +75,80 @@ def dev_connect(self):
# print("DBG: flash is disconnected")


class ch341_class_linux(object):

def __init__(self):
self.dll = None
self.dev_connected = 0
self.dll_path = './dll/linux/x64/libch347.so'
self.iobuffer = create_string_buffer(65544)
self.ilength = 0
self.iIndex = 0
self.flash_connected = 0
self.status = 0 # 0:idle
self.command = 0
self.vtx_id = 0
self.fw_path = ""
self.fw_full_size = 0
self.fw_done_size = 0
self.update_state = 0
self.percent = 0
self.write_crc = 0
self.read_crc = 0
self.success = 0

try:
self.dll = cdll.LoadLibrary(self.dll_path)
except:
a = 1 # print("Error: Can't find"+self.dll_path)

def open_device(self):
self.iIndex = self.dll.CH34xOpenDevice("/dev/ch34x_pis0".encode())
Copy link

@SumolX SumolX Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend using an auto-detect method here.

import glob

def open_device(self):
        # Return first entry
        device = glob.glob('/dev/ch34x_pis[0-9]*')[0]
        self.iIndex = self.dll.CH34xOpenDevice(device.encode())
        return self.iIndex

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

return self.iIndex

def close_device(self):
self.dll.CH34xCloseDevice(self.iIndex)

def get_version(self):
return self.dll.CH34xGetVersion()

def get_driver_version(self):
return self.dll.CH34xGetDrvVersion()

def get_chip_version(self):
ver_str = create_string_buffer(1)
self.dll.CH34x_GetChipVersion(self.iIndex, ver_str)
return int.from_bytes(ver_str[0], byteorder='big')

def set_stream(self, cs):
if cs == True:
self.dll.CH34xSetStream(self.iIndex, 0x80)
else:
self.dll.CH34xSetStream(self.iIndex, 0x81)

def stream_spi4(self):
self.dll.CH34xStreamSPI4(
self.iIndex, 0x80, self.ilength, self.iobuffer)

# def GetDeviceDescr(self,iIndex):
# self.dll.CH341GetDeviceDescr(iIndex, self.oBuffer_P, self.ioLength);
def dev_connect(self):
if self.dev_connected == 0:
if self.open_device() > 0:
self.dev_connected = 1
# print("DBG: ch341 is connected")
# else:
# time.sleep(0.1)
elif self.dev_connected == 1 and self.status == 0:
if self.get_chip_version() == 0:
self.dev_connected = 0
# print("DBG: ch341 is disconnected")
if self.flash_connected == 1:
self.flash_connected = 0
# print("DBG: flash is disconnected")



def flash_read_id(ch341):
ch341.iobuffer[0] = 0x9f
ch341.iobuffer[1] = 0x9f
Expand Down Expand Up @@ -114,16 +189,20 @@ def flash_connect(ch341):
# print("DBG: flash is disconnected")
ch341.flash_connected = 0


ch341 = ch341_class()
if sys.platform.startswith('win'):
ch341 = ch341_class_windows()
elif sys.platform.startswith('linux'):
ch341 = ch341_class_linux()
else:
ch341 = ch341_class_windows()


def ch341ThreadProc():
# print('start ch341ThreadProc')

while True:
ch341.dev_connect()
flash_connect(ch341)
# flash_connect(ch341)
if ch341.command == 1:
flash_read_vtx_id(ch341)
ch341.vtx_id = int.from_bytes(ch341.iobuffer[4], byteorder='big')
Expand Down
Loading