58
DYN2MS-02F-0116A17
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
InBfTop+;
}
while(InBfBtmPointer!=InBfTopPointer)
{
c = InputBuffer[Comm.InBfBtmPointer];
InBfBtm+;
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;
R+;
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
!
DYN232M
DYN232M
D Y N A C S E RV O S Y S T E M - R S 2 3 2 M O T I O N
TM
TM
DTPU
P O S I T I O N I N G