Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

USB-6501 handshaking with C/C++

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

0 Kudos
Message 1 of 5
(4,845 Views)

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 

Kind regards
Tobias S.
0 Kudos
Message 2 of 5
(4,813 Views)

If you can use 2 lines, maybe emulate I2C?  There's plenty of documentation on the protocol.  

0 Kudos
Message 3 of 5
(4,800 Views)

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:

  1. Set the 8 lines output from the sender which code a number of parameters to be sent
  2. Set the output line of handshaking of the sender at High
  3. The receiver reads the High signal from the sender and read the 8 lines, to get a number of parameters to be sent.
  4. The receiver set its output line of handshaking at High. The sender reads this signal and, then, set the output line of handshaking of the sender at Low.
  5. The receiver reads the Low signal from the sender and, then, set the output line of handshaking at Low.
  6. The sender send the first 8 bit of the first number to be sent. The process essentially goes back to 1

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);


		}
	}

}

 

0 Kudos
Message 4 of 5
(4,797 Views)

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...

0 Kudos
Message 5 of 5
(4,795 Views)