// ActuatorDemo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include #include "../Actuator//Actuator.h" //系数 const double coef[5] = { -6.204673384923,0.025161776479,-0.000014765722,0.000000003440,0 }; void ExeTest() { void* hPlcDev = CreatePlcDevice("192.168.0.10", 502); void* hWrDev = CreateWrDevice("COM1", 115200, 'N', 8, 1); unsigned short nState = 0; unsigned short nGrainType = 0; int ret = 0; if (hPlcDev) ret = ConnectDevice(hPlcDev); if (hWrDev) ret = ConnectDevice(hWrDev); char ch = 0; while ('c' != (ch = getchar())) //按 c 退出自动检测 { //读PLC状态字 ret = ReadState(hPlcDev, &nState, &nGrainType); if (ret <= 0) { printf("读取PLC状态字异常!\n"); } else { //代表读取到数据 if (nGrainType > 0 && nState > 0) { //下面是主流程的处理 switch (nState) { case READY_GRAIN: //粮食品种修改了 { //向水分仪发粮种更改,启动准备 ret = SendCommands(hWrDev, WR_READY, nGrainType, 20, 0); if (ret <= 0) { printf("粮食品种修改失败!水分仪故障!\n"); ret = ResponseState(hPlcDev, DEVICE_WR_FAULT); } else { printf("粮食品种修改成功!准备就绪\n"); ret = ResponseState(hPlcDev, READY_FINISH); } } break; case DETECT_START: //开始测试 { ret = SendCommands(hWrDev, WR_DETECT, 0, 30, 0); if (ret <= 0) { printf("执行检测失败!\n"); ret = ResponseState(hPlcDev, DEVICE_WR_FAULT); } else { unsigned short nDatas[10] = { 0 }; unsigned short nSendData[3] = { 0 }; //从水分仪器读取数据 ret = ReadDatas(hWrDev, nDatas); printf("执行检测成功!\n"); ret = ResponseState(hPlcDev, 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(hPlcDev, nSendData); } } break; case DEVICE_WR_RESET: //复位命令 { ret = SendCommands(hWrDev, WR_RESET, 0, 20, 0); if (ret <= 0) { printf("复位失败!\n"); ret = ResponseState(hPlcDev, DEVICE_WR_FAULT); } else { printf("复位成功!\n"); } } break; default: break; } } } } if (hPlcDev) Disconnect(hPlcDev); if (hPlcDev) DestroyDevice(hPlcDev); if (hWrDev) Disconnect(hWrDev); if (hWrDev) DestroyDevice(hWrDev); } //手动写参数 void ExeWriteParam() { void* hWrDev = CreateWrDevice("COM6", 115200, 'N', 8, 1); unsigned short nState = 0; unsigned short nGrainType = 0; int ret = 0; if (hWrDev) ret = ConnectDevice(hWrDev); ret = WriteParams(hWrDev, 10500, 12000, 50, 1, 1); if (ret <= 0) printf("下发参数失败!\n"); if (hWrDev) DestroyDevice(hWrDev); } void ExeManReady() { void* hWrDev = CreateWrDevice("COM6", 115200, 'N', 8, 1); unsigned short nState = 0; unsigned short nGrainType = 0; int ret = 0; if (hWrDev) ret = ConnectDevice(hWrDev); ret = SendCommands(hWrDev, WR_READY, 0x01, 20, 0); //下发确认 if (ret <= 0) printf("准备失败!\n"); else { printf("准备就绪,OK!\n"); } if (hWrDev) DestroyDevice(hWrDev); } //手动检测 void ExeManTest() { void* hWrDev = CreateWrDevice("COM6", 115200, 'N', 8, 1); unsigned short nState = 0; unsigned short nGrainType = 0; int ret = 0; if (hWrDev) ret = ConnectDevice(hWrDev); ret = SendCommands(hWrDev, WR_DETECT, 0, 30, 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"); } if (hWrDev) DestroyDevice(hWrDev); } void DirectDetect(char nType) { void* hWrDev = CreateWrDevice("COM6", 115200, 'N', 8, 1); unsigned short nState = 0; unsigned short nGrainType = nType-48; int ret = 0; if (nGrainType < 1 && nGrainType>4) { printf("粮食信息非法!\r\n"); return; } if (hWrDev) ret = ConnectDevice(hWrDev); ret = SendCommands(hWrDev, WR_GOODS, nGrainType); //执行启动命令 if (ret <= 0) { printf("下发粮种信息失败!\n"); } else { ret = SendCommands(hWrDev, WR_DETECT, 0, 30, 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(" Orig Val: %d %d %d %d", nDatas[0], nDatas[1], nDatas[2], nDatas[3]); printf(" -->Water Rate:%lf", v); printf(" \n"); printf("检测执行OK!\n"); } } if (hWrDev) DestroyDevice(hWrDev); } // void ExeVerifyTest() { void* hWrDev = CreateWrDevice("COM6", 115200, 'N', 8, 1); unsigned short nState = 0; unsigned short nGrainType = 0; int ret = 0; if (hWrDev) ret = ConnectDevice(hWrDev); ret = SendCommands(hWrDev, WR_VERIFY, 0, 30, 0); //执行校准命令 if (ret <= 0) { printf("校准执行失败!\n"); } else { unsigned short nVcc = 0; //从水分仪器读取数据 ret = ReadVcc(hWrDev, &nVcc); printf(" Detected Val: %d", nVcc); printf(" \n"); printf("校准执行OK!\n"); } if (hWrDev) DestroyDevice(hWrDev); } void ExeZero() { void* hWrDev = CreateWrDevice("COM6", 115200, 'N', 8, 1); unsigned short nState = 0; unsigned short nGrainType = 0; int ret = 0; if (hWrDev) ret = ConnectDevice(hWrDev); ret = SendCommands(hWrDev, WR_RESET, 0, 15, 0); //下发复位 if (ret <= 0) { printf("复位失败!\n"); } else { printf("复位成功!\n"); } if (hWrDev) DestroyDevice(hWrDev); } void ReadWeight() { void* hWrDev = CreateWrDevice("COM1", 115200, 'N', 8, 1); int ret = 0; if (hWrDev) ret = ConnectDevice(hWrDev); unsigned short nWeight = 0; //从水分仪器读取数据 ret = ReadWeight(hWrDev, &nWeight); if (ret >= 0) { printf(" Weight Val: %d", nWeight); printf(" \n"); printf("称重执行OK!\n"); } else { printf("称重执行失败!\n"); } if (hWrDev) DestroyDevice(hWrDev); } void TestLinearize() { int nSampeCount = 12; float x[]={0,1,2,3,4,5,6,7,8,9,10,11}; //realval float y[]={2506,2200,1846,1440,1004,548,79,-387,-847,-1288,-1700,-2066}; //input double coef[5] = { 0 }; int ret = Linearize(x, y, coef, 2, nSampeCount); if (ret > 0) { printf("Coef: %f, %f, %f, %f, %f\n", coef[0], coef[1], coef[2], coef[3], coef[4]); float v = 3.0f; printf("Input %lf, Cal Val:%lf\n", v, (coef[0] + coef[1] * v + coef[2] * v * v + coef[3] * v * v * v + coef[4] * v * v * v * v) ); } } int main() { std::cout << "Hello World!\n"; //double val = 11531998.626; //unsigned char sval[32] = { 0 }; //memcpy(sval, &val, 8); //unsigned char sv2[] = { 0x27, 0x31, 0x08, 0xd4, 0xdb, 0xfe, 0x65, 0x41 }; //val = *(double*)sv2; // //unsigned short sv3[] = { 0x3127, 0xd408, 0xfedb, 0x4165 }; //val = *(double*)sv3; //unsigned short sv4[] = { 0x2731, 0x08d4, 0xdbfe, 0x6541 }; //val = *(double*)sv4; //unsigned short sv5[] = { 0x4165, 0xfedb, 0xd408, 0x3127 }; //val = *(double*)sv5; char ch = 0; while ('x' != ( ch = getchar() )) //按 x 退出程序 { switch (ch) { case 'a': //自动测试 ExeTest(); break; case 'p': //向水份仪下发参数 ExeWriteParam(); break; case 'r': //手动检测,需要准备 ExeManReady(); break; case 'd': //手动检测,不许准备 ExeManTest(); break; case 'v': //校准 ExeVerifyTest(); break; case 'z': //复位清零 ExeZero(); break; case 'l': //拟合测试 TestLinearize(); break; case '1': case '2': case '3': case '4': DirectDetect(ch); break; case 'w': ReadWeight(); break; default: break; } } } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件