如上图所示
我自己做的一个信号转换工具,可以转换4-20mA、0-20mA电流型,
可以转换0-5V电压型、-5 - 5V、0-10V、-10 - 10V
并将它们转换为工程实际值以及到PLC中的码值。
下图中设置工程的量程为0-100;
码值上下范围为4000-20000;
若输入一个实际的工程值,则将自己运算出码值和实际的电流或电压值!
如上图转换结果红色的字体部分。
部分源码如下:
void CSignalConvertDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData();
int index=-1;
index = ((CComboBox*)GetDlgItem(IDC_COMBO1))->GetCurSel();
// ((CComboBox*)GetDlgItem(IDC_COMBO1)->GetLBText(index,m_salary);
switch (index)
{
case 0 :
m_MaxSignal=20;
m_MinSignal=4;
convert();
break;
case 1 :
m_MaxSignal=5;
m_MinSignal=1;
convert();
break;
case 2 :
m_MaxSignal=20;
m_MinSignal=0;
convert();
break;
case 3 :
m_MaxSignal=5;
m_MinSignal=0;
convert();
break;
case 4 :
m_MaxSignal=5;
m_MinSignal=-5;
convert();
break;
case 5 :
m_MaxSignal=10;
m_MinSignal=0;
convert();
break;
case 6 :
m_MaxSignal=10;
m_MinSignal=-10;
convert();
default:
m_MaxSignal=20;
m_MinSignal=4;
convert();
break;
}
UpdateData(false);
}
void CSignalConvertDlg::convert(/*double& m_MaxCodeValue,
double& m_MinCodeValue, double& m_MaxSignal, double& m_MinSignal*/)
{
if (m_maxProjectValue==0)
m_projectResult=0;
static int i=-1;++i;
bool j = FALSE;
if (i==0)
j = FALSE;
else j = m_changeValue[3]==1 || m_changeValue[4]==1 || m_changeValue[5]==1 || m_changeValue[6]==1 || m_changeValue[7]==1;
if (j || m_changeValue[0]==1)
{
if (m_maxProjectValue>0)
{
m_Signalresult=(m_MaxSignal-m_MinSignal)*(m_projectResult-m_minProjectValue)/(m_maxProjectValue-m_minProjectValue)+m_MinSignal;
m_codeResult = (m_MaxCodeValue-m_MinCodeValue)*(m_Signalresult-m_MinSignal)/(m_MaxSignal-m_MinSignal)+m_MinCodeValue;
m_changeValue[0]=0;
m_changeValue[4]=0;
m_changeValue[5]=0;
m_changeValue[6]=0;
m_changeValue[7]=0;
}
}
if(j || m_changeValue[1]==1)
{
m_Signalresult = (m_MaxSignal-m_MinSignal)*(m_codeResult-m_MinCodeValue)/(m_MaxCodeValue-m_MinCodeValue)+m_MinSignal;
m_changeValue[1]=0;
m_changeValue[3]=0;
m_changeValue[4]=0;
m_changeValue[5]=0;
m_changeValue[6]=0;
m_changeValue[7]=0;
if(m_maxProjectValue>0)
m_projectResult=(m_maxProjectValue-m_minProjectValue)*(m_Signalresult-m_MinSignal)/(m_MaxSignal-m_MinSignal)+m_minProjectValue;
}
if (j || m_changeValue[2]==1)
{
m_codeResult = (m_MaxCodeValue-m_MinCodeValue)*(m_Signalresult-m_MinSignal)/(m_MaxSignal-m_MinSignal)+m_MinCodeValue;
m_changeValue[2]=0;
m_changeValue[3]=0;
m_changeValue[4]=0;
m_changeValue[5]=0;
m_changeValue[6]=0;
m_changeValue[7]=0;
if(m_maxProjectValue>0)
m_projectResult=(m_maxProjectValue-m_minProjectValue)*(m_Signalresult-m_MinSignal)/(m_MaxSignal-m_MinSignal)+m_minProjectValue;
}
}
结束。