前言
JTool CMD和DLL是用于USB转I2C、USB转IO等工具的命令行脚本工具和二次开发库
- CMD工具通常在生产测试环境中有很大作用,如在bat批处理脚本或Powershell脚本中实现自动化动作或检测
- DLL用于提供应用层调用的API,方便进行二次开发(C#、Python、QT、Matlab等)
CMD在Powershell中的应用示例
获取IO3的ADC采样值,判断是否在范围内
jtool adcon 3 0 #通道3作为ADC采样,不使用差分
$output = & "jtool" adcget 3 #获取通道3的采样值
$number = 0
# 尝试将输出转换为整数
if ([int]::TryParse($output, [ref] $number)) {
$number = [Math]::Round($number * 3.3 / 4096,2)
"转换成功,输出的数字为: $number"
$number = [Math]::Abs($number - 3.3)
"对比偏差值为: $number"
if($number -gt 0.3){ #判断偏差
Write-Error "对比失败,超出范围"
}
} else {
$output
Write-Error "转换失败,输出的不是有效的数"
}
DLL在C#程序中的应用示例
读取I2C数据
//导入DLLAPI
[DllImport("jtool.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr DevOpen(DevType DevType, string Sn, int Id);
[DllImport("jtool.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool DevClose(IntPtr DevHandle);
[DllImport("jtool.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int I2CRead(IntPtr DevHandle, byte slave_addr, int reg_type, UInt32 reg_addr, UInt16 len, byte[] buf);
private void Read()
{
//打开设备,不指定SN和ID,只要有I2C设备就打开
IntPtr p = DevOpen(DevType.dev_i2c, null, -1);
if (p == IntPtr.Zero)
{
Console.WriteLine("打开设备失败");
return;
}
int readlen = 16;
byte[] buffread = new byte[readlen];
I2CRead(p, 0xA0, 1, 0x00, readlen, buffread);//读取从机地址A0,寄存器地址00,读16个字节到buffread
Console.WriteLine(BitConverter.ToString(buffread).Replace("-", " "));//打印读取的数据
DevClose(p);//关闭设备
}
在使用cmd或dll之前,建议先使用我们提供的上位机,先确保能正常使用
← JTool CMD/DLL
CMD命令集 →