7.9A Appendix : C++ Code for Serial Communication Protocol
The following code shows an example to generate a data packet and call functions in RS232 serial protocol.
Note: in the description of RS232 communication protocol above (Section 7), the last byte of packet is
always B0, but in the code of below, the first byte is always B0.
#define Go_Absolute_Pos 0x01
#define Is_AbsPos32 0x1b
#define General_Read 0x0e
#define Is_TrqCurrent 0x1e
#define Read_MainGain 0x18
#define Is_MainGain 0x10
char InputBuffer[256]; //Input buffer from RS232,
char OutputBuffer[256]; //Output buffer to RS232,
unsigned char InBfTopPointer,InBfBtmPointer;//input buffer pointers
unsigned char OutBfTopPointer,OutBfBtmPointer;//output buffer pointers
unsigned char Read_Package_Buffer[8],Read_Num,Read_Package_Length,Global_Func;
unsigned char MotorPosition32Ready_Flag, MotorTorqueCurrentReady_Flag, MainGainRead_Flag;
long Motor_Pos32;
int MotorTorqueCurrent, MainGain_Read;
void DlgRun::ReadPackage()
{
unsigned char c,cif;
ReadRS232Port(); // Include customer code to read from serial port
while(There is data in the customer hardware RS232 receiving Buffer)
{
InputBuffer[InBfTopPointer] = HardwaerRS232ReceiveBuffer; //Load InputBuffer with received packets
InBfTopPointer++;
}
while(InBfBtmPointer!=InBfTopPointer)
{
c = InputBuffer[Comm.InBfBtmPointer];
InBfBtmPointer++;
cif = c&0x80;
if(cif==0)
{
Read_Num = 0;
Read_Package_Length = 0;
}
if(cif==0||Read_Num>0)
{
Read_Package_Buffer[Read_Num] = c;
Read_Num++;
if(Read_Num==2)
{
cif = c>>5;
cif = cif&0x03;
Read_Package_Length = 4 + cif;
c = 0;
}
if(Read_Num==Read_Package_Length)
{
Get_Function();
Read_Num = 0;
Read_Package_Length = 0;
}
}
}
}
C++ Code for Serial Communication - Page 1
!