2.14.3 Send and Receive
SerialLinkSend and SerialLinkRecv two handshake structures are structure between NC and PLC for serial communication, and INCON-M84/M86/M86R communicates with other device through RS485.
- PLC transmission and reception of handshake data structure is as follows:
Length for the length, buffer for the temporary storage area, finish for the communication completed flag.
struct SerialLinkHandShake {
int length;
unsigned char buffer[SERIALLINK_BUFFER_LENGTH];
int finish;
};
- In the case of Transmit, PLC filled char into buffer[ ] then writes data length into length, after length wrote NC start sends out char string from RS485. NC sets finish as 1 when sent whole string, when PLC sensed finish as 1 writes finish as 0. Examples are as follows:
The following example M68 planning for the transmission of information M code.
void plcRun(Status &sts, PlcBlock &plc)
{
....
if( plc.mCode.flag ) {
switch( plc.mCode.data ) {
case 68:
// length is none zero and finish is 1 were true (send finish)
if( plc.serialLinkSend->length && plc.serialLinkSend->finish ) {
plc.serialLinkSend->length = 0;
plc.mCode.finish = 1;
} else if( plc.serialLinkSend->length == 0 ) { // length is zero send data
plc.serialLinkSend->buffer[0] = 'T';
plc.serialLinkSend->buffer[1] = 'e';
plc.serialLinkSend->buffer[2] = 's';
plc.serialLinkSend->buffer[3] = 't';
plc.serialLinkSend->buffer[4] = '\r';
plc.serialLinkSend->buffer[5] = '\n';
plc.serialLinkSend->length = 6;
}
break;
}
}
...
}
- In terms of reception, when out side device sent char to controller, NC received char and wrote to buffer[ ], and length wrote in char count of buffer[ ], PLC read in and set finish to 1, PLC also does not need wrote finish to 1 instance, and NC will keep update length, and PLC write finish to 1 if needs. Caution the length of buffer[ ] was 256. Examples are as follows:
static char *strCopy( char *Dest, char *Source, int MaxLen )
{
int i;
for( i=0; i<MaxLen; i++ ) {
Dest[i] = Source[i];
if( !Dest[i] )
return Dest;
}
Dest[i+1] = 0;
return Dest;
}
static char serialLinkMsg[SERIALLINK_BUFFER_LENGTH+1];
void plcRun(Status &sts, PlcBlock &plc)
{
...
if( plc.serialLinkRecv ) { // check if function available
if( plc.serialLinkRecv->length ) {
if( plc.serialLinkRecv->length == SERIALLINK_BUFFER_LENGTH ) { // receive length equal buffer length.
strCopy( serialLinkMsg, plc.serialLinkRecv->buffer, SERIALLINK_BUFFER_LENGTH ); // call strCopy to serialLinkRecv.
plc.errorMessage = serialLinkMsg;
plc.serialLinkRecv->finish = 1;
} else {
int c = plc.serialLinkRecv->buffer[plc.serialLinkRecv->length-1];
if( c == '\n' ) { // if C is line feed, call strCopy than shows message.
strCopy( serialLinkMsg, plc.serialLinkRecv->buffer, plc.serialLinkRecv->length );
plc.errorMessage = serialLinkMsg;
plc.serialLinkRecv->finish = 1;
}
}
} else {
plc.serialLinkRecv->finish = 0;
}
}
...
}