//OpenPort------------------------------- //Initialize COM port, make stepper motor's current position home (0) //Input: None //Ouput: RS232 error code int zStepperDriver::OpenPort() { int RS232Error; char out[5]; char ptName[4]; //Initialize comm port CloseCom(prevPort); sprintf(ptName,"COM%i ",port); RS232Error = OpenComConfig (port, ptName, 9600, 0, 8, 1, 512, 512); SetXMode (port, 0); SetCTSMode (port, 1); SetComTime (port, 10); sprintf(out, "HM0\r"); ComWrt(port, out, strlen(out)); //set previous port as newly opened port prevPort = port; return RS232Error; }
int RKC_CommData ( int _ACT, int _ID, double *_Val) { int rLop = 0; int RTNval = 0; int BuffLEN = 0; char Buff[128]={0}; char BBC = 0x00; char dTRN[64] = {0}; static int Rs232Err = 0, NullPort = 0; switch (_ACT) { case Config: CloseCom(rkc.COM); if (Rs232Err = ReturnRS232Err (), Rs232Err < 0) return Rs232Err; OpenComConfig(rkc.COM, "", rkc.Baud, rkc.Pari, rkc.Dbit, rkc.Sbit, 512, 512); if (Rs232Err = ReturnRS232Err (), Rs232Err < 0) { NullPort = 1; return Rs232Err; } SetCTSMode(rkc.COM, rkc.Flc); if (Rs232Err = ReturnRS232Err (), Rs232Err < 0) return Rs232Err; SetComTime(rkc.COM, rkc.Tout); if (Rs232Err = ReturnRS232Err (), Rs232Err < 0) return Rs232Err; NullPort = 0; break; case getTP: Fmt(Buff, "%c%i[w2p0]%s[w2]%c", _EOT, (_ID & 0xFF), "M1", _ENQ); // MessagePopup("getTP",Buff); //DJ_Added_0502KB_12_57 break; case getSP: Fmt(Buff, "%c%i[w2p0]%s[w2]%c", _EOT, (_ID & 0xFF), "S1", _ENQ); break; case getAL1: Fmt(Buff, "%c%i[w2p0]%s[w2]%c", _EOT, (_ID & 0xFF), "A1", _ENQ); break; case getAL2: Fmt(Buff, "%c%i[w2p0]%s[w2]%c", _EOT, (_ID & 0xFF), "A2", _ENQ); break; case getAT: Fmt(Buff, "%c%i[w2p0]%s[w2]%c", _EOT, (_ID & 0xFF), "G1", _ENQ); break; case getST: Fmt(Buff, "%c%i[w2p0]%s[w2]%c", _EOT, (_ID & 0xFF), "G2", _ENQ); break; case setRUN: Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "SR", *_Val, _ETX); break; case setSP: Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "S1", *_Val, _ETX); break; case setAL1: Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "A1", *_Val, _ETX); break; case setAL2: Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "A2", *_Val, _ETX); break; case setAT: Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "G1", *_Val, _ETX); break; case setST: Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "G2", *_Val, _ETX); break; case setPB: Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "PB", *_Val, _ETX); break; } if (NullPort) { *_Val = 0.00; } // Command Transfer Process if ((_ACT > 0x1F) && (NullPort == 0)) { // ========================================================= [ Set RKC Data ] // Flush Out Queue FlushOutQ (rkc.COM); Delay(0.5); // Count Buffer Length BuffLEN = StringLength (Buff); // Calc BBC BBC = 0x00; for (rLop=4; rLop < BuffLEN; rLop++) BBC ^= Buff[rLop]; // Append BBC Fmt ( Buff, "%s[a]<%c", BBC); BuffLEN +=1; // Sending Command ComWrt ( rkc.COM, Buff, BuffLEN); // Flush In Queue FlushInQ (rkc.COM); Delay(0.5); ComRd (rkc.COM, dTRN, 1); if (dTRN[0] == 0x06) RTNval = 0; // Sending OK else if (dTRN[0] == 0x15) RTNval = -10; // Sending Lost else RTNval = -1; // Connect Lost } else if ((_ACT > 0x0F) && (NullPort == 0)) { // ========================================================= [ Get RKC Data ] // Flush OUT Queue FlushOutQ (rkc.COM); Delay(0.5); // Send CMD ComWrt ( rkc.COM, Buff, 6); // Flush IN Queue FlushInQ (rkc.COM); Delay(0.5); // Receive CMD dTRN[0] = 0x00; ComRd (rkc.COM, dTRN, 11); Rs232Err = ReturnRS232Err(); if (ReturnRS232Err() == 0) Scan (dTRN ,"%s[i5]>%f", _Val); Delay(0.5); Fmt ( Buff, "%c%c", _EOT, 0x00); ComWrt (rkc.COM, Buff, 1); } return RTNval; }
int post(int run) { int ret_val; int test_item, index; int byte_in_queue ; // old: int byte_in_queue ; char portname[6]; //char hwID_limit[5] = {" "} ; char pos_capture[3000]; //char char_read ; double begin, end ; float testtime ; FillBytes(portname, 0, 5, 32) ; portname[5] = '\0' ; sprintf(portname,"COM%d",g_dev_serial_port) ; //test_item = InsertTestItem("RESET test", NULL, NULL, NULL, NULL); ret_val = ConfirmMessage ("Turn unit back on", "Press the Power button and the Reset button. Did the three LEDs flash?"); /******************************************************************************** if (ret_val == YES) UpdateTestItem(test_item, "RESET test", NULL, NULL, NULL, "PASS") ; else UpdateTestItem(test_item, "RESET test", NULL, NULL, NULL, "FAIL"); ****************************************************************************/ Delay(1.0) ; //test_item = InsertTestItem("Power On Self Test",NULL,NULL,NULL,NULL); begin = Timer() ; FillBytes(pos_capture, 0, 2999, 32); pos_capture[2999] = '\0' ; test_item = InsertTestItem("Opening device port",NULL,NULL,NULL,NULL); ret_val = OpenComConfig (g_dev_serial_port, portname, 115200, 0, 8, 1, 512, 512); if (ret_val < 0) { UpdateTestItem(test_item, "Opening UUT port", "ERROR", NULL, NULL, "FAIL") ; return FAILURE ; } else UpdateTestItem(test_item, "Opening UUT port", NULL, NULL, NULL, "PASS") ; if (!run) { ComWrtByte (1, 'n'); } else { ComWrtByte (1, 'Y'); test_item = InsertTestItem("Power On Self Test",NULL,NULL,NULL,NULL); SetComTime(g_dev_serial_port, 5.0) ; FlushInQ(g_dev_serial_port) ; FlushOutQ(g_dev_serial_port) ; //ComRd(g_dev_serial_port, pos_capture, 3000) ; for (index = 0; index < 2999; index++) { byte_in_queue = ComRdByte(g_dev_serial_port) ; if (byte_in_queue < 0) break ; // no more byte to read if ( (byte_in_queue == CR) || (byte_in_queue == LF) ) pos_capture[index] = 32 ; else pos_capture[index] = (char) byte_in_queue; } // for all chars in COM buffer index = FindPattern(pos_capture, 0, -1, "failed", 0, 0) ; if (index > 0) { //test_item = InsertTestItem("Power On Self Test",NULL,NULL,NULL,"FAILED"); UpdateTestItem(test_item,NULL,NULL,NULL,NULL,"FAIL"); return FAILURE ; } else //test_item = InsertTestItem("Power On Self Test",NULL,NULL,NULL,"PASS"); UpdateTestItem(test_item,NULL,NULL,NULL,NULL,"PASS"); } // else, perform post test return SUCCESS ; }
/* In another thread, receives data and writes them into TSQ */ int CVICALLBACK receiveDataWriteToTSQ(void *functionData){ int storeFileFlag = 0; GetCtrlVal(panelHdl, MainPanel_StoreTheData_Switch, &storeFileFlag); if(0 == dataSrc){ //read data from [device port] if(OpenComConfig(comPort, "", baudRate, parity, dataBits, stopBits, inQueueSize, outQueueSize) <0) showError("Failed to open RS232 port!"); if(storeFileFlag > 0){ if( writeFPP >0){ writeFP = fopen(writeFilePath, "a"); if(writeFP == NULL) showError("Open WriteFile Failed"); fseek(writeFP, writeFPP, SEEK_SET); }else{ SYSTEMTIME localTime; char dirName[MAX_PATHNAME_LEN]; char fileName[200]; GetProjectDir(dirName); strcat(dirName, "\\DataStorage"); GetLocalTime(&localTime); sprintf(fileName, "%04d-%d-%d,%d_%02d_%02d.dat", localTime.wYear, localTime.wMonth, localTime.wDay, localTime.wHour, localTime.wMinute, localTime.wSecond); MakePathname(dirName, fileName, writeFilePath); writeFP = fopen(writeFilePath, "a"); if(writeFP == NULL) showError("Open WriteFile Failed"); } }//if(storeFileFlag) }else if(1 == dataSrc){ //read data from [file] loadFP = fopen(loadFilePath, "r"); if(loadFP == NULL){ showError("Load File Failed"); return -1; } fseek(loadFP, loadFPP, SEEK_SET); }else{ //[simulating data] } GetCtrlVal(panelHdl, MainPanel_StoreTheData_Switch, &storeFileFlag); while(receiveFlag){ writeTSQEndFlag = 1; switch(dataSrc){ case 0: bytesRead = ComRd(comPort, readData, READ_LENGTH); if(storeFileFlag > 0) fwrite(readData, sizeof(char), READ_LENGTH, writeFP); break; case 1: Delay(fileLoadSpeed); if(!feof(loadFP)){ bytesRead = fread(readData, sizeof(char), READ_LENGTH, loadFP); }else{ receiveFlag = 0; MessagePopup("Reach The End", "All data in this file has been read !"); } break; case 2: for(int i=0; i<READ_LENGTH; i++) readData[i] = 0x80; Delay(0.1); break; }//switch() CmtWriteTSQData(tsqHdl, &readData, READ_LENGTH, TSQ_INFINITE_TIMEOUT, NULL); writeTSQEndFlag = 0; }//while() switch(dataSrc){ case 0: //CloseCom(comPort); if(storeFileFlag >0){ if(!feof(writeFP)) writeFPP = ftell(writeFP); fclose(writeFP); writeFP = NULL; } break; case 1: if(!feof(loadFP)) loadFPP = ftell(loadFP); fclose(loadFP); loadFP = NULL; break; }//switch() addLog("End-Acq", 0, panelHdl); return 0; }