/* * Class: kinetic_Kinetic1090Puck * Method: puckRead * Signature: ()[I */ JNIEXPORT jintArray JNICALL Java_kinetic_Kinetic1090Puck_puckRead(JNIEnv *env, jobject obj) { DWORD rxBytes; DWORD txBytes; DWORD eventDWord; DWORD bytesReturned; FT_STATUS ftStatus = FT_GetStatus(ftHandle, &rxBytes, &txBytes, &eventDWord); if(ftStatus != FT_OK) { return (*env)->NewIntArray(env,0); } if(rxBytes == 0) { return (*env)->NewIntArray(env,0); } if(rxBytes > 1024) { rxBytes = 1024; } ftStatus = FT_Read(ftHandle, rxBuffer, rxBytes, &bytesReturned); jintArray outputArray = (*env)->NewIntArray(env, bytesReturned); jint *outputBody = (*env)->GetIntArrayElements(env, outputArray, 0); for(int i=0;i<bytesReturned;i++) { outputBody[i] = (int)(rxBuffer[i]); } (*env)->ReleaseIntArrayElements(env, outputArray, outputBody, 0); return outputArray; }
/* Returns number of bytes ready, or zero. */ int serial_ready (void) { DWORD rxqueue, txqueue, stat, estat; stat = FT_GetStatus (handle, &rxqueue, &txqueue, &estat); return rxqueue; }
int CKMotionIO::NumberBytesAvailToRead(int *navail, bool ShowMessage) { FT_STATUS ftStatus; DWORD EventDWord; DWORD RxBytes; DWORD TxBytes; *navail = strlen(m_SaveChars); // take into account any already read in Mutex->Lock(); ftStatus = FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord); if (ftStatus != FT_OK) { if (ShowMessage) Failed(); else m_Connected=false; Mutex->Unlock(); return 1; } else { *navail+=(int)RxBytes; Mutex->Unlock(); return 0; } }
void getSerialNumber( FT_HANDLE ftHandle ) { FT_STATUS status; char buf[80]; char c; char *p; unsigned long nBytesWritten; unsigned long eventStatus; unsigned long nRxCnt;// Number of characters in receive queue unsigned long nTxCnt;// Number of characters in transmit queue memset( buf, 0, sizeof( buf ) ); printf("Get serial number.\n"); printf("==================\n"); FT_Purge( ftHandle, FT_PURGE_RX | FT_PURGE_TX ); sprintf( buf, "N\r" ); if ( FT_OK != ( status = FT_Write( ftHandle, buf, strlen( buf ), &nBytesWritten ) ) ) { printf("Error: Failed to write command. return code = %d\n", status ); return; } // Check if there is something to receive while ( 1 ){ if ( FT_OK == FT_GetStatus( ftHandle, &nRxCnt, &nTxCnt, &eventStatus ) ) { // If there are characters to receive if ( nRxCnt ) { if ( FT_OK != ( status = FT_Read( ftHandle, buf, nRxCnt, &nBytesWritten ) ) ) { printf("Error: Failed to read data. return code = %d\n", status ); return; } p = buf; while ( *p ) { if ( 0x0d == *p ) { *p = 0; break; } p++; } printf( "Serial = %s \n", buf ); break; } } else { printf("Error: Failed to get status. return code = %d\n", status ); return; } } }
int D2xxSerial::available() { DWORD EventDWord; DWORD RxBytes; DWORD TxBytes; DWORD BytesReceived; FT_GetStatus(handle,&RxBytes,&TxBytes,&EventDWord); return RxBytes; }
/* * Class: kinetic_Kinetic1090Puck * Method: puckBytesAvailable * Signature: ()I */ JNIEXPORT jint JNICALL Java_kinetic_Kinetic1090Puck_puckBytesAvailable(JNIEnv *env, jobject obj) { DWORD rxBytes; DWORD txBytes; DWORD eventDWord; FT_STATUS ftStatus = FT_GetStatus(ftHandle, &rxBytes, &txBytes, &eventDWord); if (ftStatus != FT_OK) { return -1; } return rxBytes; }
/*Writing data to USB */ quint16 ftdiChip::Write(void *pdata, DWORD leng, DWORD *nMin) { FT_STATUS ftStatus; DWORD dwRx=0,dwTx=0,dwEvent=0; ftStatus=FT_GetStatus(hdUSB,&dwRx,&dwTx,&dwEvent); if (ftStatus!=FT_OK){ return retErr; } if ((4096-dwTx)>=(DWORD)leng){// запись, если в выходном буфере есть место ftStatus=FT_Write(hdUSB,pdata,leng,nMin); if(ftStatus!=FT_OK){ emit signalStatusError(tr("Error writing data to USB"),true); return retErr; } } ftStatus=FT_GetStatus(hdUSB,&dwRx,&dwTx,&dwEvent); if (ftStatus!=FT_OK){ return retErr; } return retOk; }
void serial_sync () { DWORD rx, tx, ev; while (1) { FT_GetStatus (handle, &rx, &tx, &ev); if (tx == 0) break; usleep (1*1000); } if (verbose > 2) printf("\033[31m(S)\033[0m"); }
/*************************************************************************** Routine: LinkStatus Inputs: boolean *pbReadable : pointer to storage for Readable status boolean *pbWriteable : pointer to storage for Writeable status Returns: void Description: Determines whether or not the OPTOTRAK system is ready to accept data and whether there is data available from the OPTOTRAK. ***************************************************************************/ NDI_DECL1 boolean NDI_DECL2 LinkStatus( boolean *pbReadable, boolean *pbWriteable ) { DWORD dwRxStat; DWORD dwTxStat; DWORD dwEvStat; *pbReadable = *pbWriteable = FALSE; if( s_hndl ) { if( FT_GetStatus( s_hndl, &dwRxStat, &dwTxStat, &dwEvStat ) == FT_OK ) { *pbReadable = (dwRxStat > 0) ? TRUE : FALSE; *pbWriteable = (dwTxStat > 0) ? FALSE : TRUE; return TRUE; } /* if */ } /* if */ return FALSE; }
bool CMmcUsbHndlBase::GetStatus(unsigned int *pAmountInRxQueue, unsigned int *pAmountInTxQueue, unsigned int *pEventStatus) { if( !AreLibraryFunctionsLoaded() ) { perror("Library not loaded"); return false; } FT_STATUS ftStatus = FT_GetStatus(m_Handle, (DWORD*)pAmountInRxQueue, (DWORD*)pAmountInTxQueue, (DWORD*)pEventStatus); if( ftStatus != FT_OK ) { std::string errormsg = GetFtStatusDescription(ftStatus); errormsg += ":FT_GetStatus"; perror(errormsg.c_str()); } return (FT_OK == ftStatus); }
int CKMotionIO::ReadBytesAvailable(char *RxBuffer, int maxbytes, DWORD *BytesReceived, int timeout_ms) { FT_STATUS ftStatus; DWORD EventDWord; DWORD RxBytes; DWORD TxBytes; Mutex->Lock(); ftStatus=FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord); if (ftStatus != FT_OK) { Failed(); Mutex->Unlock(); return 1; } if ((int)RxBytes > maxbytes) RxBytes = maxbytes-1; // leave room for null RxBuffer[0]=0; // set buf empty initially *BytesReceived=0; if (RxBytes > 0) { ftStatus = FT_Read(ftHandle,RxBuffer,RxBytes,BytesReceived); if (ftStatus == FT_OK) { RxBuffer[*BytesReceived]=0; // null terminate } else { Failed(); Mutex->Unlock(); return 1; } } Mutex->Unlock(); return 0; }
BOOL ReadByteReady (void) { if (ParPort == -1) { DWORD EventDWord = 0; DWORD RxBytes = 0; DWORD TxBytes = 0; FT_GetStatus(ftHandleA,&RxBytes,&TxBytes,&EventDWord); if (RxBytes > 0) return TRUE; else return FALSE; } else { BYTE b; b = prStatus(); // wait for ACK if (!((b ^ c) & 0x20)) return FALSE; return TRUE; } }
extern "C" BOOL ReadByteReady (void) //check if byte is waiting in queue { if (ParPort == -1) { DWORD EventDWord = 0; DWORD RxBytes = 0; DWORD TxBytes = 0; if(usb_timeout_error) return FALSE; FT_GetStatus(ftHandleA,&RxBytes,&TxBytes,&EventDWord); if (RxBytes > 0) return TRUE; else return FALSE; } else { BYTE b; b = prStatus(); // wait for ACK if (!((b ^ c) & 0x20)) return FALSE; return TRUE; } }
int FTDXXDevice::communicate (unsigned char* packet, unsigned char* buffer, int send, int receive, bool twice) throw () { if (!_initialized) return false; _bytes = _totalbytes = 0; #ifdef FOUND_ftd2xx _t = _timeout; _status = FT_Write(_handle, &packet[0], send, &_bytes); FXXCHECK("Failed to write data."); while (_totalbytes < (unsigned int)receive && _t > 0) { _bytes = 0; usleep(_latency); FT_GetStatus(_handle, &_temp_rx, &_temp_dword, &_temp_dword); if (_temp_rx != 0) { _status = FT_Read(_handle, &buffer[_totalbytes], _temp_rx, &_bytes); FXXCHECK("Failed to read data."); } _totalbytes += _bytes; _t--; } #endif return _totalbytes; }
void image_transfer(FILE *fp, ftdi_context_t *c, u8 dump, u8 type, u32 addr, u32 size) { u32 ram_addr = addr; int bytes_left = size; int bytes_done = 0; int bytes_do; int trunc_flag = 0; int i; int chunk = 0; struct timespec time_start; struct timespec time_stop; double time_duration; time_t time_diff_seconds; long time_diff_nanoseconds; dev_cmd_resp_t r; // make sure handle is valid if(!c->handle) die(err[DEV_ERR_NULL_HANDLE], __FUNCTION__); // decide a better, more optimized chunk size if(size > 16 * 1024 * 1024) chunk = 32; else if( size > 2 * 1024 * 1024) chunk = 16; else chunk = 4; // convert to megabytes chunk *= 128 * 1024; if(c->verbose) _printf(info[INFO_CHUNK], CHUNK_SIZE); if(c->verbose) _printf(info[INFO_OPT_CHUNK], chunk); // get initial time count // QueryPerformanceFrequency(&time_freq); // QueryPerformanceCounter(&time_start); clock_gettime(CLOCK_MONOTONIC, &time_start); while(1){ if(bytes_left >= chunk) bytes_do = chunk; else bytes_do = bytes_left; if(bytes_do % 512 != 0) { trunc_flag = 1; bytes_do -= (bytes_do % 512); } if(bytes_do <= 0) break; for(i = 0; i < 2; i++){ if(i == 1) { printf("\n"); _printf("Retrying\n"); FT_ResetPort(c->handle); FT_ResetDevice(c->handle); _printf("Retrying FT_ResetDevice() success\n"); // set synchronous FIFO mode //FT_SetBitMode(c->handle, 0xff, 0x40); _printf("Retrying FT_SetBitMode() success\n"); FT_Purge(c->handle, FT_PURGE_RX | FT_PURGE_TX); _printf("Retrying FT_Purge() success\n"); } device_sendcmd(c, &r, dump ? DEV_CMD_DUMPRAM : DEV_CMD_LOADRAM, 2, 0, 1, ram_addr, (bytes_do & 0xffffff) | type << 24); if(dump){ c->status = FT_Read(c->handle, buffer, bytes_do, &c->bytes_written); fwrite(buffer, bytes_do, 1, fp); }else{ fread(buffer, bytes_do, 1, fp); c->status = FT_Write(c->handle, buffer, bytes_do, &c->bytes_written); } if(c->bytes_written) break; } // check for a timeout if(c->bytes_written == 0) die(err[DEV_ERR_TIMED_OUT], __FUNCTION__); // dump success response c->status = FT_Read(c->handle, buffer, 4, &c->bytes_read); bytes_left -= bytes_do; bytes_done += bytes_do; ram_addr += bytes_do; // progress bar prog_draw(bytes_done, size); c->status = FT_GetStatus(c->handle, &c->bytes_read, &c->bytes_written, &c->event_status); } // stop the timer // QueryPerformanceCounter(&time_stop); clock_gettime(CLOCK_MONOTONIC, &time_stop); // get the difference of the timer time_diff_seconds = time_stop.tv_sec - time_start.tv_sec; time_diff_nanoseconds = time_stop.tv_nsec - time_start.tv_nsec; time_duration = (double)time_diff_seconds + (double)(time_diff_nanoseconds/1000000000.0f); // erase progress bar prog_erase(); if(c->verbose && trunc_flag) _printf(info[INFO_TRUNCATED]); if(c->verbose) _printf(info[INFO_COMPLETED_TIME], time_duration, (float)size/1048576.0f/(float)time_duration); }
BOOL readFrame( FT_HANDLE ftHandle ) { CANMsg msg; int i,j; char buf[80]; unsigned long nRxCnt; unsigned long nTxCnt; unsigned long eventStatus; unsigned long nRcvCnt; char c; static char msgReceiveBuf[80]; static int cntMsgRcv = 0; static int state = CANUSB_STATE_NONE; // Check if there is something to receive if ( FT_OK == FT_GetStatus( ftHandle, &nRxCnt, &nTxCnt, &eventStatus ) ) { // If there are characters to receive if ( nRxCnt ) { // Must fit to buffer if ( nRxCnt > sizeof( gbufferRx ) ) { nRxCnt = sizeof( gbufferRx ); } // Read data if ( ( FT_OK == FT_Read( ftHandle, gbufferRx, nRxCnt, &nRcvCnt ) ) && ( nRcvCnt > 0 ) ) { for ( i=0; i<nRcvCnt; i++ ) { // Get character c = gbufferRx[ i ]; if ( CANUSB_STATE_NONE == state ) { if ( ('t' == c ) || ( 'T' == c ) || ('r' == c ) || ( 'R' == c ) ) { state = CANUSB_STATE_MSG; memset( msgReceiveBuf, 0, sizeof( msgReceiveBuf ) ); msgReceiveBuf[ 0 ] = c; cntMsgRcv = 1; } } else if ( CANUSB_STATE_MSG == state ) { msgReceiveBuf[ cntMsgRcv++ ] = c; if ( 0x0d == c ) { printf("Raw Msg = %s\n", msgReceiveBuf ); if ( !canusbToCanMsg( msgReceiveBuf, &msg ) ) { printf("Message conversion failed!\n"); state = CANUSB_STATE_NONE; return FALSE; } if ( msg.flags & CANMSG_EXTENDED ) { printf("Extended "); } else { printf("Standard "); } printf("message received: id=%X len=%d timestamp=%X ", msg.id, msg.len, msg.timestamp ); if ( msg.len ) { printf("data="); for ( j=0; j<msg.len; j++ ) { printf("%02X ", msg.data[j]); } } printf("\n"); gnReceivedFrames++; state = CANUSB_STATE_NONE; } // full message } // STATE_MSG } // for each char } // Read data } // characters to receive } // getstatus return TRUE; }
int CKMotionIO::FlushInputBuffer() { FT_STATUS ftStatus; DWORD RxBytes; DWORD TxBytes; DWORD BytesReceived; DWORD BytesWritten; DWORD EventDWord; char s[10]; char RxBuffer[500]; ftStatus = FT_Purge(ftHandle,FT_PURGE_RX|FT_PURGE_TX); if (ftStatus != FT_OK) return 1; // discard any data in the read queue in the driver DWORD t0=timeGetTime(); do { ftStatus=FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord); if (ftStatus != FT_OK) return 1; if (RxBytes > 0) { if (RxBytes > 400) RxBytes=400; ftStatus = FT_Read(ftHandle,RxBuffer,RxBytes,&BytesReceived); } } while (RxBytes > 0 && timeGetTime()-t0 < CONNECT_TIMEOUT); if (SendAbortOnConnect) { // send flush command to DSP s[0]=ABORT_CHAR; s[1]=0; ftStatus = FT_Write(ftHandle, s, 1, &BytesWritten); if (ftStatus != FT_OK) return 1; if (BytesWritten != 1) return 1; // wait and be sure chars are transmitted t0=timeGetTime(); do { ftStatus=FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord); if (ftStatus != FT_OK) return 1; } while (TxBytes != 0 && timeGetTime()-t0 < CONNECT_TIMEOUT); if (TxBytes != 0) return 1; // wait for a fixed time for the abort acknowledge // to come back which is exactly 3 characters ESC C \r t0=timeGetTime(); do { ftStatus=FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord); if (ftStatus != FT_OK) return 1; } while (RxBytes < 3 && timeGetTime()-t0 < CONNECT_TIMEOUT); if (RxBytes == 0) { // KMotion seems to be present but not responding // after two attemps flag as non responsive and // stop trying NonRespondingCount++; if (NonRespondingCount == 2) { ErrorMessageBox("KMotion present but not responding\r\r" "Correct problem and restart application"); } return 1; } if (RxBytes != 3) return 1; NonRespondingCount=0; // read the 3 bytes ftStatus = FT_Read(ftHandle,RxBuffer,RxBytes,&BytesReceived); if (ftStatus != FT_OK) return 1; if (BytesReceived != 3) return 1; if (RxBuffer[0] != '\x1b') return 1; if (RxBuffer[1] != 'C') return 1; if (RxBuffer[2] != '\r') return 1; } // verify there are no transmit or receive characters ftStatus=FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord); if (ftStatus != FT_OK) return 1; if (RxBytes != 0) return 1; if (TxBytes != 0) return 1; // OK looks like we are in sync return 0; }
bool CApoxObj::readUSBData( void ) { bool bData = false; DWORD eventStatus; DWORD nRxCnt; // Number of characters in receive queue DWORD nTxCnt; // Number of characters in transmit queue DWORD nRcvCnt; if ( FT_OK == FT_GetStatus( m_ftHandle, &nRxCnt, &nTxCnt, &eventStatus ) ) { // If there are characters to receive if ( nRxCnt ) { if ( nRxCnt > sizeof( m_bufferRx ) ) { nRxCnt = sizeof( m_bufferRx ); } if ( ( FT_OK == FT_Read( m_ftHandle, m_bufferRx, nRxCnt, &nRcvCnt ) ) && ( nRcvCnt > 0 ) ) { for ( uint32_t i=0; i<nRcvCnt; i++ ) { if ( USB_OK == processUSBByte( m_bufferRx[ i ], &m_lengthMsgRcv, m_bufferMsgRcv ) ) { if ( USB_MESSAGE_COMPLETE == m_RxMsgState ) { // A message if ( 0x00 == m_bufferMsgRcv[ 0 ] ) { // Response to control message from adapter // ---------------------------- // [0] 0x00 // [1] command | 0x80 // [2..n] response data if ( m_responseList.nCount < APOX_MAX_RESPONSEMSG ) { responseMsg *pMsg = new responseMsg; if ( NULL != pMsg ) { dllnode *pNode = new dllnode; if ( NULL != pNode ) { pMsg->id = 0x00; pMsg->command = m_bufferMsgRcv[ 1 ] & 0x7F; pMsg->len = (uint8_t)(m_lengthMsgRcv - 2); //(minus two for the first two bytes) memcpy( pMsg->data, ( m_bufferMsgRcv + 2 ), pMsg->len ); pNode->pObject = pMsg; LOCK_MUTEX( m_responseMutex ); dll_addNode( &m_responseList, pNode ); UNLOCK_MUTEX( m_responseMutex ); } else { delete pMsg; } } } } else if ( 0xff == m_bufferMsgRcv[ 0 ] ) { // Unsolicited emergency message from adapter // ---------------------------- // [0] 0xFF // [1] 0x80 // [2] ERRORCODE (0-255) switch( m_bufferMsgRcv[ 2 ] ) { case CAN_RCV_OVERFLOW: m_stat.cntOverruns++; m_emergencyInfo |= EMERGENCY_OVERFLOW; break; case CAN_RCV_WARNING: m_emergencyInfo |= EMERGENCY_RCV_WARNING; break; case CAN_TX_WARNING: m_emergencyInfo |= EMERGENCY_TX_WARNING; break; case CAN_RX_BUS_PASSIVE: m_emergencyInfo |= EMERGENCY_TXBUS_PASSIVE; break; case CAN_TX_BUS_PASSIVE: m_emergencyInfo |= EMERGENCY_RXBUS_PASSIVE; break; case CAN_BUS_OFF: m_emergencyInfo |= EMERGENCY_BUS_OFF; break; } } else { // CAN message // ---------------------------- // 0:[who|rtr|idMode|(unused)] // 1:[ID MSB] // 2:[ID3] // 3:[ID2] // 4:[ID LSB] // 5:[TIMESTAMP MSB] // 6:[TIMESTAMP LSB] // 7:[RX_FLAGS] // 8:[LENGTH] // 9-16:[DATA 0-8 bytes] if ( m_receiveList.nCount < APOX_MAX_RCVMSG ) { PCANALMSG pMsg = new canalMsg; pMsg->flags = 0; if ( NULL != pMsg ) { dllnode *pNode = new dllnode; if ( NULL != pNode ) { pMsg->timestamp = (((DWORD)m_bufferMsgRcv[8]<<24 ) & 0xff000000) | (((DWORD)m_bufferMsgRcv[7]<<16 ) & 0x00ff0000) | (((DWORD)m_bufferMsgRcv[6]<<8 ) & 0x0000ff00) | (((DWORD)m_bufferMsgRcv[5] ) & 0x000000ff) ; pMsg->id = (((DWORD)m_bufferMsgRcv[4]<<24) & 0x1f000000) | (((DWORD)m_bufferMsgRcv[3]<<16) & 0x00ff0000) | (((DWORD)m_bufferMsgRcv[2]<<8 ) & 0x0000ff00) | (((DWORD)m_bufferMsgRcv[1] ) & 0x000000ff) ; pMsg->sizeData = m_bufferMsgRcv[10]; memcpy( (void *)pMsg->data, (m_bufferMsgRcv + 11 ), pMsg->sizeData ); // If extended set extended flag if ( m_bufferMsgRcv[0] & 0x20 ) pMsg->flags |= CANAL_IDFLAG_EXTENDED; // Check for RTR package if ( (m_bufferMsgRcv[0] & 0x40) ) pMsg->flags |= CANAL_IDFLAG_RTR; pNode->pObject = pMsg; LOCK_MUTEX( m_receiveMutex ); dll_addNode( &m_receiveList, pNode ); UNLOCK_MUTEX( m_receiveMutex ); // Update statistics m_stat.cntReceiveData += pMsg->sizeData; m_stat.cntReceiveFrames += 1; } else { delete pMsg; } } } else { // Full buffer m_stat.cntOverruns++; } } // recv types m_RxMsgState = USB_IDLE; // reset state for next msg } // Complete msg } } // byte loop } // read OK } // Bytes to read } return bData; }