首先讲接线,要触发CTS等引脚的事件,必须对CTS等引脚施加一个高电平。高电平的来源可以来自第四脚,同时必须在软件中对第四脚置高电平。
C#部分代码如下
mSP.PortName = "COM1";
mSP.BaudRate = 9600;
mSP.DtrEnable = true; //这里是对DTR置高电平
mSP.RtsEnable = true; //这里是对RTS置高电平
mSP.Open();
mSP.PinChanged += mSP_PinChanged;
int mSPPinCount = 0;
StringBuilder mSB = new StringBuilder();
System.DateTime mDTPinLast = System.DateTime.Now;
void mSP_PinChanged(object sender, System.IO.Ports.SerialPinChangedEventArgs e)
{
if (((System.TimeSpan)(System.DateTime.Now - mDTPinLast)).TotalMilliseconds < HealthyExamination.Common.BaseC.GlobeValues.LocalParas.CaptureSerialPortPinDelayMS)
{
return;
}
mDTPinLast = System.DateTime.Now;
mSPPinCount++;
//引脚 简写 功能说明
//1 CD 载波侦测(Carrier Detect)
//2 RXD 接收数据(Receive)
//3 TXD 发送数据(Transmit)
//4 DTR 数据终端准备(Data Terminal Ready)
//5 GND 地线(Ground)
//6 DSR 数据准备好(Data Set Ready)
//7 RTS 请求发送(Request To Send)
//8 CTS 清除发送(Clear To Send)
//9 RI 振铃指示(Ring Indicator)
if (e.EventType == System.IO.Ports.SerialPinChange.CtsChanged) //引脚8
{
mSB.Insert(0, System.DateTime.Now.ToString("mm:ss:ffff") + "\t" + "CtsChanged mSPPinCount=" + mSPPinCount.ToString() + "\r\n");
}
else if (e.EventType == System.IO.Ports.SerialPinChange.CDChanged) //引脚1?
{
mSB.Insert(0, System.DateTime.Now.ToString("mm:ss:ffff") + "\t" + "CDChanged mSPPinCount=" + mSPPinCount.ToString() + "\r\n");
}
else if (e.EventType == System.IO.Ports.SerialPinChange.DsrChanged) //引脚6
{
mSB.Insert(0, System.DateTime.Now.ToString("mm:ss:ffff") + "\t" + "DsrChanged mSPPinCount=" + mSPPinCount.ToString() + "\r\n");
}
else if (e.EventType == System.IO.Ports.SerialPinChange.Ring) //引脚9
{
mSB.Insert(0, System.DateTime.Now.ToString("mm:ss:ffff") + "\t" + "Ring mSPPinCount=" + mSPPinCount.ToString() + "\r\n");
}
else if (e.EventType == System.IO.Ports.SerialPinChange.Break) //引脚
{
mSB.Insert(0, System.DateTime.Now.ToString("mm:ss:ffff") + "\t" + "Break mSPPinCount=" + mSPPinCount.ToString() + "\r\n");
}
else
{
mSB.Insert(0, System.DateTime.Now.ToString("mm:ss:ffff") + "\t" + "Others mSPPinCount=" + mSPPinCount.ToString() + "\r\n");
}
}