使用 C# 通过串行和并行端口控制 Real Measurement "3153 Hioki Hipot" 设备
通过C#中的串行和并行端口控制3153 Hioki 耐压测试仪设备。
引言
串行端口RS-232仍在工业应用中使用,但最近已从大多数笔记本电脑中消失。 .NET Framework中有一个SerialPort
类来处理所有串行端口通信,您只需通过添加"using System.IO.Ports;
"即可在代码中使用此M类。此外,对于并行端口访问,您必须使用以下URL中的Inpout32.dll:http://logix4u.net/Legacy_Ports/Parallel_Port/Inpout32.dll_for_Windows_98/2000/NT/XP.html。
在此项目中,我使用真实设备通过串行端口与Hioki 3153耐压测试仪进行通信。测试失败条件将通过LED指示,如本文所述:I/O端口解密 - 1 - 通过并行端口控制LED(发光二极管),作者:Levent Saltuklaroglu。
在本文末尾,您可以获取有关耐压测试仪设备的信息。所有信息均取自设备手册。感谢Behlul Savaskan输入这些部分。我还必须提及,Hioki可能没有客户支持部门。我通过电子邮件询问了一些问题,但没有收到他们的任何回复。最后,我还添加了一个安装项目,用于将并行端口所需的inpout32.dll复制到C:\Windows\System32文件夹。
命名空间
我使用了两个不同的命名空间:hipotinterface
,它包含主程序和串行端口代码;以及ParallelPort
,它处理并行端口通信。
主代码
这是串行端口和主窗口的代码。当主窗体通过FrmMain_Load
事件加载时,所有并行端口引脚都设置为低电平,并根据本文档“HIOKI 3153自动绝缘电阻耐压测试仪设置”部分中指示的设置来设置设备。根据用户手册,每个命令后都必须使用一个分隔符(回车和换行)。我使用了SerialPort
类的Writeline
方法。像“conf:with:kind AC50”这样的设置取自用户手册。我使用FrmMain_Load
事件,在操作员更改参数时强制设备以我的初始设置启动。
在CommWith3153At9600
函数中,耐压测试仪设备与我们的PC通信。您可以阅读“3153到PC接口”部分,了解发送消息和查询的详细信息。
RunTest()
函数根据CommWith3153At9600
函数的结果处理红色/绿色标签转换,并关闭串行和并行端口。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.IO.Ports;
using System.Threading;
namespace hipotinterface
{
public partial class FrmMain : Form
{
//RS232 variables
//Create a new SerialPort object with Hioki 3153 HiTester settings;
public static SerialPort _serialPort =
new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
public static string strTestResult = null;
static bool _ESR0CommandSent = false;
const string CRLF = "\r\n"; //Delimiter for 3153
private void FrmMain_Load(object sender, EventArgs e)
{
//change all parallel port data lines to low state.
ParallelPort.HipotParPort.HipotParPort1(false);
try
{
//Open Port
_serialPort.Open();
_serialPort.ReadTimeout = 5000;
//Send any Character To Pass 3153 to Remote Mode.
_serialPort.WriteLine(CRLF);
//Set Withstand Test-voltage type to :AC 50 Hz
_serialPort.WriteLine("conf:with:kind AC50");
_serialPort.WriteLine(CRLF);
//Set Withstand Test Voltage to 3.00kV
_serialPort.WriteLine("conf:with:volt 3.00");
_serialPort.WriteLine(CRLF);
//Set Withstand Cutoff current upper limit to 5.5 mA
_serialPort.WriteLine("with:clow ON");
_serialPort.WriteLine(CRLF);
_serialPort.WriteLine("conf:with:cupp 5.5");
_serialPort.WriteLine(CRLF);
//Set Withstand Cutoff current lower limit to 0.7 mA
_serialPort.WriteLine("conf:with:clow 0.7");
_serialPort.WriteLine(CRLF);
//Set Withstand Ramp Timer(Test Time) to :3.0 s.
_serialPort.WriteLine("with:tim ON");
_serialPort.WriteLine(CRLF);
_serialPort.WriteLine("conf:with:tim 3.0");
_serialPort.WriteLine(CRLF);
//Set Withstand Ramp-up Timer to OFF
_serialPort.WriteLine("with:utim OFF");
_serialPort.WriteLine(CRLF);
//Set Withstand Ramp-down Timer to OFF
_serialPort.WriteLine("with:dtim OFF");
_serialPort.WriteLine(CRLF);
////
//Set Insulation Test-voltage type to :DC
_serialPort.WriteLine("conf:ins:kind AC50");
_serialPort.WriteLine(CRLF);
//Set Insulation Test Voltage to 500V
_serialPort.WriteLine("conf:ins:volt 500");
_serialPort.WriteLine(CRLF);
//Set Insulation Resistance upper limit to :OFF.
_serialPort.WriteLine("ins:rupp OFF");
_serialPort.WriteLine(CRLF);
//Set Insulation Resistance lower limit to : 4.0 Mohm
_serialPort.WriteLine("conf:ins:rlow 4.0");
_serialPort.WriteLine(CRLF);
//Set Insulation Test Time to :1.0 s.
_serialPort.WriteLine("ins:tim ON");
_serialPort.WriteLine(CRLF);
_serialPort.WriteLine("conf:ins:tim 1.0");
_serialPort.WriteLine(CRLF);
//Set Insulation delay time to OFF.
_serialPort.WriteLine("ins:del OFF");
_serialPort.WriteLine(CRLF);
////
//Set Test Mode: Withstand voltage mode -> insulation resistance mode
_serialPort.WriteLine(":mode AWI");
_serialPort.WriteLine(CRLF);
_serialPort.Close();
}
catch (Exception)
{
MessageBox.Show("Initialization Error.");
Application.Restart();
}
}
public FrmMain()
{
InitializeComponent();
AssemblyInfo ainfo = new AssemblyInfo();
this.Text = "Tuse HIPOT Operator Interface " + ainfo.Version;
}
public void CommWith3153At9600()
{
strTestResult = null;
try
{
//Open Port
_serialPort.Open();
_serialPort.ReadTimeout = 5000;
//Send Clear command in case of unexpected behaviours.
_serialPort.WriteLine("*cls");
_serialPort.WriteLine(CRLF);
//Send Start Command&Read
_serialPort.WriteLine("start");
_serialPort.WriteLine(CRLF);
//Read Status
_ESR0CommandSent = true;
while (_ESR0CommandSent == true)
{
char ch = ' ';
while ((ch == '0') || (ch == ' '))
{
_serialPort.WriteLine("ESR0?");
_serialPort.WriteLine(CRLF);
//character, carriage return is ascii:13 and Line feed is ascii 10
string message = _serialPort.ReadLine();
if (message.StartsWith("9"))
//Test completed and within limits of comparator (PASS)
{
ch = '9';
strTestResult = "Passed";
_ESR0CommandSent = false;
}
else if (message.StartsWith("10"))
//Test completed and above upper of comparator (FAIL)
{
ch = 'A';
strTestResult = "Above Failed";
_ESR0CommandSent = false;
}
else if (message.StartsWith("12"))
//Test completed and below lower limit of comparator (FAIL)
{
ch = 'C';
strTestResult = "Lower Failed";
_ESR0CommandSent = false;
}
} // while ((ch == '0') || (ch == ' '))
}//while
if (strTestResult != "Passed")
{
_ESR0CommandSent = false;
} //if
//Print Result to The Status Bar
statusBar1.Text = "Result: " + strTestResult;
}
catch (Exception)
{
MessageBox.Show("Cannot communicate with the device");
ParallelPort.HipotParPort.HipotParPort1(false);
System.Environment.Exit(-1);
}
} //CommWith3153At9600
public void RunTest()
{
button_Run.Enabled = false;
lblDut1.BackColor = SystemColors.Window;
lblDut1.Update();
statusBar1.Text = "";
lblDut1.Text = "";
strTestResult = null;
CommWith3153At9600();
if (strTestResult == "Passed") //
{
lblDut1.BackColor = Color.GreenYellow;
} //if
else
{
lblDut1.BackColor = Color.Red;
lblDut1.Text = "Failed";
//make the bulb on to warn the operator that test has failed.
ParallelPort.HipotParPort.HipotParPort1(true);
statusBar1.Text = "HIPOT TEST FAILED";
MessageBox.Show("HIPOT TEST FAILED!!!",
"Hipot Test Failed",
MessageBoxButtons.OK, MessageBoxIcon.Error);
//send stop command to the hipot and close serial connection
_serialPort.WriteLine("stop");
_serialPort.WriteLine(CRLF);
_serialPort.WriteLine("*cls");
_serialPort.WriteLine(CRLF);
_serialPort.Close();
//make the bulb off
ParallelPort.HipotParPort.HipotParPort1(false);
goto result1;
} //else
_serialPort.WriteLine("*cls");
_serialPort.WriteLine(CRLF);
_serialPort.Close();
strTestResult = null;
result1:
button_Run.Enabled = true;
button_Run.Text = "RUN";
button_Run.Update();
} //public void RunTest()
private void button_Run_Click_1(object sender, EventArgs e)
{
RunTest();
} //button_Run_Click
}// public partial class FrmMain : Form
}
并行端口代码
Parallelport.cs文件中的代码是根据I/O Ports Uncensored - 1 - Controlling LEDs (Light Emitting Diodes) with Parallel Port by Levent Saltuklaroglu修改的;如果您需要逐行信息,应该查看该文章。该代码假设LPT1是默认并行端口,并且在测试失败时仅点亮第一个引脚(D0)。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace ParallelPort
{
public partial class HipotParPort : Form
{
//Default is LPT1. If you want to use LPT2 choose 632
public static int nAddress = 888;
//decimal 1 -> only D0 is high.
public static Int32 onValue = 1;
//decimal 0 -> all data pins of the ParallelPort is low.
public static Int32 offValue = 0;
// put some time between ON/OFF states in case of burning...
public static int nDelay = 1000;
public class PortAccess
{
//http://logix4u.net/Legacy_Ports/Parallel_Port/
// Inpout32.dll_for_Windows_98/2000/NT/XP.html
//Check the upper url to learn how the inpout32.dll works
//Note that inpout32.dll must
// be put to the C:\Windows\System32 folder.
[DllImport("inpout32.dll", EntryPoint="Out32")]
public static extern void Output(int nAddress, int nDecimalValue);
} //public class PortAccess
public static void HipotParPort1(Boolean bBulbOn)
{
Application.DoEvents();
if (bBulbOn)
PortAccess.Output(nAddress, onValue);
else
PortAccess.Output(nAddress, offValue);
Thread.Sleep(nDelay);
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// HipotParPort
//
this.ClientSize = new System.Drawing.Size(104, 47);
this.Name = "HipotParPort";
this.ResumeLayout(false);
} //public static void HipotParPort1(Boolean bBulbOn)
}
}
如何使用HIPOT程序
Hipot Interface程序用于控制HIOKI HIPOT测试设备。
注意:您必须移除所有外部I/O卡、启动/停止按钮等(如果存在)。因为它们在远程模式下将无法工作。此外,为了操作员安全,顶盖必须关闭。我曾向Hioki询问在远程模式下顶盖的使用情况;不幸的是,我没有收到任何回复。如果您运行程序,您将看到以下屏幕:
您必须点击“运行”按钮开始测试。如果被测设备(DUT)通过测试,表单上将显示一个绿色条。否则,您将看到一个红色条,表示测试失败,并弹出一个消息。
HIOKI 3153 自动绝缘电阻耐压测试仪设置
- 3153耐压测试仪的默认连接设置
- 串口:"COM1"
- 波特率:9600
- 奇偶校验:1
- 数据位:8
- 停止位:1
- 按以下初始设置排列耐压测试仪程序
- 发送任意字符使3153进入远程模式
- 设置耐压测试电压类型为AC 50 Hz
- 设置耐压测试电压为3.00kV
- 设置耐压截止电流上限为5.5 mA
- 设置耐压截止电流下限为0.7 mA
- 设置耐压斜坡计时器(测试时间)为3.0秒
- 设置耐压上升计时器为关闭
- 设置耐压下降计时器为关闭
- 设置绝缘测试电压类型为DC
- 设置绝缘测试电压为500V
- 设置绝缘电阻上限为关闭
- 设置绝缘电阻下限为4.0 Mohm
- 设置绝缘测试时间为1.0秒
- 设置绝缘延迟时间为关闭
- 设置测试模式:耐压模式 -> 绝缘电阻模式
- 如果测试结果为失败,则并行端口的D0引脚(#2)变为高电平。但您也可以设置其他引脚点亮。
- 如果测试结果为失败,并且要让测试仪发出蜂鸣声,则不会向耐压测试仪发送停止命令。要停止它,操作员必须单击“确定”按钮。
请注意,这些设置是通过手动尝试设备获得的。
3153 到 PC 接口
3153包含两个8位事件寄存器。可以通过读取这些寄存器来确定设备的状态。
事件寄存器在以下情况下被清除
- 执行"*cls"命令时
- 执行事件寄存器时
- 设备通电时
- 标准事件状态寄存器 (SESR) 位分配
- 3153不支持该命令
- 程序头有错误
- 数据参数数量错误
- 参数格式错误
- 指定的数据值超出设置范围
- 指定的数据值不可接受
- 当数据溢出输出队列时
- 当输出队列中的数据丢失时
- 事件状态寄存器 0 (ESR0) 位分配
- ESR0 = xxxx1100 = 0x0C = .12 ----> 测试完成并低于比较器下限(失败)
- ESR0 = xxxx1010 = 0x0A = .10 ----> 测试完成并高于比较器上限(失败)
- ESR0 = xxxx1001 = 0x09 = .9 -----> 测试完成并位于比较器限值内(通过)
- RS 232 命令参考
- CLS -----> 清除状态字节寄存器和事件寄存器
- ESR? -----> 返回标准事件状态寄存器 (SESR) 的内容,作为NR1格式的0到255之间的数值,然后清除SESR寄存器
- ESR0? -----> 返回事件状态寄存器0 (ESR0) 的值,作为NR1格式的0到255之间的数值,然后清除ESR0寄存器
位 7 PON |
电源开启标志 当电源开启或从断电恢复时,此位设置为1 |
位 6 | 未使用 |
位 5 CME |
命令错误 当收到的命令包含语法或语义错误时,此位设置为1 |
位 4 EXE |
执行错误 当收到的命令由于某种原因无法执行时,此位设置为1 |
位 3 DDE |
设备相关错误 当命令由于命令错误、查询错误或执行错误以外的某种原因无法执行时,此位设置为1 |
位 2 QYE |
查询错误 当输出队列控制检测到查询错误时,此位设置为1 |
位 1 | 未使用 |
位 0 | 未使用 |
位 7 | 未使用 |
位 6 | 未使用 |
位 5 | 未使用 |
位 4 | 未使用 |
位 3 EOM |
测试完成 |
位 2 LFAIL |
低于比较器下限 |
位 1 UFAIL |
高于比较器上限 |
位 0 PASS |
在比较器限制范围内 |
最后说明
我希望您觉得我的项目非常有用,并且可以实现其他设备。我尽量避免将所有可用的串口选项放入组合框的传统方式。通过研究3153到PC接口部分和我的代码,您可以了解如何实现这些功能。此外,请原谅我的英语。