We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi
I am using stm32f103RBT6 Microcontroller For sending timestamp in millisecond through usb to a receiver . The program works fine when when i am testing in keil uvision before sending through usb but after sending through usb in receiver software it's missing a event (sometime it does lose event and sometimes not). Can anyone please tell me where the problem can be ? the code is too long to pçst here so i can't upload the whole program. Will really appreciate any help. Thanks
Here is the Fifo stack part
typedef struct FifoStack { u16 u16EventTimeStampMSPart; //This variable have only the ms part!!! u32 u32EventTimeStamp; // This variable have only the s part!!! u8 u8EventInput; } SFIFO_STACK; // Private function prototypes ------------------------------------------------- void UsbConnect(void); void StackPush(u32 u32EventTime, u16 u16EventTimeMS, u8 u8Input); u8 StackPop(SFIFO_STACK *psFifoStack); void GetMessage(u8 *pu8Buffer); void TaskFilter(void); // ***************************************************************************** /// @brief Function that saves an event in cell /// @fn void StackPush(u32 u32EventTime, u16 u16EventTimeMS, u8 u8Input) /// @param[in] u32EventTime @brief Event time /// @param[in] u16EventTimeMS @brief Event time, ms part ASIF /// @param[in] u8Input @brief Entry number that generated the event // ***************************************************************************** void StackPush(u32 u32EventTime, u16 u16EventTimeMS, u8 u8Input) { if ((s16StackWriteIndex + 1) != s16StackReadIndex) // checks for stack space { sFifo[s16StackWriteIndex].u16EventTimeStampMSPart = u16EventTimeMS; //saves the time stamp on the stack, part MS sFifo[s16StackWriteIndex].u32EventTimeStamp = u32EventTime; // saves the time stamp on the stack sFifo[s16StackWriteIndex].u8EventInput = u8Input; //saves the digital input on the stack s16StackWriteIndex++; // incrementing the write index if (s16StackWriteIndex >= STACK_SIZE) // make circular written index s16StackWriteIndex = 0; } } // ***************************************************************************** /// @brief Function that retrieves a cell event /// @fn u8 StackPop(SFIFO_STACK *psFifoStack) /// @param[out] psFifoStack @brief Event /// @retval TRUE, se ok // ***************************************************************************** u8 StackPop(SFIFO_STACK *psFifoStack) { if (s16StackReadIndex != s16StackWriteIndex) // checks for data on the stack { psFifoStack->u16EventTimeStampMSPart = sFifo[s16StackReadIndex].u16EventTimeStampMSPart; //search timestamp cell, part MS psFifoStack->u32EventTimeStamp = sFifo[s16StackReadIndex].u32EventTimeStamp; // search timestamp cell psFifoStack->u8EventInput = sFifo[s16StackReadIndex].u8EventInput; // search the digital input cell number s16StackReadIndex++; // incrementing the read index if (s16StackReadIndex >= STACK_SIZE) // make the reading rate is circular s16StackReadIndex = 0; return TRUE; } else return FALSE; }
I posted a image here
www.electro-tech-online.com/.../
This is the code for receiver software
Communication file
#include "communication.h" #include <QStringList> #include "msgbox.h" #include "hiddevice.h"
Communication::Communication() { thread = new QThread(); mutex; rcv = new Reception((QObject*)this); rcv->moveToThread(thread); connect(this,SIGNAL(Rx_Again()),rcv,SLOT(doWork())); }
void Communication::StartRX() { if(thread->isRunning() == false) thread->start(); QMetaObject::invokeMethod(rcv, "doWork", Qt::QueuedConnection); }
int Communication::ConfigPort(QByteArray Str) { return serial.Init(0x483,0x473); }
int Communication::Recv() { int res = 0; if(mutex.tryLock()) { res = serial.Read(32); mutex.unlock(); } return res; }
void Communication::CallRxAgain() { emit Rx_Again(); }
int Communication::SaveRXdata() { return 0; }
void Communication::ComRX() { emit Rx_Finish(); }
void Communication::Send(Packet* data) { int err; // serial.ClearBufferTX(); memset(&serial.bufferTX,0,HidDevice::BUFFER_SIZE); memcpy(&serial.bufferTX[0], data->Pack()->data(), data->Pack()->size()); err = serial.Write(serial.bufferTX, 32);
}
void Communication::ClosePort() { serial.Close(); }
QByteArray Communication::getRX_Data() { QByteArray res = QByteArray((char*)serial.bufferRX,31); return res; }
void Communication::run() {
Communication::~Communication() { if(thread->isRunning()) thread->deleteLater(); }
/****************************** M'todo para recep''o dos dados ********************************/ Reception::Reception(QObject* t) { comclass = t; }
/**************************** M'todo para recep''o dos dados *****************************/ void Reception::doWork() { int recvBytes = -1; while( (recvBytes = ((Communication*)comclass)->Recv()) < 0 ) { if (recvBytes < 0) printf("RecvBytes < 0 !!!"); }
((Communication*)comclass)->ComRX(); }
Hid device File
#include "hiddevice.h"
HidDevice::HidDevice() { memset(&bufferTX[0], 0, BUFFER_SIZE); memset(&bufferRX[0], 0, BUFFER_SIZE); }
// ***************************************************************************** /// @brief Inicializa a HID /// @fn int SerialPort:: Init(QByteArray s) /// param[in] QByteArray s /// @brief Serial port address /// retval SProcess Status: Failed-1; 0-Success // ***************************************************************************** int HidDevice::Init(unsigned short vendor_id, unsigned short product_id) { int res; #define MAX_STR 255 wchar_t wstr[MAX_STR]; // Open the device using the VID, PID, // and optionally the Serial number. handle = hid_open(vendor_id, product_id, NULL); // res = hid_get_manufacturer_string(handle, wstr, MAX_STR); // qDebug("Manufacturer String: %s\n", wstr); // Read the Product String res = hid_get_product_string(handle, wstr, MAX_STR); qDebug("Product String: %ls\n", wstr); hid_set_nonblocking(handle,0);
if(handle==0) return 0; else return 1; }
void HidDevice::Close() { if (handle > 0) hid_close(handle);
int HidDevice::Read(void) { return Read(BUFFER_SIZE); }
int HidDevice::Read(int nb) { int n_bytes_rx = 0, timeout_flag = 1; // HidDevice::ClearBuffers(); memset (bufferRX, 0, nb);//zera o buffer de recepcao timeout_flag = hid_read(handle, bufferRX, nb); if (timeout_flag < 0) n_bytes_rx = TIMEOUT; return n_bytes_rx; }
int HidDevice::Write(unsigned char *buffer_tx, int n_bytes) { int counter; counter = hid_write(handle, buffer_tx, n_bytes); return counter; }