//SetStepMode-------------------------------
//Helper function for SetZStepperModeStruct
//Input: 0 (half-step), 1 (full step), or 2 (wave step)
//Ouput: 1 if successful, 0 if not
int zStepperDriver::SetStepMode(int stepperMode)
{

	char out[5];
	switch(stepperMode)
	{
		case 0:
			sprintf(out, "SH\r");
			break;
		case 1:
			sprintf(out, "SF\r");
			break;
		case 2:
			sprintf(out, "SW\r");
			break;
		default:
			//MessagePopup("Step Mode Error", "Invalid step mode specified.");
			return 0;		// Falls through to here if invalid input
	}

	ComWrt(port, out, 3);

	stepMode = stepperMode;

	return 1;

}
//ReadPos-------------------------------
//Report Position, save to member variables
//Input: none
//Ouput: motor position relative to origin in um
double zStepperDriver::ReadPos(double convFactor)
{

	char out [5];
	char in [15];
	int termCharVal = 13;
	int numBytesRead=0;
	double pos;

	//Output command to RS232

	sprintf(out, "RC\r");
	ComWrt(port, out, strlen(out));
	Sleep(.05*1000);		//*1000 because original value was for Delay function in CVI
	//ComRd (port, in, 11);
	numBytesRead = ComRdTerm (port, in, 11, termCharVal);
	if (numBytesRead > 0)
		in[numBytesRead]='\0';  //Add null terminator because sometimes there is random stuff in char array.
	pos = atoi(in);
	
	//if (getStepMode() ==0)
	//	pos = pos*2;

	pos = pos * convFactor;
	

	currentZPos = pos;
	return pos;

}
//SetTorque-------------------------------
//helper function for toggleZTorqueStruct
//Input: mode = 0 (off) or 1 (on)
//Ouput: 1 if successful, 0 if not
int zStepperDriver::SetTorque(int mode)
{

	char out[5];
	switch(mode)
	{
		case 0:
			sprintf(out, "SO\r");
			break;
		case 1:
			sprintf(out, "SP\r");
			break;
		default:
			//MessagePopup("Holding Torque Error", "Invalid torque mode specified.");
			return 0;
	}

	ComWrt(port, out, 3);
	return 1;
}
//Offset-------------------------------
//Move by value specified by stepSize field.
//Input: zStep = distance, in um, to move (relative to current position)
//Ouput: none
void zStepperDriver::Offset(double zStep, double convFactor)
{
	char Str[15];
	int numSteps;
	
	//Calculate wait time
	double waitTime = .015 * zStep;
	
	if(waitTime < 0)
		waitTime = -waitTime;
	
	if(zStep > 0)
		numSteps = (int) (zStep / convFactor + 0.5);
	else
		numSteps = (int) (zStep / convFactor - 0.5);
	
	//Output command to RS232
	sprintf(Str, "II%i\r", numSteps);
	ComWrt(port, Str, strlen(Str));
	Sleep(waitTime*1000);		//*1000 because original value was for Delay function in CVI 

	ReadPos(currentStepConvFactor);
}
//OpenPort-------------------------------
//Initialize COM port, make stepper motor's current position home (0)
//Input: None
//Ouput: RS232 error code
int zStepperDriver::OpenPort()
{
	int RS232Error;
	char out[5];
	char ptName[4];

	//Initialize comm port
	CloseCom(prevPort);
	
	sprintf(ptName,"COM%i ",port);
	
	RS232Error = OpenComConfig (port, ptName, 9600, 0, 8, 1, 512, 512);
	SetXMode (port, 0);
	SetCTSMode (port, 1); 
	SetComTime (port, 10);
	
	sprintf(out, "HM0\r");
	ComWrt(port, out, strlen(out));

	//set previous port as newly opened port
	prevPort = port;
	
	return RS232Error;
}
Exemple #6
0
Fichier : rkc.c Projet : wft999/25m
int RKC_CommData ( int _ACT, int _ID, double *_Val)
{
	int rLop = 0;
	int RTNval = 0;
	int BuffLEN = 0;
	char Buff[128]={0};
	char BBC = 0x00;
	char dTRN[64] = {0};
	static int Rs232Err = 0, NullPort = 0;
	
	switch (_ACT)
	{
		case Config:
			CloseCom(rkc.COM);
			if (Rs232Err = ReturnRS232Err (), Rs232Err < 0)
				return  Rs232Err;
			
			OpenComConfig(rkc.COM, "", rkc.Baud, rkc.Pari, rkc.Dbit, rkc.Sbit, 512, 512);
			if (Rs232Err = ReturnRS232Err (), Rs232Err < 0)
			{
				NullPort = 1;
				return  Rs232Err;
			}
			
			SetCTSMode(rkc.COM, rkc.Flc);
			if (Rs232Err = ReturnRS232Err (), Rs232Err < 0)
				return  Rs232Err;

			SetComTime(rkc.COM, rkc.Tout);
			if (Rs232Err = ReturnRS232Err (), Rs232Err < 0)
				return  Rs232Err;
			
			NullPort = 0;
		break;
			
		case getTP:
			
			Fmt(Buff, "%c%i[w2p0]%s[w2]%c", _EOT, (_ID & 0xFF), "M1", _ENQ);
			// MessagePopup("getTP",Buff);  //DJ_Added_0502KB_12_57
			
			break;
											   
		case getSP:
			Fmt(Buff, "%c%i[w2p0]%s[w2]%c", _EOT, (_ID & 0xFF), "S1", _ENQ);
		break;

		case getAL1:
			Fmt(Buff, "%c%i[w2p0]%s[w2]%c", _EOT, (_ID & 0xFF), "A1", _ENQ);
		break;

		case getAL2:
			Fmt(Buff, "%c%i[w2p0]%s[w2]%c", _EOT, (_ID & 0xFF), "A2", _ENQ);
		break;

		case getAT:
			Fmt(Buff, "%c%i[w2p0]%s[w2]%c", _EOT, (_ID & 0xFF), "G1", _ENQ);
		break;

		case getST:
			Fmt(Buff, "%c%i[w2p0]%s[w2]%c", _EOT, (_ID & 0xFF), "G2", _ENQ);
		break;

		case setRUN:
			Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "SR", *_Val, _ETX);
		break;

		case setSP:
			Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "S1", *_Val, _ETX);
		break;

		case setAL1:
			Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "A1", *_Val, _ETX);
		break;

		case setAL2:
			Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "A2", *_Val, _ETX);
		break;

		case setAT:
			Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "G1", *_Val, _ETX);
		break;

		case setST:
			Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "G2", *_Val, _ETX);
		break;
			
		case setPB:
			Fmt(Buff, "%c%i[w2p0]%c%s[w2]%f[p1]%c", _EOT, (_ID & 0xFF), _STX, "PB", *_Val, _ETX);
		break;
	}

	if (NullPort)
	{
		*_Val = 0.00;
	}
	
	// Command Transfer Process
	if ((_ACT > 0x1F) && (NullPort == 0))
	{
		// ========================================================= [ Set RKC Data ]
		// Flush Out Queue
		FlushOutQ (rkc.COM);
		Delay(0.5);
		
		// Count Buffer Length
		BuffLEN = StringLength (Buff);
		// Calc BBC
		BBC = 0x00;
		for (rLop=4; rLop < BuffLEN; rLop++)	BBC ^= Buff[rLop];
		// Append BBC
		Fmt ( Buff, "%s[a]<%c", BBC); 
		BuffLEN +=1;
		// Sending Command
		ComWrt ( rkc.COM, Buff, BuffLEN);
		
		// Flush In Queue	
		FlushInQ (rkc.COM);
		Delay(0.5);
		ComRd (rkc.COM, dTRN, 1);

		if (dTRN[0] == 0x06)
			RTNval =   0;	// Sending OK
		else if (dTRN[0] == 0x15)
			RTNval = -10;	// Sending Lost
		else	
			RTNval =  -1;	// Connect Lost
	} 
	else if ((_ACT > 0x0F) && (NullPort == 0))
	{
		// ========================================================= [ Get RKC Data ]
		// Flush OUT Queue	
		FlushOutQ (rkc.COM);
		Delay(0.5);

		// Send CMD
		ComWrt ( rkc.COM, Buff, 6);
		
		// Flush IN Queue
		FlushInQ (rkc.COM);
		Delay(0.5);

		// Receive CMD
		dTRN[0] = 0x00;
		ComRd (rkc.COM, dTRN, 11);
		Rs232Err = ReturnRS232Err();
		if (ReturnRS232Err() == 0)
			Scan (dTRN ,"%s[i5]>%f", _Val);


		Delay(0.5);
		Fmt ( Buff, "%c%c", _EOT, 0x00);
		ComWrt (rkc.COM, Buff, 1);	
	}
	return RTNval;
}
//MoveTo-------------------------------
//Move to absolute position
//Input: desired z position in um, relative to origin, converstion factor
//Ouput: none
void zStepperDriver::MoveTo(double zPos, double convFactor)
{
	
	char Str [15];
	char out [15];
	char in [15];
	int retVal = 0;
	int termCharVal = 13;
	int numSteps;

	//Calculate wait time
	//double stepWait = 0.010 * (zPos - ReadPos(convFactor)) ;  //In seconds

	//if(stepWait < 0)
	//	stepWait = -stepWait;

	//double waitTime = stepWait + .1;
	
	if(zPos > 0)
	{
		numSteps = (int) (zPos / convFactor + 0.5);
		numSteps += 0;		//Fudge factor
	}
	else
	{	
		numSteps = (int) (zPos / convFactor - 0.5);
		numSteps -= 0;		//Fudge factor
	}
	//Output command to RS232
	sprintf(Str, "MI%i\r", numSteps);
	ComWrt(port, Str, strlen(Str));
	Sleep(0.1*1000);		//*1000 because original value was for Delay function in CVI
//	retVal = FlushInQ (port);
//	Sleep(.5*1000);		//*1000 because original value was for Delay function in CVI
	//Sleep(waitTime*1000);		//*1000 because original value was for Delay function in CVI

	//Wait here until z has reached position
	int delta;
	int test;
	int count = 0;
	sprintf(out, "RT\r");
	do
	{
		
		retVal=ComWrt(port, out, strlen(out));
		Sleep(0.1*1000);		//*1000 because original value was for Delay function in CVI
		//ComRd (port, in, 5);
		retVal=ComRdTerm (port, in, 7, termCharVal);
		if (retVal > 0)
			in[retVal]='\0';
		Sleep(0.1*1000);		//*1000 because original value was for Delay function in CVI
		//printf("%s \n",in);
		//retVal = FlushOutQ (port);
		//Sleep(.5*1000);		//*1000 because original value was for Delay function in CVI
		//retVal=ComRdTerm (port, in, 7, termCharVal);
		//Sleep(.5*1000);		//*1000 because original value was for Delay function in CVI
		delta = atoi(in);
		test = abs(delta);
		sprintf(in,"");
		count++;
	} while(test > 4);


	ReadPos(convFactor);


}