Writing to SSC32 in C++

Hi All!

Its my first time using the controller, Ive got it hooked up to a robotic arm and I think Ive got the hang of it so far (been able to send commands to the controller via the hyperlink terminal such as #5 P1000 and the #5 servo motor moves as expected), so for sure it is all connected up properly.

I am now trying to get it set up and working with a user interface but for whatever reason my commands are not transmitting to the controller. Below is the functional part of my code, I cant see whats wrong with my createfile and writefile functions but something must not be right because the arm is not moving. Basically it takes the motor and value integers from another program and then puts them into the char buffer using the sprintf function, from stepping through it I can see that it is working fine as when I check buffer it will give me something like “#3 P300” depending on what I have sent it from the other program.

I think it might be something to do with my createfile function because the pt_port handle has a value of 0xffffffff once it has been defined using createfile, and that does not seem right at all! If anyone here has done this sort of thing before using C++ and has any advice then I would be eternally gratefull! Thankyou for your time!

extern “C” APRON2NUMBER_API int Apron_Do(int id, float data1, float data2,
float* data3, float* data4,
float* data5, float* data6)
{
// Results can be watched by APRON
int result = 0;
DWORD bw = 0;
DWORD cw = 0;
int motor = 0;
int value = 0;
char buffer[128];
char answer[128];
std::wstring command;

	switch(id)
		{
		case 0: 
			if(demo_active)
						{

							if(pt_port != NULL)
							{
								CloseHandle(pt_port);
								pt_port = NULL;
							}

		pt_port = CreateFile("COM3", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); 
		SetupComm(pt_port, 128, 128);
	        GetCommState(pt_port, &pt_dcb);
		pt_dcb.BaudRate = 115200;
		pt_dcb.ByteSize = 8;
		pt_dcb.Parity = NOPARITY;
		pt_dcb.StopBits = ONESTOPBIT;
	        pt_dcb.fBinary = TRUE;
		pt_dcb.fParity = TRUE;
		SetCommState(pt_port, &pt_dcb);

		motor = (int)data1[0];
		value = (int)data2[0];	

		sprintf_s(buffer, "#%d P%d\r\n", motor, value);

		WriteFile(pt_port, &buffer, sizeof(buffer), &bw, NULL); 					
		ReadFile(pt_port, answer, 128, &cw, NULL);

		result = 0;
						}
			else
						{
							result = -1;
						}
			break;
		default:
			result = -1;
		}
	return result;

}

I’m not a C programmer… But, I don’t see a carriage return anywhere. The SSC-32 requires a CR before it will process the commands. But, If the green LED does not go out then it is truly not receiving anything.

I have not been following this much but: sprintf_s(buffer, “#%d P%d\r\n”, motor, value);
Should output a CR(\r) and Newline(\N), which should work I think…

Kurt

I saw that too, it should work. But if he can’t open the “file” (COM3) for output, (ffff sounds like an error code) then that must be addressed first. Maybe try another (com1/com2) port first? Maybe com3 is used already? These kinds of questions.

Alan KM6VV

Thanks for your feedback guys!

I think my problem was that I was leaving the hyperterminal connected whilst trying out the program which in turn hogged the com port, I tried connecting with the hyperterminal then disconnecting and running the program and now the arm seems to respond. However it does so only once before the program freezes but thats something I will take a look at.

I have a further questions however, as you can see I am sending instructions like #5 P1000 to the controller, what is the maximum position value that I can send to the controller? Or is the position value relative to the current position of the motor (if you get what I mean)? If this is the case is there an instruction which I can send where if I vary a value from its minimum to its maximum then I can make a degree of freedom motor on my arm move from (for example) position 0 to its maximum position?

I hope that makes sense and thanks for your help!

The questions you are asking… It’s almost as if you haven’t read the users manual for the SSC-32. :open_mouth:

lynxmotion.com/ViewPage.aspx … ID=70#serv

The values are absolute, and the servo positions can be 500 to 2500, but you’ll probably get less then that, depending on the servo and its implementation, i.e., what “joint” you’re driving. Start small, then slowly move the joint out to explore the range of the servo in question.

SEQ does a nice job of moving the servos, and can dump the ranges to a file, from which you can cut and paste into your application. Lynxterm will work as well, although you might have to write down the values observed.

Alan KM6VV