11 KiB
11 KiB
TS660_DLL API 使用说明
数字延迟发生器 TS660 串口通信动态链接库
1. 概述
TS660_DLL 是数字延迟发生器 TS660 的 Windows 串口通信封装库,提供 C 语言接口(extern "C"),可通过 C/C++/C#/Python 等语言调用。
主要功能:
- 读取板卡温度
- 读取时序配置参数(延时、脉宽、触发模式、频率等)
- 设置触发模式(内部/外部/软件)
- 设置内触发频率
- 控制通道输出使能
- 调节通道延时和脉宽(10ps 分辨率)
- 控制全局输出
- 软件触发
2. 运行环境
- 操作系统: Windows 7/10/11 (x64)
- 通信接口: RS-232 串口(或 USB 转串口)
- 波特率: 115200(设备默认)
- 无额外运行时依赖
3. 交付文件清单
TS660_DLL_SDK/
├── TS660_DLL.dll 动态链接库(x64, Release)
├── TS660_DLL.lib 导入库(链接时使用)
├── TS660API.h C 接口头文件
├── TS660DDG.h 协议常量与数据结构定义
├── TS660_DLL_API.md 本文档
├── example/
│ └── example.c C 调用示例
├── TS660_Test.exe 硬件测试程序(可选)
└── TS660_UnitTest.exe 单元测试程序(可选)
4. 快速开始
4.1 C/C++ 调用
#include "TS660API.h"
int main() {
/* 1. 打开设备 */
void* handle = ts660_init("COM10", 0); /* 0 = 使用默认波特率 115200 */
if (!handle) {
printf("打开失败\n");
return -1;
}
/* 2. 读取温度 */
float temp;
if (ts660_read_temperature(handle, &temp) == 0) {
printf("温度: %.2f°C\n", temp);
}
/* 3. 设置触发模式为内部触发 */
ts660_set_trigger(handle, 0); /* 0=内部, 1=外部, 2=软件 */
/* 4. 设置内触发频率 1kHz */
ts660_set_trigger_freq(handle, 1000.0);
/* 5. 设置通道1延时 10ns、脉宽 100ns */
ts660_set_delay(handle, 1, 10);
ts660_set_pulse_width(handle, 1, 100);
/* 6. 使能通道1输出 */
ts660_set_output_channel(handle, 1, 1);
/* 7. 使能全局输出 */
ts660_set_global_output(handle, 1);
/* 8. 关闭设备 */
ts660_close(handle);
return 0;
}
编译命令(MSVC):
cl /I. example.c /link TS660_DLL.lib /OUT:example.exe
4.2 C# 调用
using System;
using System.Runtime.InteropServices;
class TS660 {
[DllImport("TS660_DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr ts660_init(string com_port, int baud_rate);
[DllImport("TS660_DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void ts660_close(IntPtr handle);
[DllImport("TS660_DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int ts660_read_temperature(IntPtr handle, ref float out_temp);
[DllImport("TS660_DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int ts660_set_delay(IntPtr handle, int channel, long delay_ns);
static void Main() {
IntPtr handle = ts660_init("COM10", 0);
if (handle == IntPtr.Zero) { Console.WriteLine("打开失败"); return; }
float temp = 0;
ts660_read_temperature(handle, ref temp);
Console.WriteLine($"温度: {temp:F2}°C");
ts660_set_delay(handle, 1, 10); // 通道1延时10ns
ts660_set_pulse_width(handle, 1, 100); // 通道1脉宽100ns
ts660_close(handle);
}
}
4.3 Python 调用
from ctypes import *
dll = CDLL("TS660_DLL.dll")
# 打开设备
handle = dll.ts660_init(b"COM10", 0)
if not handle:
print("打开失败"); exit(-1)
# 读取温度
temp = c_float()
dll.ts660_read_temperature(handle, byref(temp))
print(f"温度: {temp.value:.2f}°C")
# 设置通道1延时10ns、脉宽100ns
dll.ts660_set_delay(handle, 1, 10)
dll.ts660_set_pulse_width(handle, 1, 100)
# 关闭
dll.ts660_close(handle)
5. 接口函数详解
所有函数返回 int: 0 表示成功,-1 表示句柄无效,-2 表示通信失败(超时或校验错误)。
5.1 ts660_init - 打开设备
void* ts660_init(const char* com_port, int baud_rate);
| 参数 | 类型 | 说明 |
|---|---|---|
| com_port | const char* | 串口名,如 "COM3"、"COM10"。COM10 以上会自动加 \\.\ 前缀 |
| baud_rate | int | 波特率。传 0 或负数使用默认值 115200 |
返回: 成功返回不透明句柄(非 NULL),失败返回 NULL。
5.2 ts660_close - 关闭设备
void ts660_close(void* handle);
关闭串口并释放资源。调用后句柄失效,不可再用。
5.3 ts660_read_temperature - 读取温度
int ts660_read_temperature(void* handle, float* out_temp);
| 参数 | 说明 |
|---|---|
| out_temp | 输出:板卡温度(摄氏度,IEEE754 浮点) |
5.4 ts660_read_timing_config - 读取时序配置
int ts660_read_timing_config(void* handle, int channel,
long long* out_delay_ns, long long* out_width_ns,
int* out_trigger_mode, double* out_trig_freq_hz);
| 参数 | 说明 |
|---|---|
| channel | 通道号 1 |
| out_delay_ns | 输出:该通道延时(ns) |
| out_width_ns | 输出:该通道脉宽(ns) |
| out_trigger_mode | 输出:触发模式(1=内触发计数器, 2=外触发, 3=软件, 4=内触发DDS) |
| out_trig_freq_hz | 输出:内触发频率(Hz) |
5.5 ts660_set_trigger - 设置触发模式
int ts660_set_trigger(void* handle, int mode);
| mode 值 | 说明 | 协议 OPT 值 |
|---|---|---|
| 0 | 内部触发 | 1 |
| 1 | 外部触发 | 2 |
| 2 | 软件触发 | 3 |
5.6 ts660_set_trigger_freq - 设置内触发频率
int ts660_set_trigger_freq(void* handle, double freq_hz);
内部按 周期(ns) / 4ns 换算为控制字。例如 1kHz → 周期 1,000,000ns → 控制字 250,000。
5.7 ts660_set_output_channel - 通道输出控制
int ts660_set_output_channel(void* handle, int channel, int enable);
| 参数 | 说明 |
|---|---|
| channel | 通道号 1~8 |
| enable | 1=使能输出, 0=关闭 |
5.8 ts660_set_delay - 设置通道延时
int ts660_set_delay(void* handle, int channel, long long delay_ns);
分辨率 10ps。内部按 delay_ns × 100 换算为 64 位控制字。
5.9 ts660_set_pulse_width - 设置通道脉宽
int ts660_set_pulse_width(void* handle, int channel, long long width_ns);
分辨率 10ps。内部按 width_ns × 100 换算为 64 位控制字。
5.10 ts660_set_global_output - 全局输出控制
int ts660_set_global_output(void* handle, int enable);
enable=1 使能所有通道输出,enable=0 关闭。
5.11 ts660_software_trigger - 软件触发
int ts660_software_trigger(void* handle);
注意: 需先调用 ts660_set_trigger(mode=2) 切换到软件触发模式,否则设备返回操作失败。
6. 错误码
| 返回值 | 含义 |
|---|---|
| 0 | 成功 |
| -1 | 句柄无效(未初始化或已关闭) |
| -2 | 通信失败(超时、校验错误、设备无响应) |
7. 通信协议
7.1 帧结构
┌──────────┬──────────┬─────┬─────┬─────┬──────────┬──────┐
│ 帧头 │ 帧长 │ CMD │ OPT │ PA* │ 校验和 │ 帧尾 │
│ 2 字节 │ 2 字节 │1字节│2字节│可变 │ 2 字节 │1字节 │
│ EB 90 │ Num │ │ │ │ 低字节在前│ CC │
└──────────┴──────────┴─────┴─────┴─────┴──────────┴──────┘
- Num = CMD(1) + OPT(2) + PA 字节数 + CHK(2) + TAIL(1)
- CMD: bit7=读写(0=写, 1=读), bit0-6=命令码
- OPT: 功能字,2 字节小端
- PA*: 参数,可变长度,小端
- 校验和: 帧头到校验和前所有字节求和取低16位,小端
- 帧尾: 0xCC
- 两字节间发送间隔不得超过 10ms
7.2 命令码表
| 命令 | CMD 码 | 读写 | OPT | PA | 说明 |
|---|---|---|---|---|---|
| READ_TEMP | 0x13 | 读(0x93) | 无效 | 无 | 读取温度 |
| READ_TIMING | 0x41 | 读(0xC1) | 无效 | 无 | 读取全部时序配置 |
| SET_TRIGGER | 0x01 | 写 | 触发模式(1/2/3) | 无 | 设置触发模式 |
| SET_TRIG_FREQ | 0x05 | 写 | 0x0000 | 4字节控制字 | 设置内触发频率 |
| SET_CHANNEL | 0x06 | 写 | (ch<<8)|en | 无 | 通道输出控制 |
| SET_DELAY | 0x08 | 写 | 通道号 | 8字节控制字 | 设置延时 |
| SET_PULSE_WIDTH | 0x09 | 写 | 通道号 | 8字节控制字 | 设置脉宽 |
| SET_GLOBAL_OUT | 0x07 | 写 | 1/0 | 无 | 全局输出控制 |
| SOFTWARE_TRIG | 0x14 | 写 | 0x0000 | 无 | 软件触发 |
7.3 READ_TIMING 响应参数
响应帧 DATA = 351 字节 = CMD(1) + OPT(2) + 87 个 uint32 参数(PA0~PA86)
| PA 编号 | 说明 |
|---|---|
| PA0~PA15 | 通道1~8 延时(低32位+高32位,64位控制字,÷100→ns) |
| PA16~PA31 | 通道1~8 脉宽(低32位+高32位) |
| PA32 | 触发设置(字节3)、触发边沿(字节2)、触发电平mV(低16位) |
| PA33 | 内触发频率计数器值(lsb=4ns) |
| PA34 | GATE设置(高16位)、通道输出状态(bit0 |
| PA35~PA50 | 通道1~8 序列步进(64位控制字) |
| PA51~PA66 | 通道1~8 序列起始(64位控制字) |
| PA67 | 序列使能控制 |
| PA69 | 板卡温度(32位浮点) |
| PA70~PA86 | 其他参数(极性、自检、阻抗、DDS、分频等) |
四通道卡中 PA8PA15、PA24PA31、PA43PA50、PA59PA66 对应通道5~8 的参数无效。
8. 典型工作流程
1. ts660_init() 打开设备
2. ts660_read_temperature() 读取温度(可选,检查设备状态)
3. ts660_set_trigger(0) 设置为内部触发模式
4. ts660_set_trigger_freq() 设置内触发频率
5. ts660_set_delay() 设置各通道延时
6. ts660_set_pulse_width() 设置各通道脉宽
7. ts660_set_output_channel() 使能需要输出的通道
8. ts660_set_global_output(1) 开启全局输出
9. (可选) 切换软件触发模式后 ts660_software_trigger()
10. ts660_close() 关闭设备
9. 注意事项
- 波特率: 设备默认 115200,不是 9600。
baud_rate传 0 使用默认值。 - COM10+ 端口: 传
"COM10"即可,DLL 自动加\\.\前缀。 - 软件触发: 调用
ts660_software_trigger()前必须先用ts660_set_trigger(2)切换到软件触发模式,否则设备返回失败。 - 延时/脉宽分辨率: 内部按 10ps 控制(
ns × 100)。设置 10ns 实际控制字为 1000。 - 多线程: 句柄非线程安全,多线程访问需外部加锁。
- 超时: 默认通信超时 1000ms,设备无响应时函数返回 -2。
- DLL 部署: 运行时需将
TS660_DLL.dll放在可执行文件同目录或系统路径中。
10. 版本历史
| 版本 | 日期 | 说明 |
|---|---|---|
| 1.0 | 2026-08 | 初版,完整协议实现 |