// AnalogInputTrigger_AI_Scan_Cplusplus.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include "..\Lib\HSDAQ.h" #pragma comment(lib,"..\\Lib\\HSDAQ.lib") HANDLE hHS; char IP[20],ConnectConfig[30],str[50]; WORD Databuff[30000000]; void CharToWide(LPCSTR astr, LPTSTR wstr); int _tmain(int argc, _TCHAR* argv[]) { int temp; bool ret = false; char str[50]; int ch = 0; unsigned short gVal = 0; short oVal = 0; unsigned short gVarr[8]; short oVarr[8]; short ChCnt = 0; short Gain = 0; short TrigMode = 6; //6 : Analog Input Mode long sampleRate = 0; unsigned long targetCnt = 0; short DataTransMethod = 0; short AutoRun = 0; unsigned int Status = 0; unsigned int readsize = 0; unsigned long totalRecv = 0; unsigned long Reserv = 0; unsigned int i = 0; int analogmode=-1; int file_count=0; bool En_Channel[8]; float hightriglevel[8]; float lowtriglevel[8]; unsigned long leftsidecnt=0; unsigned long rightsidecnt=0; CString infoBuf=""; char LOG_PATH[256]; TCHAR TC_LOG_PATH[256],File_Full_Path[256]; HANDLE ofile; GetCurrentDirectory(200,infoBuf.GetBuffer(256)); infoBuf.ReleaseBuffer(); sprintf(LOG_PATH,"%S",infoBuf); printf("PET-7H16M Analog Input Trigger demo\n"); printf("Please input IP (xxx.xxx.xxx.xxx) !!\n"); while(1) { scanf("%s",IP); sprintf(ConnectConfig,"%s,9999,10010,0,0",IP); hHS = HS_Device_Create(ConnectConfig); if (hHS < 0) { printf("Invalid IP, please input again.\n"); } else { Sleep(100); break; } } HS_GetSDKVersion(str); printf("HSDAQ SDK Version = %s\n", str); HS_GetFirmwareVersion(hHS,str); printf("ET7H16 Firmware Versoin = %s\n",str); for (ch = 0; ch < 8; ch++) { //Read Gain Offset valus for each channel HS_ReadGainOffset(hHS, ch, &gVal, &oVal); printf("ch : %d Gain Value : %u Offset Valus : %d\n", ch,gVal,oVal); gVarr[ch] = gVal; oVarr[ch] = oVal; } printf("Please input number of channels for sample data(1 ~ 8)\n"); scanf("%d",&ChCnt); printf("Please Select AI range\n"); printf("0 : +/- 5V\n"); printf("1 : +/- 10V\n"); scanf("%d",&Gain); printf("Please input sample rate(1 Hz ~ 200 KHz)\n"); scanf("%ld",&sampleRate); printf("Please input Target Count(1 ~ 30000000)\n"); scanf("%lu",&targetCnt); printf("Please input Analog Input Trigger Mode\n"); printf("0 : Above\n"); printf("1 : Below\n"); printf("2 : Out-Range\n"); printf("3 : In-Range\n"); scanf("%d",&analogmode); printf("Enable/Disable the channels for AI trigger\n"); printf("0 : disable\n"); printf("1 : enable\n"); for(ch=0;ch<8;ch++) { printf("Channel %d \n",ch); scanf("%d",&En_Channel[ch]); } printf("Input high limit for each channel\n"); if(Gain==0) printf("Range : +/- 5V\n"); else printf("Range : +/- 10V\n"); for(ch=0;ch<8;ch++) { if(En_Channel[ch]==1) { printf("Channel %d \n",ch); scanf("%f",&hightriglevel[ch]); } } printf("Input low limit for each channel\n"); if(Gain==0) printf("Range : +/- 5V\n"); else printf("Range : +/- 10V\n"); for(ch=0;ch<8;ch++) { if(En_Channel[ch]==1) { printf("Channel %d \n",ch); scanf("%f",&lowtriglevel[ch]); } } while(1) { printf("Input the counts that need to read before trigged, 'Before-Trigger-Counts' and 'After-Trigger-Counts' must add up to be equal to 'Target Count'\n"); scanf("%lu",&leftsidecnt); printf("Input the counts that need to read after trigged, 'Before-Trigger-Counts' and 'After-Trigger-Counts' must add up to be equal to 'Target Count'\n"); scanf("%lu",&rightsidecnt); if((leftsidecnt+rightsidecnt)!=targetCnt) { printf("'Before-Trigger-Counts' and 'After-Trigger-Counts' is not equal to 'Target Count'\nPlease input parameters again\n\n"); } else break; } ret = HS_SetAIScanParam(hHS, ChCnt, Gain, TrigMode, sampleRate, targetCnt, DataTransMethod, AutoRun); if (!ret) { printf("HS_SetAIScanParam failed Error code1 0x%x \n", HS_GetLastError()); return 0; } ret = HS_SetAIAnalogTriggerParam(hHS,analogmode,En_Channel,hightriglevel,lowtriglevel,8,leftsidecnt,rightsidecnt,0); if (!ret) { printf("HS_SetAIDelayTriggerParam failed Error code1 0x%x",HS_GetLastError()); return 0; } else { HS_GetAIScanParam(hHS, &ChCnt, &Gain, &TrigMode, &sampleRate, &targetCnt, &DataTransMethod, &AutoRun); HS_GetAIAnalogTriggerParam(hHS,&analogmode,En_Channel,hightriglevel,lowtriglevel,8,&leftsidecnt,&rightsidecnt,&Reserv); printf("Set AI Scan : Ch count = %d Gain = %d TrigMode = %d sampleRate = %lu targetCnt = %lu\n",ChCnt,Gain,TrigMode,sampleRate,targetCnt); printf("Analog Input Trigger Mode = %d Before-Trigger-Counts = %lu After-Trigger-Counts = %lu\n",analogmode,leftsidecnt,rightsidecnt); for(ch=0;ch<8;ch++) { if(En_Channel[ch]==1) printf("CH %d Enable High Limit %2.3f Low Limit %2.3f\n",ch,hightriglevel[ch],lowtriglevel[ch]); else printf("CH %d Disable High Limit %2.3f Low Limit %2.3f\n",ch,hightriglevel[ch],lowtriglevel[ch]); } HS_StartLogger(hHS,LOG_PATH, 2, 0); if (!ret) { printf("HS_StartAIScan failed Error code1 0x%x \n", HS_GetLastError()); return 0; } while (1) { ret = HS_GetTotalSamplingStatus(hHS, &totalRecv,&Status); if (ret == false) { printf("Error code3 0x%x",HS_GetLastError()); } else { if ((Status >>7)&0x01 == 1) { break; } else if ((Status >>2)&0x01 == 1) { printf("[error] AD_DATA_SAMPLING_TIMEOUT \n"); break; } else if ((Status >>1)&0x01 == 1) { printf("[error] AD_BUF_OVERFLOW \n"); break; } else if ((Status & 0x01) == 0x01) { printf("Received Count : %lu Status :%X\r",totalRecv,Status); } else { printf("Sampling Count : %lu Status :%X\r",totalRecv,Status); } } Sleep(10); } HS_StopLogger(hHS); CharToWide(LOG_PATH,TC_LOG_PATH); file_count=HS_GetAllLogFiles(TC_LOG_PATH,0); ofile=HS_LogFile_Open_byIndex(file_count-1,File_Full_Path); ret=HS_GetLogFile_AIDataHex(ofile,0,targetCnt,Databuff); HS_LogFile_Close(ofile); printf("\nTotal files %d\nFile Full Path %S\n",file_count,File_Full_Path); printf("\nIndex"); for (i = 0; i < ChCnt; i++) { printf("|CH%1d ",i); } for (i = 0; i < totalRecv; i++) { if(i%ChCnt==0) printf("\n%5d", i); printf("|%04X", Databuff[i]); if (i % ChCnt == ChCnt - 1) { printf("\n"); } } printf("\r\nAP total read = %lu\r\n",totalRecv ); printf("\n"); printf("Press any key to quit program"); } HS_Device_Release(hHS); getchar(); getchar(); return 0; } void CharToWide(LPCSTR astr, LPTSTR wstr) { MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, astr, -1, wstr, 2048); }