利用RS232的DTR(4脚,输出)、DSR(6脚,输入)和CTS(8脚,输入)
注:这里还可以用RTS(7脚)来代替DTR,而作为输入的还可以用DCD(1脚,输入)和Ring(9脚,输入)
下面的例子运行后,用一导线短接4脚和1脚、4脚和6脚、4脚和8脚、4脚和9脚。
VB脚本:
Option
Explicit
Private Sub Form_Load()
MSComm1.PortOpen = True
MSComm1.DTREnable = True
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvCD ' Change in the CD line.
Debug.Print " CD "
Case comEvCTS ' Change in the CTS line.
Debug.Print " CTS "
Case comEvDSR ' Change in the DSR line.
Debug.Print " DSR "
Case comEvRing ' Change in the Ring Indicator.
Debug.Print " Ring "
End Select
End Sub
Private Sub Form_Load()
MSComm1.PortOpen = True
MSComm1.DTREnable = True
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvCD ' Change in the CD line.
Debug.Print " CD "
Case comEvCTS ' Change in the CTS line.
Debug.Print " CTS "
Case comEvDSR ' Change in the DSR line.
Debug.Print " DSR "
Case comEvRing ' Change in the Ring Indicator.
Debug.Print " Ring "
End Select
End Sub
C++脚本:(用的victor 控件)
AnsiString __fastcall Victor_CommDevice::CommDeviceCommNotify(
int
SBtn,
bool
Test)
{
// 动态结构体赋值
stcom -> CTS = CurCommDevice -> CTS;
stcom -> DCD = CurCommDevice -> DCD;
stcom -> DSR = CurCommDevice -> DSR;
stcom -> Ring = CurCommDevice -> Ring;
if ((stcom -> CTS || stcom -> DCD || stcom -> DSR || stcom -> Ring) && ! down)
{ // 每次按下只处理一次事件
down = true ;
try
{
if (SBtn == 1 )
{
ComFlag = " CTS+DCD+DSR+RLSD " ;
Process();
}
else if (SBtn > 1 )
{
if (stcom -> CTS)
{
ComFlag = " CTS " ;
CTS_Process();
}
if (stcom -> DCD)
{
ComFlag = " DCD " ;
DCD_Process();
}
if (stcom -> DSR)
{
ComFlag = " DSR " ;
DSR_Process();
}
if (stcom -> Ring)
{
ComFlag = " Ring " ;
RLSD_Process();
}
if (Test)
{
return ComFlag;
}
}
}
catch (Exception & e)
{
PMSGDLG -> ShowErrorMsg( " 无法指定的重载函数! 错误原因: " + e.Message);
if (Test)
{
return "" ;
}
Abort();
}
}
else if ( ! stcom -> CTS && ! stcom -> DCD && ! stcom -> DSR && ! stcom -> Ring)
{ // 弹开或者其它自动还原数据
down = false ;
if (Test)
{
return "" ;
}
}
}
{
// 动态结构体赋值
stcom -> CTS = CurCommDevice -> CTS;
stcom -> DCD = CurCommDevice -> DCD;
stcom -> DSR = CurCommDevice -> DSR;
stcom -> Ring = CurCommDevice -> Ring;
if ((stcom -> CTS || stcom -> DCD || stcom -> DSR || stcom -> Ring) && ! down)
{ // 每次按下只处理一次事件
down = true ;
try
{
if (SBtn == 1 )
{
ComFlag = " CTS+DCD+DSR+RLSD " ;
Process();
}
else if (SBtn > 1 )
{
if (stcom -> CTS)
{
ComFlag = " CTS " ;
CTS_Process();
}
if (stcom -> DCD)
{
ComFlag = " DCD " ;
DCD_Process();
}
if (stcom -> DSR)
{
ComFlag = " DSR " ;
DSR_Process();
}
if (stcom -> Ring)
{
ComFlag = " Ring " ;
RLSD_Process();
}
if (Test)
{
return ComFlag;
}
}
}
catch (Exception & e)
{
PMSGDLG -> ShowErrorMsg( " 无法指定的重载函数! 错误原因: " + e.Message);
if (Test)
{
return "" ;
}
Abort();
}
}
else if ( ! stcom -> CTS && ! stcom -> DCD && ! stcom -> DSR && ! stcom -> Ring)
{ // 弹开或者其它自动还原数据
down = false ;
if (Test)
{
return "" ;
}
}
}