ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Chroma Meter CS-200 C++仪表控制软件

2022-03-01 10:31:06  阅读:136  来源: 互联网

标签:200 usb ++ Chroma file printf input txt cBuf


#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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有