// ActuatorDemo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
|
//
|
|
#include <iostream>
|
#include <thread>
|
#include <windows.h>
|
#include "../Actuator//Actuator.h"
|
|
/******************* 为PLC自动监测准备相关全部变量******************************** */
|
static bool plcScan = true;
|
static bool devRunning = false;
|
/*************************************************************************************
|
**************************************************************************************/
|
|
|
//系数
|
const double coef[5] = { -6.204673384923,0.025161776479,-0.000014765722,0.000000003440,0 };
|
const char sPortName[] = "COM6";
|
|
//手动写粮食品种
|
void ManReady()
|
{
|
if (devRunning){printf("水分仪忙!"); return;}
|
|
devRunning = true;
|
void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1);
|
if (hWrDev)
|
{
|
int ret = ConnectDevice(hWrDev);
|
|
if (ret >= 0)
|
{
|
|
ret = SendCommands(hWrDev, WR_READY, 0x01, 0, 500000); //下发确认
|
|
if (ret <= 0)
|
printf("准备失败!\n");
|
else
|
{
|
printf("准备就绪,OK!\n");
|
}
|
}
|
|
Disconnect(hWrDev);
|
DestroyDevice(hWrDev);
|
}
|
|
devRunning = false;
|
}
|
|
//手动检测
|
void ManTest()
|
{
|
if (devRunning) { printf("水分仪忙!"); return; }
|
|
devRunning = true;
|
|
void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1);
|
if (hWrDev)
|
{
|
int ret = ConnectDevice(hWrDev);
|
|
if (ret >= 0)
|
{
|
ret = SendCommands(hWrDev, WR_DETECT, 0, 60, 0); //执行启动命令
|
|
if (ret <= 0)
|
{
|
printf("执行检测失败!\n");
|
}
|
else
|
{
|
unsigned short nDatas[10] = { 0 };
|
|
//从水分仪器读取数据
|
ret = ReadDatas(hWrDev, nDatas);
|
|
//第一个值为电平值,用来计算水分率
|
double v = coef[0] +
|
coef[1] * nDatas[0] +
|
coef[2] * nDatas[0] * nDatas[0] +
|
coef[3] * nDatas[0] * nDatas[0] * nDatas[0] +
|
coef[4] * nDatas[0] * nDatas[0] * nDatas[0] * nDatas[0];
|
|
v = CalcValue(nDatas[0], coef[0], coef[1], coef[2], coef[3], coef[4]);
|
|
printf(" After Detection,");
|
printf(" %d %d %d %d", nDatas[0], nDatas[1], nDatas[2], nDatas[3]);
|
printf(" Water Rate:%lf", v);
|
printf(" \n");
|
printf("检测执行OK!\n");
|
|
}
|
}
|
|
Disconnect(hWrDev);
|
DestroyDevice(hWrDev);
|
}
|
|
devRunning = false;
|
}
|
|
//手动复位
|
void ManReset()
|
{
|
if (devRunning) { printf("水分仪忙!"); return; }
|
|
devRunning = true;
|
|
void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1);
|
|
if (hWrDev)
|
{
|
int ret = ConnectDevice(hWrDev);
|
|
if (ret >= 0)
|
{
|
ret = SendCommands(hWrDev, WR_RESET, 0, 15, 0); //下发复位
|
|
if (ret <= 0)
|
{
|
printf("复位失败!\n");
|
}
|
else
|
{
|
printf("复位成功!\n");
|
}
|
}
|
|
Disconnect(hWrDev);
|
DestroyDevice(hWrDev);
|
}
|
|
devRunning = false;
|
}
|
|
void GetWeight()
|
{
|
if (devRunning) { printf("水分仪忙!"); return; }
|
|
devRunning = true;
|
|
void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1);
|
|
if (hWrDev)
|
{
|
|
int ret = ConnectDevice(hWrDev);
|
|
|
unsigned short nWeight = 0;
|
|
if (ret >= 0)
|
{
|
//从水分仪器读取数据
|
ret = ReadWeight(hWrDev, &nWeight);
|
|
if (ret >= 0)
|
{
|
printf(" Weight Val: %d", nWeight);
|
printf(" \n");
|
printf("称重执行OK!\n");
|
}
|
else
|
{
|
printf("称重执行失败!\n");
|
}
|
}
|
|
Disconnect(hWrDev);
|
DestroyDevice(hWrDev);
|
}
|
|
devRunning = false;
|
}
|
|
void CurrVersion()
|
{
|
if (devRunning) { printf("水分仪忙!"); return; }
|
|
devRunning = true;
|
|
void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1);
|
if (hWrDev)
|
{
|
int ret = ConnectDevice(hWrDev);
|
|
//unsigned char nVersions[2] = { 0 };
|
unsigned short nVersions;
|
|
if (ret >= 0)
|
{
|
//从水分仪器读取数据
|
ret = ReadVersion(hWrDev, &nVersions);
|
|
if (ret >= 0)
|
{
|
printf(" Version1 Val: %d", nVersions & 0x00FF);
|
printf(" \n");
|
printf(" Version2 Val: %d", nVersions >> 8);
|
printf(" \n");
|
}
|
else
|
{
|
printf("版本执行失败!\n");
|
}
|
}
|
|
Disconnect(hWrDev);
|
DestroyDevice(hWrDev);
|
}
|
|
devRunning = false;
|
}
|
|
void ExecPlcReady(void* hPlc, int nGrainType)
|
{
|
if (devRunning) { printf("水分仪忙!"); return; }
|
|
devRunning = true;
|
|
void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1);
|
if (hWrDev)
|
{
|
int ret = ConnectDevice(hWrDev);
|
|
if (ret >= 0)
|
{
|
//向水分仪发粮种更改,启动准备
|
ret = SendCommands(hWrDev, WR_READY, nGrainType, 0, 500000);
|
if (ret < 0)
|
{
|
printf("粮食品种修改失败!水分仪故障!\n");
|
ret = ResponseState(hPlc, DEVICE_WR_FAULT);
|
}
|
else
|
{
|
printf("粮食品种修改成功!准备就绪\n");
|
ret = ResponseState(hPlc, READY_FINISH);
|
}
|
}
|
|
Disconnect(hWrDev);
|
DestroyDevice(hWrDev);
|
}
|
|
devRunning = false;
|
}
|
void ExecPlcDetect(void* hPlc, int nTimeout /*以秒计*/)
|
{
|
if (devRunning) { printf("水分仪忙!"); return; }
|
|
devRunning = true;
|
|
void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1);
|
if (hWrDev && hPlc)
|
{
|
int ret = ConnectDevice(hWrDev);
|
|
if (ret >= 0)
|
{
|
ret = SendCommands(hWrDev, WR_DETECT, 0, nTimeout, 0);
|
if (ret <= 0)
|
{
|
printf("执行检测失败!\n");
|
ret = ResponseState(hPlc, DEVICE_WR_FAULT);
|
}
|
else
|
{
|
unsigned short nDatas[10] = { 0 };
|
unsigned short nSendData[3] = { 0 };
|
|
//从水分仪器读取数据
|
ret = ReadDatas(hWrDev, nDatas);
|
|
printf("执行检测成功!\n");
|
ret = ResponseState(hPlc, DETECT_FINISH);
|
|
//计算水分率
|
double v = coef[0] +
|
coef[1] * nDatas[0] +
|
coef[2] * nDatas[0] * nDatas[0] +
|
coef[3] * nDatas[0] * nDatas[0] * nDatas[0] +
|
coef[4] * nDatas[0] * nDatas[0] * nDatas[0] * nDatas[0];
|
|
|
nSendData[0] = 0x01; // 粮食品种
|
nSendData[1] = 100 * v; // 21.30% //含水率
|
nSendData[2] = 100 * nDatas[1] / 0.52; // 51.50; //容重比
|
|
printf("Get Vals: wr:%f, weight:%f, temperatur:%f, humidity:%f",
|
v,
|
nDatas[1],
|
nDatas[2] / 100.f,
|
nDatas[3] / 100.f);
|
|
ret = UploadData(hPlc, nSendData);
|
}
|
|
}
|
|
Disconnect(hWrDev);
|
|
DestroyDevice(hWrDev);
|
}
|
|
devRunning = false;
|
}
|
void ExecPlcReset(void* hPlc, int nTimeout /*以秒计*/)
|
{
|
if (devRunning) { printf("水分仪忙!"); return; }
|
|
devRunning = true;
|
|
void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1);
|
if (hWrDev)
|
{
|
int ret = ConnectDevice(hWrDev);
|
|
if (ret >= 0)
|
{
|
//向水分仪发粮种更改,启动准备
|
ret = SendCommands(hWrDev, WR_RESET, 0, nTimeout, 0);
|
if (ret <= 0)
|
{
|
printf("复位失败!\n");
|
ret = ResponseState(hPlc, DEVICE_WR_FAULT);
|
}
|
else
|
{
|
printf("复位成功!\n");
|
}
|
}
|
|
Disconnect(hWrDev);
|
DestroyDevice(hWrDev);
|
}
|
|
devRunning = false;
|
}
|
|
static void ExecPLCCommand()
|
{
|
|
unsigned short nPlcCommandState = 0;
|
unsigned short nPlcCommandGrainType = 0;
|
float fPlcCommandDrift = 0;
|
|
bool ret = -1;
|
void* hPlcDev = CreatePlcDevice("127.0.0.1", 1502);
|
if (hPlcDev) ret = ConnectDevice(hPlcDev);
|
|
while (plcScan) //按 c 退出自动检测
|
{
|
ret = ReadState(hPlcDev, &nPlcCommandState, &nPlcCommandGrainType);
|
if (ret <= 0)
|
{
|
printf("读取PLC状态字异常!\n");
|
}
|
else
|
{
|
//读PLC状态字
|
if (nPlcCommandGrainType > 0 && nPlcCommandState > 0)
|
{
|
//下面是主流程的处理
|
switch (nPlcCommandState)
|
{
|
case READY_GRAIN: //粮食品种修改了
|
ExecPlcReady(hPlcDev, nPlcCommandGrainType);
|
break;
|
case DETECT_START: //开始测试
|
ExecPlcDetect(hPlcDev, 60);
|
break;
|
case DEVICE_WR_RESET: //复位命令
|
ExecPlcReset(hPlcDev, 10);
|
break;
|
}
|
}
|
}
|
/*一秒读取PLC一次信息*/
|
Sleep(1000);
|
}
|
|
}
|
|
int main()
|
{
|
|
std::cout << "水分仪测试程序已开启!\n";
|
|
char ch = 0;
|
while ('x' != ( ch = getchar() )) //按 x 退出程序
|
{
|
switch (ch)
|
{
|
case 'a': //自动测试
|
{
|
std::thread workThread = std::thread(ExecPLCCommand);
|
workThread.detach();
|
}
|
break;
|
case 'r': //手动检测,需要准备
|
ManReady();
|
break;
|
case 'd': //手动检测,不许准备
|
ManTest();
|
break;
|
case 'z': //复位清零
|
ManReset();
|
break;
|
case 'w':
|
GetWeight();
|
break;
|
case 'V':
|
CurrVersion();
|
break;
|
default:
|
break;
|
}
|
|
}
|
|
return 1;
|
}
|