void* ReadData(void* ptr) { DeviceParams_t* device = (DeviceParams_t*)ptr; DWORD RxBytes; FT_STATUS status; printf("Thread Started\r\n"); while(true) { WaitForData(device); status = FT_GetQueueStatus(device->handle, &RxBytes); if(status != FT_OK) { fprintf(stderr, "Can't read from ftdi device: %d\n", status); return NULL; } //printf("RX %s [%d]\r\n", device->serial, RxBytes); if(RxBytes > 0) { DWORD BytesReceived; char* data = new char[RxBytes]; FT_Read(device->handle, data, RxBytes, &BytesReceived); fprintf(stderr, "%d Bytes Read\n", BytesReceived); delete[] data; } } return NULL; }
int64_t CSimpleFileCache::Seek(int64_t iFilePosition) { CLog::Log(LOGDEBUG,"CSimpleFileCache::Seek, seeking to %"PRId64, iFilePosition); int64_t iTarget = iFilePosition - m_nStartPosition; if (iTarget < 0) { CLog::Log(LOGDEBUG,"CSimpleFileCache::Seek, request seek before start of cache."); return CACHE_RC_ERROR; } int64_t nDiff = iTarget - m_nWritePosition; if ( nDiff > 500000 || (nDiff > 0 && WaitForData((unsigned int)nDiff, 5000) == CACHE_RC_TIMEOUT) ) { CLog::Log(LOGWARNING,"%s - attempt to seek past read data (seek to %"PRId64". max: %"PRId64". reset read pointer. (%"PRId64")", __FUNCTION__, iTarget, m_nWritePosition, iFilePosition); return CACHE_RC_ERROR; } LARGE_INTEGER pos; pos.QuadPart = iTarget; if(!SetFilePointerEx(m_hCacheFileRead, pos, NULL, FILE_BEGIN)) return CACHE_RC_ERROR; m_nReadPosition = iTarget; m_space.Set(); return iFilePosition; }
bool MsgHandlerJSON::ReceiveString(std::string& str) { static char cmd[1024]; int bytes_read = 0; bool done = false; // wait for command if(!WaitForData()) { return false; } bzero(cmd, sizeof(cmd)); while(!done) { int rc = recv(m_sock, cmd + bytes_read, sizeof(cmd) - bytes_read, MSG_DONTWAIT); if(rc <= 0) { m_closed = (rc == 0); return false; } bytes_read += rc; if(cmd[bytes_read - 1] == '\x0a' || cmd[bytes_read - 1] == '\x0d') { done = true; } } if(done) { str = cmd; } return done; }
NS_IMETHODIMP nsSyncStreamListener::Available(PRUint32 *result) { if (NS_FAILED(mStatus)) return mStatus; mStatus = mPipeIn->Available(result); if (NS_SUCCEEDED(mStatus) && (*result == 0) && !mDone) { mStatus = WaitForData(); if (NS_SUCCEEDED(mStatus)) mStatus = mPipeIn->Available(result); } return mStatus; }
uint32_t AJS_TargetIO_PinGet(void* pinCtx) { GPIO* gpio = (GPIO*)pinCtx; if (SendCmd('r', gpio, 0, 0)) { if (WaitForData(200) == AJ_OK) { if ((recvBuf[0] == 'r') && (recvBuf[1] == (int)gpio->pinId)) { return recvBuf[2]; } } } else { AJ_ErrPrintf(("AJS_TargetIO_PinGet(%d)\n", gpio->pinId)); } return 0; }
bool CCustomerDisplay::TransferCommand(LPBYTE lpCmd, BYTE nCmdLen) { BYTE bPreparedCmd[56], bAnswer; DWORD dwNumberOfBytesWritten, dwNumberOfBytesRead; ZeroMemory(bPreparedCmd, 56); bPreparedCmd[0] = EOT; bPreparedCmd[1] = SOH; CopyMemory(bPreparedCmd + 2, lpCmd, nCmdLen); bPreparedCmd[2 + nCmdLen] = ETB; WriteFile(hComm, bPreparedCmd, 2 + nCmdLen + 1, &dwNumberOfBytesWritten, NULL); if (WaitForData(hComm, 1, 1000)) { ReadFile(hComm, &bAnswer, 1, &dwNumberOfBytesRead, NULL); return (bAnswer == ACK); } else return false; }
void MainManager::Initialize() { qRegisterMetaType<AnalysisRecord>(); M4D::Imaging::APipeFilter *filter = new M4D::Imaging::ImageConvertor< InputImageType >(); _conversionPipeline.AddFilter( filter ); _inConnection = static_cast< InImageConnection * >(&(_conversionPipeline.MakeInputConnection( *filter, 0, false ))); _inConvConnection = static_cast< ImageConnectionType * >(&(_conversionPipeline.MakeOutputConnection( *filter, 0, true ) )); CreateResultProcessPipeline(); _resultsPage = new ResultsPage( *this ); QObject::connect( this, SIGNAL(ResultProcessingStarted()), _resultsPage, SLOT( WaitForData() ), Qt::QueuedConnection ); QObject::connect( this, SIGNAL(ShowResultsSignal( AnalysisRecord )), _resultsPage, SLOT( ShowResults( AnalysisRecord ) ), Qt::QueuedConnection ); //TODO }
void WTcp::WaitForConnection() { while (1) { // // Block until a connection is established, or the socket is closed. // int AddrSize = sizeof(SOCKADDR); m_ConnectSock = accept(m_ListenSock, (PSOCKADDR)&m_RemoteAddr, &AddrSize); if (m_ConnectSock == INVALID_SOCKET) { if (WSAGetLastError() == WSAEINTR) // socket closed break; if (WSAGetLastError() == WSAESHUTDOWN) // socket closed break; Abort(TCP_CANT_ACCEPT); // socket error } // // Let the user know that a connection was established. // m_AmConnected = TRUE; if (m_ConnectionHandler != NULL) { (*m_ConnectionHandler)(m_AmConnected, inet_ntoa(m_RemoteAddr.sin_addr), m_ConnectionHandlerArg); } // // When WaitForData exits, we're disconnected, so close the socket. // WaitForData(); closesocket(m_ConnectSock); // // Let the user know that the connection was dropped. // m_AmConnected = FALSE; if (m_ConnectionHandler != NULL) { (*m_ConnectionHandler)(m_AmConnected, inet_ntoa(m_RemoteAddr.sin_addr), m_ConnectionHandlerArg); } } }
int ReadString(int MySocket,char *buffer) { int actual; // Wait for data to become available if(WaitForData(MySocket)==0) { // Timeout printf("Warning: Timeout...\n"); return 0; } // Read data if((actual=recv(MySocket,buffer,200,0))==-1) { printf("Error: Unable to receice data (%i)...\n",errno); perror("sockets"); // Print error message based on errno exit(1); } else { buffer[actual]=0; } return actual; }
int64_t CSimpleFileCache::Seek(int64_t iFilePosition, int iWhence) { CLog::Log(LOGDEBUG,"CSimpleFileCache::Seek, seeking to %"PRId64, iFilePosition); int64_t iTarget = iFilePosition - m_nStartPosition; if (SEEK_END == iWhence) { CLog::Log(LOGERROR,"%s, cant seek relative to end", __FUNCTION__); return CACHE_RC_ERROR; } else if (SEEK_CUR == iWhence) iTarget = iFilePosition + m_nReadPosition; if (iTarget < 0) { CLog::Log(LOGDEBUG,"CSimpleFileCache::Seek, request seek before start of cache."); return CACHE_RC_ERROR; } int64_t nDiff = iTarget - m_nWritePosition; if ( nDiff > (3 * 1024 * 1024) || (nDiff > 0 && WaitForData((unsigned int)nDiff, 3000) == CACHE_RC_TIMEOUT) ) { CLog::Log(LOGWARNING,"%s - attempt to seek pass read data (seek to %"PRId64". max: %"PRId64". reset read pointer. (%"PRId64":%d)", __FUNCTION__, iTarget, m_nWritePosition, iFilePosition, iWhence); return CACHE_RC_ERROR; } LARGE_INTEGER pos; pos.QuadPart = iTarget; if(!SetFilePointerEx(m_hCacheFileRead, pos, &pos, FILE_BEGIN)) return CACHE_RC_ERROR; m_nReadPosition = iTarget; return pos.QuadPart; }
int main(int argc,char **argv) { int MySocket,MyControlPort; char SocketsBuffer[SOCKETS_BUFFER_SIZE]; struct sockaddr_in MyAddress,MyControlAddress; unsigned int ControlPort; int status; int retval; // Create socket (allocate resources) if((MySocket=socket( PF_INET, // IPv4 SOCK_STREAM, // TCP 0))==-1) { printf("Error: Unable to create socket (%i)...\n",errno); perror("sockets"); // Print error message based on errno exit(1); } // Establish TCP connection memset(&MyAddress,0,sizeof(struct sockaddr_in)); // Set structure to zero MyAddress.sin_family=PF_INET; // IPv4 MyAddress.sin_port=htons(SOCKETS_PORT); // Port number (in network order) MyAddress.sin_addr.s_addr= inet_addr(SOCKETS_IP_ADDRESS); // IP address (in network order) if(connect(MySocket,(struct sockaddr *)&MyAddress, sizeof(struct sockaddr_in))==-1) { printf("Error: Unable to establish connection to socket (%i)...\n", errno); perror("sockets"); // Print error message based on errno exit(1); } // Minimize latency by setting TCP_NODELAY option SetNODELAY(MySocket); // Clear status and reset instrument WriteString(MySocket,"*CLS;*RST\n"); // Get instrument's ID string WriteString(MySocket,"*IDN?\n"); ReadString(MySocket,SocketsBuffer); printf("Instrument ID: %s\n",SocketsBuffer); // Ask for control port WriteString(MySocket,"SYST:COMM:TCPIP:CONTROL?\n"); if(ReadString(MySocket,SocketsBuffer)==0) { printf("Warning: No response from instrument (control port).\n"); retval=0; goto SocketMainClose; } sscanf(SocketsBuffer,"%u",&ControlPort); printf("Control Port: %u\n",ControlPort); // Create socket for control port if((MyControlPort=socket(PF_INET,SOCK_STREAM,0))==-1) { printf("Error: Unable to create control port socket (%i)...\n",errno); perror("sockets"); // Print error message based on errno retval=1; goto SocketMainClose; } // Establish TCP connection to control port memset(&MyControlAddress,0, sizeof(struct sockaddr_in)); // Set structure to zero MyControlAddress.sin_family=PF_INET; // IPv4 MyControlAddress.sin_port= htons(ControlPort); // Port number (in network order) MyControlAddress.sin_addr.s_addr= inet_addr(SOCKETS_IP_ADDRESS); // IP address (in network order) if(connect(MyControlPort,(struct sockaddr *)&MyControlAddress, sizeof(struct sockaddr_in))==-1) { printf( "Error: Unable to establish connection to control port (%i)...\n", errno); perror("sockets"); // Print error message based on errno retval=1; goto SocketMainClose; } // Do a device clear DeviceClear(MyControlPort,SocketsBuffer); // Demonstrate SRQ... First, tell the instrument when to generate an SRQ // NOTE: The following commands are dependant of the instrument // This example is for the Agilent L4410A, L4411A,34410A or 34411A WriteString(MySocket, "*ESE 1\n"); // Operation Complete causes Standard Event WriteString(MySocket, "*SRE 32\n"); // Standard Event causes SRQ // Now perform some operation(s) and raise SRQ when done WriteString(MySocket,"CONF:FREQ\n"); // Go to frequency measurement mode WriteString(MySocket,"*OPC\n"); // Tell us when you're ready! // Now wait until data becomes available on the control link if(WaitForData(MyControlPort)==0) { printf("Warning: Instrument did not generate SRQ.\n"); retval=0; goto SocketClose; } // Data available... Read and interpret! ReadString(MyControlPort,SocketsBuffer); printf("Data read on control port: %s\n",SocketsBuffer); if(strncmp(SocketsBuffer,"SRQ",3)!=0) { printf("Warning: Didn't receive an SRQ!\n"); retval=0; goto SocketClose; } sscanf(SocketsBuffer,"SRQ%i",&status); printf("Status is: %d",status); SocketClose: // Close control port if(close(MyControlPort)==-1) { printf("Error: Unable to close control port socket (%i)...\n",errno); perror("sockets"); // Print error message based on errno retval=1; } SocketMainClose: // Close main port if(close(MySocket)==-1) { printf("Error: Unable to close socket (%i)...\n",errno); perror("sockets"); // Print error message based on errno retval=1;; } exit(retval); }