c - Why does data i push into a queue not come out on the other end? -


i writing multithreaded communication interface 1 function (in main thread) pushes data queue (vxworks msgqlib) , function in communication task, fetches data queue forward hardware interface. now, seems push data doesn't align fetch data (or vice versa). inserted debugging messages find out what's going on have failed find culprit far. code have:

a little explanation. built functions so, insert message tag meta data before actual message i.e. each message insert 2 items queue. please don't worry "reply" part yet, still needs work on either side. i'm worried outgoing part of equation.

the function pushes data queue (in application task):

static status spiqwriteread_tdm(uint8 cmdlen, uint8 *cmd, uint8 retdatlen, uint8 *retdat) {     uint8 bytesread;     uint8 *msgtag[msgtaglen]={0};     static uint spi_init = 0;     uint i=0;      //assemble message tag     msgtag[0] = 0x01; //frameid     msgtag[1] = cmdlen/255; //msglen msb     msgtag[2] = cmdlen%255; //msglen lsb     msgtag[3] = cs_tdm; // chipselect     msgtag[4] = 0; //slotid       //copy message tag queue     for(i=0;i<msgtaglen;i++)             printf("<= msgtag[%d] 0x%x\n",i,msgtag[i]);     msgqsend(tdmtxq,msgtag,msgtaglen,no_wait,msg_pri_normal);     printf("<= txqueue(0x%x) contains %d items\n",tdmtxq, msgqnummsgs(tdmtxq));      for(i=0;i<cmdlen;i++)         printf("<= msg[%d] 0x%x\n",i,cmd[i]);     // copy message queue     msgqsend(tdmtxq,cmd,cmdlen,no_wait,msg_pri_normal);     printf("<= txqueue(0x%x) contains %d items\n",tdmtxq, msgqnummsgs(tdmtxq));      // wait maximum of 10 ticks (~1600ms) reply     bytesread = msgqreceive(tdmrxq,retdat,retdatlen,100);     for(i=0;i<retdatlen;i++)             printf("retdat[%d] 0x%x\n",i,retdat[i]);     printf("bytesread 0x%x\n",bytesread);     // compare reply , return     if (bytesread != retdatlen)         return error;     else         return ok; } 

and function fetches data queue (in communication task):

static from_q_dat *getfromqueue(msg_q_id txqid) {     uint8 msgtag[msgtaglen]= {0};     uint8 frameid = 0;     uint16 msglen = 0;     uint8 chipselect = 99;     uint8 slotid = 99;     static uint8 *cmd=null;     uint8 retdat[2]={0};     uint8 cs=99;     static from_q_dat qdat;     int retval = 0;     uint8 i=0;      // read message tag queue     msgqreceive(txqid, msgtag, msgtaglen, no_wait);     for(i=0;i<msgtaglen;i++)             printf("=> msgtag[%d] 0x%x\n",i,msgtag[i]);      // parse received message tag      frameid = msgtag[0];     msglen = (((uint16)msgtag[1])<<8) | (msgtag[2]);     chipselect = msgtag[3];     slotid = msgtag[4];     printf("frameid: %d\n",frameid);     printf("msglen: %d\n",msglen);     printf("chipselect: %d\n",chipselect);     printf("slotid: %d\n",slotid);      printf("=> txqueue(0x%x) contains %d items\n",txqid, msgqnummsgs(txqid));      cmd = (uint8*) calloc(msglen, sizeof(uint8));     // message queue      msgqreceive(txqid, cmd, msglen, no_wait);     for(i=0;i<msglen;i++)                 printf("=> cmd[%d] 0x%x\n",i,cmd[i]);      printf("=> txqueue(0x%x) contains %d items\n",txqid, msgqnummsgs(txqid));      qdat.cs = chipselect;     qdat.len = msglen;     qdat.data = cmd;     return &qdat; } 

and screen capture looks this:

<= msgtag[0] 0x1 <= msgtag[1] 0x0 <= msgtag[2] 0x9 <= msgtag[3] 0x2 <= msgtag[4] 0x0 <= txqueue(0x261c6010) contains 1 items <= msg[0] 0x5e <= msg[1] 0x8 <= msg[2] 0x40 <= msg[3] 0x6 <= msg[4] 0x0 <= msg[5] 0x0 <= msg[6] 0x0 <= msg[7] 0x0 <= msg[8] 0x0 <= txqueue(0x261c6010) contains 2 items //the next message (with '==' communication engine checks content in communication queue: == txqueue(1) contains 2 items => msgtag[0] 0x0 => msgtag[1] 0x0 => msgtag[2] 0x0 => msgtag[3] 0x1 => msgtag[4] 0x0 frameid: 0 msglen: 0 chipselect: 1 slotid: 0 => txqueue(0x261c6010) contains 1 items => txqueue(0x261c6010) contains 0 items 

i don't see why data goes queue wouldn't come out...

please check return values of function call, @ least don't miss errors. e.g. receiving messages no_wait , if happen call msgqreceive before has been sent, won't receive - important thing handle. similar when send no_wait - don't handle case if queue full.

however, sending function have:

 uint8 *msgtag[msgtaglen] 

which reason array of uint8 pointers. there's nothing in code suggests should array of pointers - , since array differs in receiver function, you'll interpret bytes quite differently in 2 functions. receiver function seems have correct, make sure sender function has same:

 uint8 msgtag[msgtaglen]; 

(also make sure 2 functions here arn't called several tasks, has static variables , not thread safe, , not re-entrant.)


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -