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