Python在Windows下操作CH341DLL

Posted 绿杨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python在Windows下操作CH341DLL相关的知识,希望对你有一定的参考价值。

 1 #! /usr/bin/env python
 2 #coding=utf-8
 3 import os
 4 import time
 5 from ctypes import *
 6 
 7 class CH341():
 8     def __init__(self, dev = 0xb8):
 9         self.dev_addr = dev
10 
11     def read(self, addr):
12         ch341 = windll.LoadLibrary("CH341DLL.dll")
13         ch341.CH341OpenDevice(0)
14         ch341.CH341SetStream(0, 0x82)
15         obuf = (c_byte * 2)()
16         ibuf = (c_byte * 1)()
17         obuf[0] = self.dev_addr
18         obuf[1] = addr
19         ch341.CH341StreamI2C(0, 2, obuf, 1, ibuf)
20         ch341.CH341CloseDevice(0)
21         return ibuf[0] & 0xff
22 
23     def write(self, addr, dat):
24         ch341 = windll.LoadLibrary("CH341DLL.dll")
25         ch341.CH341OpenDevice(0)
26         ch341.CH341SetStream(0, 0x82)
27         obuf = (c_byte * 3)()
28         ibuf = (c_byte * 1)()
29         obuf[0] = self.dev_addr
30         obuf[1] = addr
31         obuf[2] = dat & 0xff
32         ch341.CH341StreamI2C(0, 3, obuf, 0, ibuf)
33         ch341.CH341CloseDevice(0)

 

以上是关于Python在Windows下操作CH341DLL的主要内容,如果未能解决你的问题,请参考以下文章

CH341A是啥?

CH341A编程器“写操作超时失败”是怎么回事

ch341ser是电脑自带的吗

ch341和ch340一样吗

CH341SER 是干啥的??详细

将 raspbian wheezy USB 驱动程序 ch341.c 编译为 ch341.ko