int main(int argc, char **argv) { int socketFDA, socketFDB; ue9CalibrationInfo caliInfo; socketFDA = -1; socketFDB = -1; if(argc < 2) { printf("Please enter an ip address to connect to.\n"); exit(0); } else if(argc > 2) { printf("Too many arguments.\nPlease enter only an ip address.\n"); exit(0); } ipAddress = argv[1]; if( (socketFDA = openTCPConnection(ipAddress, ue9_portA)) < 0) goto exit; doFlush(socketFDA); if( (socketFDB = openTCPConnection(ipAddress, ue9_portB)) < 0) goto close; if(getCalibrationInfo(socketFDA, &caliInfo) < 0) goto close; if(StreamConfig_example(socketFDA) != 0) goto close; if(StreamStart(socketFDA) != 0) goto close; StreamData_example(socketFDA, socketFDB, &caliInfo); StreamStop(socketFDA, 1); close: if(closeTCPConnection(socketFDA) < 0) printf("Error: failed to close socket (portA)\n"); if(closeTCPConnection(socketFDB) < 0) printf("Error: failed to close socket (portB)\n"); exit: return 0; }
int main(int argc, char **argv) { int socketFD; ue9CalibrationInfo caliInfo; if(argc < 2) { printf("Please enter an ip address to connect to.\n"); exit(0); } else if(argc > 2) { printf("Too many arguments.\nPlease enter only an ip address.\n"); exit(0); } //Opening TCP connection to UE9 if( (socketFD = openTCPConnection(argv[1], ue9_port)) < 0) goto done; //Getting calibration information from UE9 if(getCalibrationInfo(socketFD, &caliInfo) < 0) goto close; allIO(socketFD, &caliInfo); close: if(closeTCPConnection(socketFD) < 0) { printf("Error: failed to close socket\n"); return 1; } done: return 0; }
int main(int argc, char **argv) { int socketFD; if(argc < 2) { printf("Please enter an ip address to connect to.\n"); exit(0); } else if(argc > 2) { printf("Too many arguments.\nPlease enter only an ip address.\n"); exit(0); } //Opening TCP connection to UE9 if( (socketFD = openTCPConnection(argv[1], ue9_port)) < 0 ) return 1; commConfig_example(socketFD); if(closeTCPConnection(socketFD) < 0) printf("Error: failed to close socket\n"); return 0; }
/*Di bawah ini fungsi untuk print perintah ke user. Kalo bikin fungsi lagi, di atasnya aja ya...*/ void Client::printSignUp() { cout << "Welcome to this chat application" << endl; cout << "Masukkan username baru\t: "; getline(cin, username); cout << "Masukkan password\t\t: "; getline(cin, password); cout << "Konfirmasi Password\t: "; getline (cin, confirmPassword); if(password.compare(confirmPassword) == 0) { char * buff; buff = new char[MAXBUF]; strcpy(buff,signup(username, password)); cout << (string)buff << endl; openTCPConnection(); setServerAddress((char*)"127.0.0.1"); reqConnect(); ConnectionHandler(buff); } else { cout << "Password dan konfirmasi password anda tidak sama" << endl; } }
int startConnectionUE9 (CONNECTION *ret) { printf("Trying to connect to UE9..."); //Opening TCP connection to UE9 if( ( (ret->socketFD = openTCPConnection(DEFAULTIP, DEFAULTPORT)) < 0)) //retunrs true if succesfull, native UE9 { return -1; } else{ //Getting calibration information from UE9 (Necessary for communication) if( (getCalibrationInfo(ret->socketFD, &ret->caliInfo) < 0)) { return -2; } else{ return 0; //Everything perfect } } }
void Client::printLogin() { char * buff; buff = new char[MAXBUF]; // string passwordCheck = "akhfa"; cout << "username: "******"password: "******"127.0.0.1"); reqConnect(); ConnectionHandler(buff); // len = send(sock,buff,strlen(buff),0); // len = -1; // bzero(buff,MAXBUF); // cout << len << endl; // cout << "buff: " << buff << endl; // cout << strlen(buff) << endl; // if (len>=0){ // len = recv(sock, buff , MAXBUF , 0); //receive message from user // if (strcmp(buff,"true")==0){ // cout << "yayyyyyyyyy" << endl; // setLoginStatus(true); // status = 1; // //// cout << "req connect.........." << endl; //// openTCPConnection(); //// setServerAddress((char*)"127.0.0.1"); //// reqConnect(); // } // } // len=-1; // if(password.compare(passwordCheck) == 0) // { // cout << "Success Login" << endl; // // } }
int main(int argc, char **argv) { int socketFD, i, j; ue9CalibrationInfo caliInfo; long error; double dblVoltage, dblIntTemp, TKelvin[NTHERMS]; long alngEnableTimers[6], alngTimerModes[6], alngEnableCounters[2], alngReadTimers[6]; long alngUpdateResetTimers[6], alngReadCounters[2], alngResetCounters[2]; double adblTimerValues[6], adblCounterValues[2]; char datafile[80]; int hour, lasthour; time_t timep; struct tm tm; FILE *data_fd, *tnow_fd, *cal_fd; double cal_coeffs[2][NTHERMS]; /* Calibration coefficients were determined from the night of 2005/05/15, calibrating using the HOBO */ // 26.011521 0.89395315 // 3.8793608 0.96553796 // 21.189119 0.89810403 // 24.789991 0.89390865 // There *must* be a better way to do this cal_coeffs[0][3] = 26.011521; cal_coeffs[1][3] = 0.89395315; cal_coeffs[0][2] = 3.8793608; cal_coeffs[1][2] = 0.96553796; cal_coeffs[0][1] = 21.189119; cal_coeffs[1][1] = 0.89810403; cal_coeffs[0][0] = 24.789991; cal_coeffs[1][0] = 0.89390865; cal_fd = fopen("ThermometerCalibrationConstants.txt", "w"); for (j=0; j<4; j++) { for (i=0; i<2; i++) { fprintf(cal_fd,"%12.8f ", cal_coeffs[i][j]); } fprintf(cal_fd,"\n"); } fprintf(cal_fd,"\n"); fflush(cal_fd); fclose(cal_fd); // Ugh. Can't remember how to do this. // cal_fd = fopen("ThermometerCalibrationConstants.txt", "r"); // for (j=0; j<4; j++) { // for (i=0; i<2; i++) { // cal_coeffs[i][j] = fscanf(cal_fd,"%12f "); // printf("%12.8f ",cal_coeffs[i][j]); // } // printf("\n"); // } // fclose(cal_fd); // for(i = 0; i < 6; i++) { alngEnableTimers[i] = 0; alngTimerModes[i] = 0; adblTimerValues[i] = 0.0; alngReadTimers[i] = 0; alngUpdateResetTimers[i] = 0; if(i < 2) { alngEnableCounters[i] = 0; alngReadCounters[i] = 0; alngResetCounters[i] = 0; adblCounterValues[i] = 0.0; } } if(argc < 2) { printf("Please enter an ip address to connect to.\n"); exit(0); } else if(argc > 2) { printf("Too many arguments.\nPlease enter only an ip address.\n"); exit(0); } //Opening TCP connection to UE9 printf("Opening TCP connection to %s\n",argv[1]); if( (socketFD = openTCPConnection(argv[1], ue9_port)) < 0) goto done; //Getting calibration information from UE9 printf("Getting calibration information.\n"); if(getCalibrationInfo(socketFD, &caliInfo) < 0) goto close; //Disable all timers and counters for(i = 0; i < 6; i++) alngEnableTimers[i] = 0; alngEnableCounters[0] = 0; alngEnableCounters[1] = 0; if((error = eTCConfig(socketFD, alngEnableTimers, alngEnableCounters, 0, 0, 0, alngTimerModes, adblTimerValues, 0, 0)) != 0) goto close; printf("Calling eTCConfig to disable all timers and counters\n"); time(&timep); tm = *localtime(&timep); // There is a way to get UT time ... // tm = *uttime(&timep); hour = tm.tm_hour; lasthour = hour; sprintf(datafile,"%04i%02i%02i_%02i%02i.txt", tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min); data_fd = fopen(datafile, "w"); printf("\nBegin acquiring data.\n"); printf("Type ctrl-C to quit.\n"); while(1) { time(&timep); tm = *localtime(&timep); hour = tm.tm_hour; if (hour != lasthour){ fclose(data_fd); sprintf(datafile,"%04i%02i%02i_%02i00.txt", tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour); data_fd = fopen(datafile, "w"); } fprintf(data_fd,"%04i/%02i/%02i %02i:%02i:%02i ", tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); dblIntTemp = 0; if(readIntTemp(socketFD, &caliInfo, &dblIntTemp) < 0) goto close; fprintf(data_fd,"%.3f ",dblIntTemp); // Read the voltage from AIN0-3 using 0-5 volt range at 16 bit resolution for (i=0; i<NTHERMS; i++) { if((error = eAIN(socketFD, &caliInfo, i, 0, &dblVoltage, LJ_rgUNI5V, 16, 0, 0, 0, 0)) != 0) goto close; TKelvin[i] = (dblVoltage * 100) * cal_coeffs[1][i] + cal_coeffs[0][i]; } // Print data out to file for (i=0; i<NTHERMS; i++) { fprintf(data_fd,"%10.3f ", TKelvin[i]); } fprintf(data_fd,"\n"); fflush(data_fd); tnow_fd = fopen("TNow","w"); for (i=0; i<NTHERMS; i++) { fprintf(tnow_fd,"%10.3f ", TKelvin[i]); } fprintf(tnow_fd,"\n"); fflush(tnow_fd); fclose(tnow_fd); sleep(1); // usleep(1000); lasthour = hour; } close: if(error > 0) printf("Received an error code of %ld\n", error); if(closeTCPConnection(socketFD) < 0) { printf("Error: failed to close socket\n"); return 1; } done: return 0; }
//Reads the StreamData low-level function response in a loop. All voltages from //the stream are stored in the voltages 2D array. int StreamData_example(int socketFDA, int socketFDB, ue9CalibrationInfo *caliInfo) { uint8 *recBuff; double **voltages; int recChars, backLog, overflow, totalScans, ret; int i, k, m, packetCounter, currChannel, scanNumber; int totalPackets; //The total number of StreamData responses read uint16 voltageBytes, checksumTotal; int numDisplay; //Number of times to display streaming information int readSizeMultiplier; //Multiplier for the StreamData receive buffer size long startTime, endTime; packetCounter = 0; currChannel = 0; scanNumber = 0; totalPackets = 0; recChars = 0; numDisplay = 6; readSizeMultiplier = 120; ret = 0; /* Each StreamData response contains (16/NumChannels) * readSizeMultiplier * samples for each channel. * Total number of scans = (16 / NumChannels) * readSizeMultiplier * numDisplay */ totalScans = (16/NumChannels)*readSizeMultiplier*numDisplay; voltages = malloc(sizeof(double)*totalScans); for(i = 0; i < totalScans; i++) voltages[i] = malloc(sizeof(double)*NumChannels); recBuff = malloc(sizeof(uint8)*46*readSizeMultiplier); printf("Reading Samples...\n"); startTime = getTickCount(); for (i = 0; i < numDisplay; i++) { /* You can read the multiple StreamData responses of 46 bytes to help * improve throughput. In this example this multiple is adjusted by the * readSizeMultiplier variable. We may not read 46 * readSizeMultiplier * bytes per each recv call, but we will continue reading until we read * 46 * readSizeMultiplier bytes total. */ recChars = 0; for(k = 0; k < 46*readSizeMultiplier; k += recChars) { //Reading response from UE9 recChars = recv(socketFDB, recBuff + k, 46*readSizeMultiplier - k, 0); if(recChars == 0) { printf("Error : read failed (StreamData).\n"); ret = -1; goto cleanmem; } } overflow = 0; //Checking for errors and getting data out of each StreamData response for (m = 0; m < readSizeMultiplier; m++) { totalPackets++; checksumTotal = extendedChecksum16(recBuff + m*46, 46); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[m*46 + 5]) { printf("Error : read buffer has bad checksum16(MSB) (StreamData).\n"); ret = -1; goto cleanmem; } if( (uint8)(checksumTotal & 0xff) != recBuff[m*46 + 4]) { printf("Error : read buffer has bad checksum16(LBS) (StreamData).\n"); ret = -1; goto cleanmem; } checksumTotal = extendedChecksum8(recBuff + m*46); if( checksumTotal != recBuff[m*46]) { printf("Error : read buffer has bad checksum8 (StreamData).\n"); ret = -1; goto cleanmem; } if( recBuff[m*46 + 1] != (uint8)(0xF9) || recBuff[m*46 + 2] != (uint8)(0x14) || recBuff[m*46 + 3] != (uint8)(0xC0) ) { printf("Error : read buffer has wrong command bytes (StreamData).\n"); ret = -1; goto cleanmem; } if(recBuff[m*46 + 11] != 0) { printf("Errorcode # %d from StreamData read.\n", (unsigned int)recBuff[11]); ret = -1; goto cleanmem; } if(packetCounter != (int)recBuff[m*46 + 10]) { printf("PacketCounter (%d) does not match with with current packet count (%d) (StreamData).\n", packetCounter, (int)recBuff[m*46 + 10]); ret = -1; goto cleanmem; } backLog = recBuff[m*46 + 45] & 0x7F; //Checking MSB for Comm buffer overflow if( (recBuff[m*46 + 45] & 128) == 128) { printf("\nComm buffer overflow detected in packet %d\n", totalPackets); printf("Current Comm backlog: %d\n", recBuff[m*46 + 45] & 0x7F); overflow = 1; } for(k = 12; k < 43; k += 2) { voltageBytes = (uint16)recBuff[m*46 + k] + (uint16)recBuff[m*46 + k+1] * 256; binaryToCalibratedAnalogVoltage(caliInfo, (uint8)(0x00), ainResolution, voltageBytes, &(voltages[scanNumber][currChannel])); currChannel++; if(currChannel > 3) { currChannel = 0; scanNumber++; } } if(packetCounter >= 255) packetCounter = 0; else packetCounter++; //Handle Comm buffer overflow by stopping, flushing and restarting stream if(overflow == 1) { printf("\nRestarting stream...\n"); doFlush(socketFDA); closeTCPConnection(socketFDB); if( (socketFDB = openTCPConnection(ipAddress, ue9_portB)) < 0) goto cleanmem; if(StreamConfig_example(socketFDA) != 0) { printf("Error restarting StreamConfig.\n"); ret = -1; goto cleanmem; } if(StreamStart(socketFDA) != 0) { printf("Error restarting StreamStart.\n"); ret = -1; goto cleanmem; } packetCounter = 0; break; } } printf("\nNumber of scans: %d\n", scanNumber); printf("Total packets read: %d\n", totalPackets); printf("Current PacketCounter: %d\n", ((packetCounter == 0) ? 255 : packetCounter-1)); printf("Current Comm backlog: %d\n", backLog); for(k = 0; k < 4; k++) printf(" AI%d: %.4f V\n", k, voltages[scanNumber - 1][k]); } endTime = getTickCount(); printf("\nRate of samples: %.0lf samples per second\n", (scanNumber*NumChannels)/((endTime - startTime)/1000.0)); printf("Rate of scans: %.0lf scans per second\n\n", scanNumber/((endTime - startTime)/1000.0)); cleanmem: free(recBuff); recBuff = NULL; for(i = 0; i < totalScans; i++) { free(voltages[i]); voltages[i] = NULL; } free(voltages); voltages = NULL; return ret; }