Chroma Meter CS-200 C++仪表控制软件
作者:互联网
#include "stdafx.h"
#include <Windows.h>
#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
typedef INT (CALLBACK* USB_INI)(INT);
typedef INT (CALLBACK* USB_NUM)(VOID);
typedef INT (CALLBACK* USB_IO)(INT, LPSTR, INT, INT);
HINSTANCE hDll = LoadLibrary("kmsecs200.dll");
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CWinApp theApp;
using namespace std;
USB_INI int_usb = (USB_INI)GetProcAddress(hDll, "int_usb");
USB_INI end_usb = (USB_INI)GetProcAddress(hDll, "end_usb");
USB_IO write64_usb = (USB_IO)GetProcAddress(hDll, "write64_usb");
USB_IO read64_usb = (USB_IO)GetProcAddress(hDll, "read64_usb");
char soft_name[]={"cs-200色彩分析仪数据读取程序(VS6 VC++) V2.0\r\n设计开发:检测校准中心 仪表工程室 ZYB 2022.3.1\r\n软件通过input.txt和result.txt同其它软件进行交互\r\ninput.txt文件写入:mes测量、con连接仪表、ext断开仪表\r\n"};
char cRemote[]={"RMT,1\r\n"};
char cRemote0[]={"RMT,0\r\n"};
char cBuf[250];
char cMes[]={"MES,1\r\n"};
char cMdr[]={"MDR,0\r\n"};
char cZero[]={"ZRC\r\n"};
char input[3];
void conn(){
printf("\r\n收到连接仪表信号con\r\n");
write64_usb(0, cRemote, 1, 7);
read64_usb(0, cBuf, 1, 250);
ofstream file;
file.open("input.txt",ios::trunc);
file.close();
}
void exit(){
printf("\r\n收到断开仪表信号ext\r\n");
write64_usb(0, cRemote0, 1, 7);
read64_usb(0, cBuf, 1, 250);
ofstream file;
file.open("input.txt",ios::trunc);
file.close();
}
void getdata(){
printf("\r\n收到检测启动信号mes,开始检测\r\n");
CString str(cBuf);
while (1)
{
write64_usb(0, cMes, 1, 7);
read64_usb(0, cBuf, 1, 250);
// Get measuring time
CString str2(cBuf);
if (str2.Left(4) == _T("ER02")){
Sleep(300);
}else
{
str=cBuf;
printf(cBuf);
break;
}
}
int nTime = atoi(str.Mid(5, 2));
// Create wait time of (measuring time - 0.5)s
DWORD dwTime = (DWORD)(nTime*1000 - 500);
Sleep(dwTime);
// 基于当前系统的当前日期/时间
time_t now = time(0);
// 把 now 转换为字符串形式
char* dt = ctime(&now);
printf("%s读取数据\r\n",dt);
while(1)
{
write64_usb(0, cMdr, 1, 7);
read64_usb(0, cBuf, 1, 250);
CString str(cBuf);
//ER02\n 或
//OK00,0,2,1, 1,0, 0,0,20, 8.642, 0.3159, 0.3526
if (str.Left(4) == _T("ER02"))
{
// measuring
// Wait 0.3s before resending command
Sleep(300);
}else{
// OK or ER
break;
}
}
printf(cBuf);
//将检测结果写入输出文件result.txt
printf("已将检测结果写入到文件result.txt\r\n");
ofstream file;
file.open("result.txt",ios::trunc);
file<<cBuf;
file.close();
}
FILE *fp; /*定义文件指针*/
//控制台消息响应,定时器响应
void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
if(1 != idEvent)
{
//读取input.txt文件,有内容就开始检测并将文件内容清空
if((fp=fopen("input.txt","r"))==NULL) /*以只读方式打开指定文件*/
{
printf("无法读取input.txt文件, error=%d/n", GetLastError());
fclose(fp);
return;
}
if(!feof(fp))
{
if(fread(&input,3,1,fp)==1)
{
CString str(input);
if (str.Left(3) == _T("mes"))
{
//进行检测并输出结果
fclose(fp);
ofstream file;
file.open("input.txt",ios::trunc);
//file<<"aaaa"<<endl;
file.close();
getdata();
}
else if (str.Left(3) == _T("ext"))
{
exit();
}
else if (str.Left(3) == _T("con"))
{
conn();
}
else
{
}
}
}
fclose(fp);
}
else
{
//cout << (LPCTSTR)info3 << endl;
}
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
return 1;
}
// TODO: code your application's behavior here.
cout<<soft_name<<endl;
printf("初始化设备\r\n");
int i=int_usb(0);
if (i==0)
printf("连接仪表成功\r\n");
else{
printf("连接仪表失败\r\n");
getchar();
return 1;
}
printf("进入远程模式\r\n");
write64_usb(0, cRemote, 1, 7);
read64_usb(0, cBuf, 1, 250);
printf(cBuf);
//校零
//printf("开始校零\r\n");
// write64_usb(0, cZero, 1, 5);
// read64_usb(0, cBuf, 1, 250);
//printf(cBuf);
printf("等待开始检测信号输入(input.txt文件写入mes)\r\n");
int TimerID = 1;//Timer的ID是1
int peried = 300;//Timer的间隔是 300ms
//设置Timer
::SetTimer(NULL, TimerID, peried, &TimerProc);
::MSG msg;
while(::GetMessage(&msg, NULL, 0, 0))
{
::DispatchMessage(&msg);
}
return nRetCode;
}
标签:200,usb,++,Chroma,file,printf,input,txt,cBuf 来源: https://blog.csdn.net/zybpub/article/details/123199657