68
DMM Technology Corp.
DYN4 AC Servo Drive Instruction Manual
REV. ZM7-A10A
CT1
7.9A Appendix : C++ Code for Serial Communication Protocol
void DlgRun::Get_Function(void)
{
char ID, ReceivedFunction_Code, CRC_Check;
ID = Read_Package_Buffer[0]&0x7f;
ReceivedFunction_Code = Read_Package_Buffer[1]&0x1f;
CRC_Check = 0;
for(int i=0;i<Comm.Read_Package_Length-1;i++)
{
CRC= Read_Package_Buffer[i];
}
CRC_Check ^= Read_Package_Buffer[Comm.Read_Package_Length-1];
CRC_Check &= 0x7f;
if(CRC_Check!= 0){
//MessageBox(“There is CRC error!”) - Customer code to indicate CRC error
}
else
{
switch(ReceivedFunction_Code){
case Is_AbsPos32:
Motor_Pos32 = Cal_SignValue(Read_Package_Buffer);
MotorPosition32Ready_Flag = 0x00;
break;
case Is_TrqCurrent:
MotorTorqueCurrent = Cal_SignValue(Read_Package_Buffer);
MotorTorqueCurrentReady_Flag = 0x00;
break;
case Is_MainGain:
MainGain_Read = Cal_SignValue(Read_Package_Buffer);
MainGainRead_Flag = 0x00;
break;
default:;
}
}
}
/*Get data with sign - long*/
long DlgRun::Cal_SignValue(unsigned char One_Package[8])
{
char Package_Length,OneChar,i;
long Lcmd;
OneChar = One_Package[1];
OneChar = OneChar>>5;
OneChar = OneChar&0x03;
Package_Length = 4 + OneChar;
OneChar = One_Package[2];
/*First byte 0x7f, bit 6 reprents sign */
OneChar = OneChar << 1;
Lcmd = (long)OneChar;
/* Sign extended to 32bits */
Lcmd = Lcmd >> 1;
for(i=3;i<Package_Length-1;i++)
{
OneChar = One_Package[i];
OneChar &= 0x7f;
Lcmd = Lcmd<<7;
Lcmd += OneChar;
}
return(Lcmd);
/* Lcmd : -2^27 ~ 2^27 - 1 */
}
C++ Code for Serial Communication - Page 2