吴忠市网站建设_网站建设公司_内容更新_seo优化
2025/12/18 20:05:20 网站建设 项目流程

CH347L I2C 扫描地址

Linux- 树莓派测试

#!/usr/bin/env python3 # coding=utf-8 import ctypes from ctypes import * import sys DEV = "/dev/ch34x_pis0" DEV_C = c_char_p(DEV.encode()) LIB = "./libch347.so" # ---- load lib ---- ch347 = ctypes.cdll.LoadLibrary(LIB) # ---- prototypes ---- ch347.CH347OpenDevice.argtypes = [c_char_p] ch347.CH347OpenDevice.restype = c_int ch347.CH347CloseDevice.argtypes = [c_int] ch347.CH347CloseDevice.restype = None # StreamI2C fallback prototype ch347.CH347StreamI2C.argtypes = [c_int, c_ulong, c_void_p, c_ulong, c_void_p] ch347.CH347StreamI2C.restype = c_int def has_retack(): return hasattr(ch347, "CH347StreamI2C_RetACK") def bind_retack(): """ int CH347StreamI2C_RetACK( int DevHandle, ULONG iWriteLength, PVOID iWriteBuffer, ULONG iReadLength, PVOID oReadBuffer, PULONG oAckCount ) """ fn = ch347.CH347StreamI2C_RetACK fn.argtypes = [c_int, c_ulong, c_void_p, c_ulong, c_void_p, POINTER(c_ulong)] fn.restype = c_int return fn def open_dev(): h = ch347.CH347OpenDevice(DEV_C) if h == -1: print("CH347 open failed") sys.exit(1) return h def close_dev(h): ch347.CH347CloseDevice(h) def probe_ack_retack(fn_retack, h, addr7): """ 真·扫描:用 RetACK 看 ACK 数 事务:START + SLA+W + STOP 只要 ack_cnt > 0,说明这个地址有ACK(存在设备) """ tx = (c_ubyte * 1)() rx = (c_ubyte * 1)() ack = c_ulong(0) tx[0] = ((addr7 << 1) & 0xFE) # SLA+W r = fn_retack(h, 1, tx, 0, rx, byref(ack)) if r != 0: return False return ack.value > 0 def probe_ack_fallback(h, addr7): """ 退化版(不保证真):用“写地址+读1字节”试图触发NACK变为错误码 注意:很多 libch347 版本不会把 NACK 反映到返回值,所以可能假阳性 """ tx = (c_ubyte * 1)() rx = (c_ubyte * 1)() tx[0] = ((addr7 << 1) | 1) & 0xFF # SLA+R r = ch347.CH347StreamI2C(h, 1, tx, 1, rx) return r == 0 # 仅作退化判断 def scan(): h = open_dev() use_retack = has_retack() fn_retack = bind_retack() if use_retack else None if use_retack: print("CH347 I2C scan mode: RetACK (reliable)") else: print("CH347 I2C scan mode: fallback (NOT reliable)") print("Tip: your libch347.so may not export CH347StreamI2C_RetACK, so true i2cdetect-like scan is impossible.") # header print(" " + " ".join(f"{i:02X}" for i in range(16))) for hi in range(0x00, 0x80, 0x10): row = [f"{hi:02X}: "] for lo in range(16): a = hi + lo if a < 0x00 or a > 0x7F: row.append("--") continue if use_retack: ok = probe_ack_retack(fn_retack, h, a) else: ok = probe_ack_fallback(h, a) row.append(f"{a:02X}" if ok else "--") print(" ".join(row)) close_dev(h) if __name__ == "__main__": scan()

Windows 电脑测试

#! /usr/bin/env python # coding=utf-8 import os, sys from ctypes import * # ===== DLL ===== _here = os.path.dirname(os.path.abspath(__file__)) _dll = "CH347DLLA64.DLL" if sizeof(c_void_p) == 8 else "CH347DLL.DLL" ch347 = WinDLL(os.path.join(_here, _dll)) USB_ID = 0 # 第一个 CH347 # ===== prototypes ===== ch347.CH347OpenDevice.argtypes = [c_int] ch347.CH347OpenDevice.restype = c_int ch347.CH347CloseDevice.argtypes = [c_int] ch347.CH347CloseDevice.restype = None ch347.CH347StreamI2C.argtypes = [ c_int, c_ulong, c_void_p, c_ulong, c_void_p ] ch347.CH347StreamI2C.restype = c_int def has_retack(): return hasattr(ch347, "CH347StreamI2C_RetACK") def bind_retack(): fn = ch347.CH347StreamI2C_RetACK fn.argtypes = [ c_int, c_ulong, c_void_p, c_ulong, c_void_p, POINTER(c_ulong) ] fn.restype = c_int return fn def open_dev(): if ch347.CH347OpenDevice(USB_ID) == -1: print("CH347 open failed") sys.exit(1) def close_dev(): ch347.CH347CloseDevice(USB_ID) def probe_retack(fn, addr7): tx = (c_ubyte * 1)() rx = (c_ubyte * 1)() ack = c_ulong(0) tx[0] = (addr7 << 1) & 0xFE # SLA+W r = fn(USB_ID, 1, tx, 0, rx, byref(ack)) return r == 0 and ack.value > 0 def probe_fallback(addr7): tx = (c_ubyte * 1)() rx = (c_ubyte * 1)() tx[0] = ((addr7 << 1) | 1) & 0xFF return ch347.CH347StreamI2C(USB_ID, 1, tx, 1, rx) == 0 def scan(): open_dev() use_retack = has_retack() fn = bind_retack() if use_retack else None print("CH347 I2C scan (Windows)") print("mode:", "RetACK (reliable)" if use_retack else "fallback (not reliable)") print(" " + " ".join(f"{i:02X}" for i in range(16))) for hi in range(0x00, 0x80, 0x10): row = [f"{hi:02X}:"] for lo in range(16): addr = hi + lo if addr > 0x7F: row.append("--") continue ok = probe_retack(fn, addr) if use_retack else probe_fallback(addr) row.append(f"{addr:02X}" if ok else "--") print(" ".join(row)) close_dev() if __name__ == "__main__": scan()

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询