JNA中SetCommMask与WaitCommEvent的位置查询及实现方案咨询
JNA串口API缺失问题解答
接口存在性问题
JNA 5.9.0版本的官方Win32绑定模块确实没有将WaitCommEvent、SetCommMask两个串口API纳入默认的Kernel32封装,也没有放在其他官方标准API模块中。JNA本身支持开发者直接扩展已有Kernel32接口补充缺失的原生方法绑定,这是官方推荐的标准扩展方式,不需要额外引入第三方依赖。
SetCommMask替代方案
SetCommMask是Windows系统底层提供的、用于配置串口需要监听的事件类型的唯一入口,没有可替代的同级别API,直接通过扩展JNA接口绑定该原生方法调用即可。
实现有效性验证
你提供的扩展Kernel32接口的实现完全符合JNA开发规范,已经可以正常满足串口事件监听的开发需求。
JNA官方贡献流程
如果要将该实现贡献到JNA官方仓库,可按照以下步骤操作:
- 拉取JNA官方仓库的最新develop分支代码
- 在
com.sun.jna.platform.win32.Kernel32接口类中,补充你已经定义好的所有EV_前缀的事件常量,以及GetCommMask、SetCommMask、WaitCommEvent三个方法的签名 - 按照JNA项目的测试规范编写对应的单元测试用例,确保测试用例可在Windows平台正常执行通过
- 提交符合规范的Commit说明,之后提交PR等待维护人员审核即可
参考实现代码
public interface MyKernel32 extends Kernel32 { public static final int EV_BREAK = 0x0040; public static final int EV_CTS = 0x0008; public static final int EV_DSR = 0x0010; public static final int EV_ERR = 0x0080; public static final int EV_EVENT1 = 0x0800; public static final int EV_EVENT2 = 0x1000; public static final int EV_PERR = 0x0200; public static final int EV_RING = 0x0100; public static final int EV_RSLD = 0x0020; public static final int EV_RX80FULL = 0x0400; public static final int EV_RXCHAR = 0x0001; public static final int EV_RXFLAG = 0x0002; public static final int EV_TXEMPTY = 0x0004; public MyKernel32 INSTANCE = (MyKernel32) Native.load(MyKernel32.class, W32APIOptions.DEFAULT_OPTIONS); public boolean GetCommMask(HANDLE handle, IntByReference mask); public boolean SetCommMask(HANDLE handle, DWORD mask); public boolean WaitCommEvent(HANDLE handle, IntByReference mask, WinBase.OVERLAPPED overlapped); } private MyKernel32 libc = MyKernel32.INSTANCE;
内容的提问来源于stack exchange,提问作者WillyL
相关产品推荐
相关产品推荐

