String connectSendSend(String data, String path, String servername, String datatype, int port){ int counter = 0; String response; String tcpHeader = "POST "+path+" HTTP/1.1\r\nHost:"+servername+"\r\nContent-Type:"+datatype+"\r\nContent-Length:"+String(data.length())+"\r\n"; while(!connectToServer(servername, port)){ counter++; if(counter>=5){ SerialUSB.println("Wi-Fi Connection to Server Failed!"); return "Error: 8"; //Wi-Fi Connection to Server Failed! } } response = sendAndReceive(data,path,servername,datatype); response = response + "\n" + sendAndReceive(data,path,servername,datatype); counter = 0; while(!closeConnection()){ counter++; if(counter>=5){ SerialUSB.println("Wi-Fi Server Disconnection Failed!"); return "Error: 11"; //Wi-Fi Server Disconnection Failed! } } return response; }
asynStatus PIGCSMotorController::getStatus(PIasynAxis* pAxis, int& homing, int& moving, int& negLimit, int& posLimit, int& servoControl) { char buf[255]; asynStatus status = sendAndReceive(char(4), buf, 99); if (status != asynSuccess) { return status; } // TODO this is for a single axis C-863/867 controller!!!! // TODO a) change it to multi-axis code. // TODO b) support other controllers which do not understand #4 or have different bit masks int idx = 2 + pAxis->getAxisNo()*4; buf[idx+4] = '\0'; char* szMask = buf+idx; long mask = strtol(szMask, NULL, 16); moving = (mask & 0x2000) ? 1 : 0; homing = (mask & 0x4000) ? 1 : 0; negLimit = (mask & 0x0001) ? 1 : 0; posLimit = (mask & 0x0004) ? 1 : 0; servoControl = (mask & 0x1000) ? 1 : 0; asynPrint(m_pCurrentLogSink, ASYN_TRACE_FLOW, "PIGCSMotorController::getStatus() buf:%s moving %d, svo: %d\n", buf, moving, servoControl); return status; }
void S2Controller::sendX() { const QString xQ = QString("-x"); cmdLineEdit->setText(xQ); sendAndReceive(xQ); QTimer::singleShot(1000, this, SLOT(cleanUp())); }
static int motorAxisSetInteger(AXIS_HDL pAxis, motorAxisParam_t function, int value) { int ret_status = MOTOR_AXIS_ERROR; int status; char buff[100]; if (pAxis == NULL) return(MOTOR_AXIS_ERROR); epicsMutexLock(pAxis->mutexId); switch (function) { case motorAxisClosedLoop: /* The MM4000 only allows turning on and off ALL motors (MO and MF commands), */ /* not individual axes. Don't implement */ if (pAxis->pController->model == MM4000) ret_status = MOTOR_AXIS_OK; else { if (value == 0) { int axisStatus, powerOn = 0; int offset = (pAxis->axis * 5) + 3; /* Offset in status string */ sprintf(buff, "%dMF", pAxis->axis + 1); ret_status = sendOnly(pAxis->pController, buff); /* Wait for Power to come on. */ while (powerOn == 0) { ret_status = sendAndReceive(pAxis->pController, "MS;", buff, sizeof(buff)); axisStatus = buff[offset]; if (!(axisStatus & MM4000_POWER_OFF)) powerOn = 1; else epicsThreadSleep(0.1); } } else { sprintf(buff, "%dMO", pAxis->axis+1); ret_status = sendOnly(pAxis->pController, buff); } } break; default: PRINT(pAxis->logParam, MOTOR_ERROR, "motorAxisSetInteger: unknown function %d\n", function); break; } if (ret_status != MOTOR_AXIS_ERROR) { status = motorParam->setInteger(pAxis->params, function, value); motorParam->callCallback(pAxis->params); } epicsMutexUnlock(pAxis->mutexId); return(ret_status); }
void S2Controller::posMon(){ // send current query string if (!s2ParameterMap[ii].getExpectedType().contains("derived")){ sendAndReceive(s2ParameterMap[ii].getSendString()); }else{ posMonListener(""); } //after delay, emit signal and return }
int RemoteRobot::main() { unsigned char* sendData = 0, * receivedData; int sendSize = 0, receivedSize = 0; MessageQueue temp; { // If there is something to send, prepare a package if(!theDebugSender.isEmpty()) { SYNC; OutBinarySize size; size << theDebugSender; sendSize = size.getSize(); sendData = new unsigned char[sendSize]; OutBinaryMemory stream(sendData); stream << theDebugSender; // make backup theDebugSender.moveAllMessages(temp); } } // exchange data with the router if(!sendAndReceive(sendData,sendSize,receivedData,receivedSize) && sendSize) { // sending failed, restore theDebugSender SYNC; // move all messages since cleared (if any) theDebugSender.moveAllMessages(temp); // restore temp.moveAllMessages(theDebugSender); } // If a package was prepared, remove it if(sendSize) { delete [] sendData; sendSize = 0; } // If a package was received from the router program, add it to receiver queue if(receivedSize > 0) { SYNC; InBinaryMemory stream(receivedData,receivedSize); stream >> theDebugReceiver; delete [] receivedData; }
void actionsClient(int sockfd){ char sendline[MAXLINE], recvline[MAXLINE]; memset(&recvline[0], 0, sizeof(recvline)); if (read(sockfd, recvline, MAXLINE) == 0){ printf("Server terminated prematurely\n"); exit(0); } fputs(recvline, stdout); while (fgets(sendline, MAXLINE, stdin) != NULL && sendAndReceive(sockfd, sendline)) memset(&sendline[0], 0, sizeof(sendline)); }
//verification si la reponse contient le code 354 au debut si oui saisie mail jusqu'à la saisie de " . " void isDataCommand(char * recv, int sockfd){ char reponse[3] = {"354"}; int k; bool equal = true; char sendline[MAXLINE]; if(strlen(recv) > 3){ for(k=0; k<strlen(reponse); k++){ if(recv[k] != reponse[k]){ equal = false; break; } } if(equal){ printf("Saisie du message:\n"); while(fgets(sendline, MAXLINE, stdin) != NULL && !isEndMail(sendline)){ write(sockfd, sendline, strlen(sendline)+1); memset(&sendline[0], 0, sizeof(sendline)); } sendAndReceive(sockfd, "\r\n.\r\n"); } } }
int MM4000AsynConfig(int card, /* Controller number */ const char *portName, /* asyn port name of serial or GPIB port */ int asynAddress, /* asyn subaddress for GPIB */ int numAxes, /* Number of axes this controller supports */ int movingPollPeriod, /* Time to poll (msec) when an axis is in motion */ int idlePollPeriod) /* Time to poll (msec) when an axis is idle. 0 for no polling */ { AXIS_HDL pAxis; int axis; MM4000Controller *pController; char threadName[20]; int status; int totalAxes; int loopState; int digits; int modelNum; int retry = 0; char *p, *tokSave; char inputBuff[BUFFER_SIZE]; char outputBuff[BUFFER_SIZE]; if (numMM4000Controllers < 1) { printf("MM4000Config: no MM4000 controllers allocated, call MM4000Setup first\n"); return MOTOR_AXIS_ERROR; } if ((card < 0) || (card >= numMM4000Controllers)) { printf("MM4000Config: card must in range 0 to %d\n", numMM4000Controllers-1); return MOTOR_AXIS_ERROR; } if ((numAxes < 1) || (numAxes > MM4000_MAX_AXES)) { printf("MM4000Config: numAxes must in range 1 to %d\n", MM4000_MAX_AXES); return MOTOR_AXIS_ERROR; } pController = &pMM4000Controller[card]; pController->pAxis = (AXIS_HDL) calloc(numAxes, sizeof(motorAxis)); pController->numAxes = numAxes; pController->movingPollPeriod = movingPollPeriod/1000.; pController->idlePollPeriod = idlePollPeriod/1000.; status = pasynOctetSyncIO->connect(portName, asynAddress, &pController->pasynUser, NULL); if (status != asynSuccess) { printf("MM4000AsynConfig: cannot connect to asyn port %s\n", portName); return MOTOR_AXIS_ERROR; } do { status = sendAndReceive(pController, "VE;", inputBuff, sizeof(inputBuff)); retry++; /* Return value is length of response string */ } while (status != asynSuccess && retry < 3); if (status != asynSuccess) return (MOTOR_AXIS_ERROR); strcpy(pController->firmwareVersion, &inputBuff[2]); /* Skip "VE" */ /* Set Motion Master model indicator. */ p = strstr(pController->firmwareVersion, "MM"); if (p == NULL) { printf("MM4000AsynConfig: invalid model = %s\n", pController->firmwareVersion); return MOTOR_AXIS_ERROR; } modelNum = atoi(p+2); if (modelNum == 4000) pController->model = MM4000; else if (modelNum == 4005 || modelNum == 4006) pController->model = MM4005; else { printf("MM4000AsynConfig: invalid model = %s\n", pController->firmwareVersion); return MOTOR_AXIS_ERROR; } sendAndReceive(pController, "TP;", inputBuff, sizeof(inputBuff)); /* The return string will tell us how many axes this controller has */ for (totalAxes = 0, tokSave = NULL, p = epicsStrtok_r(inputBuff, ",", &tokSave); p != 0; p = epicsStrtok_r(NULL, ",", &tokSave), totalAxes++) ; if (totalAxes < numAxes) { printf("MM4000AsynConfig: actual number of axes=%d < numAxes=%d\n", totalAxes, numAxes); return MOTOR_AXIS_ERROR; } for (axis=0; axis<numAxes; axis++) { pAxis = &pController->pAxis[axis]; pAxis->pController = pController; pAxis->card = card; pAxis->axis = axis; pAxis->mutexId = epicsMutexMustCreate(); pAxis->params = motorParam->create(0, MOTOR_AXIS_NUM_PARAMS); /* Determine if encoder present based on open/closed loop mode. */ sprintf(outputBuff, "%dTC", axis+1); sendAndReceive(pController, outputBuff, inputBuff, sizeof(inputBuff)); loopState = atoi(&inputBuff[3]); /* Skip first 3 characters */ if (loopState != 0) pAxis->closedLoop = 1; /* Determine drive resolution. */ sprintf(outputBuff, "%dTU", axis+1); sendAndReceive(pController, outputBuff, inputBuff, sizeof(inputBuff)); pAxis->stepSize = atof(&inputBuff[3]); digits = (int) -log10(pAxis->stepSize) + 2; if (digits < 1) digits = 1; pAxis->maxDigits = digits; /* Save home preset position. */ sprintf(outputBuff, "%dXH", axis+1); sendAndReceive(pController, outputBuff, inputBuff, sizeof(inputBuff)); pAxis->homePreset = atof(&inputBuff[3]); /* Determine low limit */ sprintf(outputBuff, "%dTL", axis+1); sendAndReceive(pController, outputBuff, inputBuff, sizeof(inputBuff)); pAxis->lowLimit = atof(&inputBuff[3]); /* Determine high limit */ sprintf(outputBuff, "%dTR", axis+1); sendAndReceive(pController, outputBuff, inputBuff, sizeof(inputBuff)); pAxis->highLimit = atof(&inputBuff[3]); } pController->pollEventId = epicsEventMustCreate(epicsEventEmpty); /* Create the poller thread for this controller */ epicsSnprintf(threadName, sizeof(threadName), "MM4000:%d", card); epicsThreadCreate(threadName, epicsThreadPriorityMedium, epicsThreadGetStackSize(epicsThreadStackMedium), (EPICSTHREADFUNC) MM4000Poller, (void *) pController); return MOTOR_AXIS_OK; }
static void MM4000Poller(MM4000Controller *pController) { /* This is the task that polls the MM4000 */ double timeout; AXIS_HDL pAxis; int status; int itera, j; int axisDone; int offset; int anyMoving; int comStatus; int forcedFastPolls=0; char *p, *tokSave; char statusAllString[BUFFER_SIZE]; char positionAllString[BUFFER_SIZE]; char buff[BUFFER_SIZE]; timeout = pController->idlePollPeriod; epicsEventSignal(pController->pollEventId); /* Force on poll at startup */ while (1) { if (timeout != 0.) status = epicsEventWaitWithTimeout(pController->pollEventId, timeout); else status = epicsEventWait(pController->pollEventId); if (status == epicsEventWaitOK) { /* We got an event, rather than a timeout. This is because other software * knows that an axis should have changed state (started moving, etc.). * Force a minimum number of fast polls, because the controller status * might not have changed the first few polls */ forcedFastPolls = 10; } anyMoving = 0; /* Lock all the controller's axis. */ for (itera = 0; itera < pController->numAxes; itera++) { pAxis = &pController->pAxis[itera]; if (!pAxis->mutexId) break; epicsMutexLock(pAxis->mutexId); } comStatus = sendAndReceive(pController, "MS;", statusAllString, sizeof(statusAllString)); if (comStatus == 0) comStatus = sendAndReceive(pController, "TP;", positionAllString, sizeof(positionAllString)); for (itera=0; itera < pController->numAxes; itera++) { pAxis = &pController->pAxis[itera]; if (!pAxis->mutexId) break; if (comStatus != 0) { PRINT(pAxis->logParam, MOTOR_ERROR, "MM4000Poller: error reading status=%d\n", comStatus); motorParam->setInteger(pAxis->params, motorAxisCommError, 1); } else { PARAMS params = pAxis->params; int intval, axisStatus; motorParam->setInteger(params, motorAxisCommError, 0); /* * Parse the status string * Status string format: 1MSx,2MSy,3MSz,... where x, y and z are the status * bytes for the motors */ offset = pAxis->axis*5 + 3; /* Offset in status string */ axisStatus = pAxis->axisStatus = statusAllString[offset]; if (axisStatus & MM4000_MOVING) { axisDone = 0; anyMoving = 1; } else axisDone = 1; motorParam->setInteger(params, motorAxisDone, axisDone); motorParam->setInteger(params, motorAxisHomeSignal, (axisStatus & MM4000_HOME)); motorParam->setInteger(params, motorAxisHighHardLimit, (axisStatus & MM4000_HIGH_LIMIT)); motorParam->setInteger(params, motorAxisLowHardLimit, (axisStatus & MM4000_LOW_LIMIT)); motorParam->setInteger(params, motorAxisDirection, (axisStatus & MM4000_DIRECTION)); motorParam->setInteger(params, motorAxisPowerOn, !(axisStatus & MM4000_POWER_OFF)); /* * Parse motor position * Position string format: 1TP5.012,2TP1.123,3TP-100.567,... * Skip to substring for this motor, convert to double */ strcpy(buff, positionAllString); tokSave = NULL; p = epicsStrtok_r(buff, ",", &tokSave); for (j=0; j < pAxis->axis; j++) p = epicsStrtok_r(NULL, ",", &tokSave); pAxis->currentPosition = atof(p+3); motorParam->setDouble(params, motorAxisPosition, (pAxis->currentPosition/pAxis->stepSize)); motorParam->setDouble(params, motorAxisEncoderPosn, (pAxis->currentPosition/pAxis->stepSize)); PRINT(pAxis->logParam, IODRIVER, "MM4000Poller: axis %d axisStatus=%x, position=%f\n", pAxis->axis, pAxis->axisStatus, pAxis->currentPosition); /* We would like a way to query the actual velocity, but this is not possible. If we could we could * set the direction, and Moving flags */ /* Check for controller error. */ comStatus = sendAndReceive(pController, "TE;", buff, sizeof(statusAllString)); if (buff[2] == '@') intval = 0; else { intval = 1; PRINT(pAxis->logParam, MOTOR_ERROR, "MM4000Poller: controller error %s\n", buff); } motorParam->setInteger(params, motorAxisProblem, intval); } motorParam->callCallback(pAxis->params); } /* Next axis */ /* UnLock all the controller's axis. */ for (itera = 0; itera < pController->numAxes; itera++) { pAxis = &pController->pAxis[itera]; if (!pAxis->mutexId) break; epicsMutexUnlock(pAxis->mutexId); } if (forcedFastPolls > 0) { timeout = pController->movingPollPeriod; forcedFastPolls--; } else if (anyMoving) timeout = pController->movingPollPeriod; else timeout = pController->idlePollPeriod; } /* End while */ }
void test01(WORD testNo, char *testName, BYTE tcpNotUdp, DWORD *blockSizes, WORD blockSizesCount) { DWORD start = getTicks(); //---------- out_test_header(testNo, testName); // show test header //---------- // find out the largest block size int i; int maxBlockSize = 0; for(i=0; i<blockSizesCount; i++) { if(maxBlockSize < (int)blockSizes[i]) { // if current max block size is smaller than this block size, store it maxBlockSize = blockSizes[i]; } } //---------- // open socket int handle; if(tcpNotUdp) { handle = TCP_open(SERVER_ADDR, SERVER_PORT_START, 0, maxBlockSize); } else { handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4); } if(handle < 0) { out_result_string(0, "TCP/UDP open() failed"); return; } //---------- // if TCP (not UDP), wait for connected state if(tcpNotUdp) { // wait until connected int res; while(1) { res = TCP_wait_state(handle, TESTABLISH, 1); if(res == E_NORMAL) { break; } DWORD now = getTicks(); if((now - start) > 5*200) { out_result_string(0, "TCP_wait_state() timeout"); goto test01close; } } if(res != E_NORMAL) { out_result_error_string(0, res, "TCP_wait_state() failed"); goto test01close; } } //--------------------- int res; for(i=0; i<blockSizesCount; i++) { res = sendAndReceive(tcpNotUdp, blockSizes[i], handle, 1); if(!res) { // if single block-send-and-receive operation failed, quit and close goto test01close; } } //--------------------- out_result(1); // success! test01close: if(tcpNotUdp) { res = TCP_close(handle, 0, 0); // close } else { res = UDP_close(handle); // close } if(res != E_NORMAL) { out_result_error_string(0, res, "TCP/UDP close() failed"); } }
void S2Controller::startZStack(){ sendAndReceive(QString("-zs")); }
void S2Controller::sendCommand() { sendCommandButton->setEnabled(false); sendAndReceive(cmdLineEdit->text()); }
void S2Controller::centerGalvos(){ sendCommandButton->setEnabled(false); sendAndReceive(QString("-png center")); }
void S2Controller::startScan(){ sendCommandButton->setEnabled(false); sendAndReceive(QString("-ss")); }