01-17-2012 03:57 AM
Hello,
I am trying to send numbers from one PC to another with USB-6501. I am trying to set up a protocol for handshaking between sender PC and receiver one. However, what I have tried doesn't work. There seems to be a problem with my protocol. I am wondering if somebody could provide a C/C++ code (or pseudo code) for a handshaking algorithm.
Thank you,
Daisuke
01-25-2012 08:15 AM
Hi Daisuke,
can you discribe a little bit more of your idea or what you already tried to do.
Can you post parts of your code as reference?
Greetings!
Tobias
01-26-2012 06:36 AM
If you can use 2 lines, maybe emulate I2C? There's plenty of documentation on the protocol.
01-26-2012 07:48 AM
Hello Tobias,
Thanks for the reply. I am trying to send a set of numbers from one PC to the other using USB6501. To do so, I use 2 lines handshaking and 8 lines for coding numbers. I send a number as float. Therefore, I need to send a signal with 8 lines 4 times, in order to send one float.
My protocole goes as follows:
The code snippets are attached below.
Sender:
void EventPulse::run() { int i,nPara, ind, sig; bool onBefore=false; readyToSend=true; cout<<"EvPulse::run() before while(1), readyToSend="<<readyToSend<<", paramReceived ="<<paramReceived<<endl; while(1){ if (paramReceived && readyToSend){ nPara = paramVec.size();//# of parameters intToBinarySeq(nPara); error = DAQmxWriteDigitalLines(taskHandle_para,1,1,10.0,DAQmx_Val_GroupByChannel,parameter,NULL,NULL); qDebug()<<"EvPulse::run (sender) redyToSend="<<readyToSend<<endl; setPulse(1); readyToSend = false; i=0; while (i < numRepSend*nPara){ if(readyToSend){ ind = i/numRepSend; floatToBinarySeq(paramVec[ind], i%numRepSend); error = DAQmxWriteDigitalLines(taskHandle_para,1,1,10.0,DAQmx_Val_GroupByChannel,parameter,NULL,NULL); readyToSend = false; setPulse(1); i++; } else if(!readyToSend){ if ( checkHandShake() == 1 && !onBefore){ setPulse(0); //wait until checkHanshake()==0 while( checkHandShake() == 1 ){ msleep(50); } readyToSend = true; } else if( checkHandShake() == 0 ) onBefore = false; } //after sending all the params, make the stim pc //to get out of the loop for receiving parmas if (i == numRepSend*nPara) { //wait until checkHandshake==1 while( checkHandShake() == 0 ){ msleep(50); } setPulse(0); } } paramReceived = false; emit paramsSent(); break; } } }
Reciever
void ReceiveParameters::run(bool &ok) { int i,numPara, n; numSeq.clear(); initialize(); ok = false; flagRead=false; stringstream ss; error = DAQmxStartTask(taskHandle_in); error = DAQmxStartTask(taskHandle_out); error = DAQmxStartTask(taskHandle_para); error = DAQmxReadDigitalLines(taskHandle_in,1,10.0,DAQmx_Val_GroupByChannel,data,2,&read,&bytesPerSamp,NULL); if (data[0]==1) flagRead = true; while ( flagRead == true ){ error = DAQmxReadDigitalLines(taskHandle_in,1,10.0,DAQmx_Val_GroupByChannel,data,2,&read,&bytesPerSamp,NULL); if ( flagGotPara == false && data[0]==1){//If parameter lines are set and paramters are not acquired yet error = DAQmxReadDigitalLines(taskHandle_para,1,10.0,DAQmx_Val_GroupByChannel,para,sizePara,&read,&bytesPerSamp,NULL); numPara = binaryToInt(); cout<<"# of parameters="<<numPara<<endl; flagGotPara = true; setPulse(1); i=0; while (i < numRepSend*numPara){ error = DAQmxReadDigitalLines(taskHandle_in,1,10.0,DAQmx_Val_GroupByChannel,data,2,&read,&bytesPerSamp,NULL); if(data[0]==0 && flagGotPara==true){ flagGotPara=false; setPulse(0); cout<<"RecvPara::run(), data[0]="<<int(data[0])<<", flagGotPara="<<flagGotPara<<endl; } if(data[0]==1 && flagGotPara==false){ error = DAQmxReadDigitalLines(taskHandle_para,1,10.0,DAQmx_Val_GroupByChannel,para,sizePara,&read,&bytesPerSamp,NULL); storeBinary(i); i++; flagGotPara=true; setPulse(1); } } ok = true; stop(); } else if (data[0]==0){ flagGotPara = false; dat_out[0]=0; error = DAQmxWriteDigitalLines(taskHandle_out,1,1,10.0,DAQmx_Val_GroupByChannel,dat_out,NULL,NULL); } } }
01-26-2012 07:52 AM
Hello Tom,
Thanks for the suggestion. I can use 2 lines and aware of I2C. However, since USB-6501 does not have internal clock, timed sampling has to be done through software. I am not sure how feasible this would be...