news 2026/7/3 15:35:21

I2CSPI 沉浸式测试

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
I2CSPI 沉浸式测试

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()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/2 15:45:16

教培行业新媒体运营困境凸显!这款软件或成转型制胜法宝?

随着数字化时代的来临&#xff0c;教培行业的竞争格局发生了巨大变化。新媒体运营成为了教培机构提升品牌知名度、获取客户的重要手段。然而&#xff0c;许多教培机构在新媒体运营过程中却遭遇了重重困境&#xff0c;如何突破这些困境&#xff0c;实现数字化转型&#xff0c;成…

作者头像 李华
网站建设 2026/7/1 19:48:56

12.18

661图片平滑器class Solution { public:vector<vector<int>> imageSmoother(vector<vector<int>>& img) {int mimg.size();//行数int nimg[0].size();//列数vector<vector<int>>v(m,vector<int>(n));//记录答案for(int i0;i<…

作者头像 李华
网站建设 2026/7/3 8:28:35

COCO 数据集

COCO 数据集 COCO&#xff08;Common Objects in Context&#xff09;是计算机视觉领域广泛使用的目标检测、实例分割和关键点检测数据集&#xff0c;由微软发布。其特点包括&#xff1a;数据规模 包含超过 33 万张图像&#xff0c;标注对象超过 250 万个&#xff0c;涵盖 80 个…

作者头像 李华
网站建设 2026/7/2 8:10:13

国内好用的测试用例管理工具有哪些?

目前市面上的测试用例管理工具有很多&#xff0c;但由于针对的项目、领域、目标用户&#xff0c;功能也并不一致&#xff0c;所以选择一款适合的测试管理平台并不轻松。做好这件事&#xff0c;首先要需求明确你用测试管理工具干什么&#xff1f;最终想要达到什么目标&#xff1…

作者头像 李华