using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; // 引入线程命名空间用于 Thread.Sleep using System.Threading.Tasks; using NationalInstruments.Visa; // 引入 VISA 命名空间 namespace ConsoleApp2 { internal class VisaTest { // 静态变量定义 private static ResourceManager resourceMgr; protected static MessageBasedSession messageSession; protected static int outputBufferSize 1024; // 设置一个默认缓冲区大小例如 1024 字节 private static string resourceName GPIB0::5::INSTR; // 请确保这里地址正确 protected static int retryTimes 3; // 默认重试3次 protected static int retryInterval 500; // 默认重试间隔 500ms protected static string identifier ; // 如果不需要校验IDN留空即可 public static void runMain(string[] args) { Console.WriteLine(--- 主板电源测试程序启动 ---); // 1. 连接设备 if (!Connect()) { Console.WriteLine(设备连接失败程序退出。); Console.ReadKey(); return; } // 2. 执行测试逻辑 RunMainboardTestLogic(); // 3. 释放资源 Release(); Console.WriteLine(测试完成按任意键退出...); Console.ReadKey(); } /// summary /// 连接并初始化设备 /// /summary static bool Connect() { try { if (resourceMgr null) resourceMgr new ResourceManager(); // 打开会话 messageSession resourceMgr.Open(resourceName) as MessageBasedSession; // 设置超时时间 (毫秒) messageSession.TimeoutMilliseconds 15000; // 清除读写缓冲区防止残留数据干扰 messageSession.Clear(); } catch (Exception ex) { Console.WriteLine(初始化VISADriver异常: ex.Message); return false; } // 校验设备标识符 if (!CheckInstrumenIdentifier()) { return false; } // 初始化仪器状态 (可选) try { messageSession.FormattedIO.WriteLine(*CLS); // 清除状态 // messageSession.FormattedIO.WriteLine(*RST); // 恢复出厂设置 (慎用可能会重置GPIB地址) } catch { } Console.WriteLine(设备连接成功: resourceName); return true; } /// summary /// 释放资源 /// /summary static bool Release() { try { // 确保输出关闭 SendSCPI(OUTP OFF); if (messageSession ! null) { messageSession.Dispose(); messageSession null; } if (resourceMgr ! null) { resourceMgr.Dispose(); resourceMgr null; } return true; } catch (Exception ex) { Console.WriteLine(释放资源时出错: ex.Message); return false; } } /// summary /// 校验设备 IDN /// /summary static bool CheckInstrumenIdentifier() { if (!string.IsNullOrEmpty(identifier)) { string idn SendSCPIReadBack(*IDN?); if (!idn.Contains(identifier)) // 使用 Contains 比 Equals 更容错 { Console.WriteLine(连接VISA设备失败, 返回的IDN不正确: {0}, idn); return false; } } return true; } /// summary /// 发送 SCPI 指令并读取返回字符串的通用方法 /// /summary static string SendSCPIReadBack(string command) { string result ; for (int i 0; i retryTimes; i) { try { messageSession.RawIO.Write(command); // 读取数据并去除首尾空白字符 result messageSession.RawIO.ReadString(outputBufferSize).Trim(); return result; } catch (Exception ex) { Console.WriteLine($通信重试 {i 1}/{retryTimes}: {ex.Message}); Thread.Sleep(retryInterval); } } throw new Exception(SCPI指令执行失败超过最大重试次数); } static bool SendSCPI(string command) { try { messageSession.RawIO.Write(command); return true; } catch (Exception er) { Console.WriteLine(发送SCPI指令失败: er.Message); return false; } } // // 以下是针对主板测试的具体业务逻辑 // /// summary /// 设置电源电压 /// /summary static void SendSetCommend(string input) { // 常见的 SCPI 指令VOLT 值 SendSCPI(input); Console.WriteLine($设置参数: {input}); } /// summary /// 开启/关闭输出 /// /summary static void SetOutput(bool on) { string state on ? ON : OFF; SendSCPI($OUTP {state}); Console.WriteLine($输出状态: {state}); } /// summary /// 读取实际电流值 /// /summary static double ReadcommendDouble(string input) { // 发送测量电流指令 string response SendSCPIReadBack(input); if (double.TryParse(response, out double current)) { return current; } return -1; // 解析失败返回 -1 } /// summary /// 主板测试主流程 /// /summary static void RunMainboardTestLogic() { Console.WriteLine(--- 开始测试流程 ---); // 2. 开启输出 SetOutput(true); // 1. 预设参数 double targetVoltage 5; // 目标电压 5V double limitCurrent 2; // 限流 2A // SendSetCommend($VOLT {targetVoltage}); SendSetCommend($CURR {limitCurrent}); SendSetCommend($SENS:CURR:RANG 0.0001); Thread.Sleep(500); // 等待电源稳定 // 3. 循环采样 (模拟测试) int sampleCount 10; for (int i 0; i sampleCount; i) { double currentVal ReadcommendDouble(MEAS:CURR?); double voltVal ReadcommendDouble(MEAS:VOLT?); Console.WriteLine($第 {i 1} 次采样 - 电压: {voltVal:F2}V, 电流: {currentVal:F8}A ({currentVal * 1000000:F2}μA)); // 简单保护逻辑如果电流超过限流值的 90 %停止测试 if (currentVal limitCurrent * 0.9) { Console.WriteLine(警告电流接近限值停止测试); break; } Thread.Sleep(1000); // 每隔 1 秒采样一次 } // 4. 关闭输出 SetOutput(false); Console.WriteLine(--- 测试流程结束 ---); } } }