Dear Christoph,
Kindly find the below tcp task for your reference.
/* ------------------------------------------------------------------------------------------------------------
example for AsTCP- library
connect with ‘tcpclient.py’. Multiple connections in parallel are possible.
---------------------------------------------------------------------------------------------------------------*/
#define _REPLACE_CONST
/*
using _REPLACE_CONST enables defining constants in *.var files
and automatically creates #defines from them.
on the other hand, this leads to a warning 6424 being generated which
can be suppressed by adding
-W 6424
to 'Additional build options' (either to complete project or 'tcp' task only)
*/
#ifdef _DEFAULT_INCLUDES
#include <AsDefault.h>
#endif
#include <bur/plctypes.h>
//#include <AsString.h>
char* p_step_str;
char* p_step_str_old;
char tt=0;
char sel_ip=0;
#define WARPING_KP_IP “192.168.0.10”
#define BEAMING_KP_IP “192.168.0.11”
#define MAIN_PANEL_IP “192.168.0.12”
#define LEASING_PANEL_IP “192.168.0.13”
#define HYDRAULIC_PANEL_IP “192.168.0.14”
#define DEBUG_PORT_IP “192.168.0.17”
#define WARPING_KP 1
#define BEAMING_KP 2
#define MAIN_PANEL 3
#define LEASING_PANEL 4
#define HYDRAULIC_PANEL 5
#define DEBUG_PORT 6
/* --------------------------------------------------------------------------------------------------------
startup after powerfail
*/
void _INIT Initialisierung( void ){
/* this could be useful when task is overloaded... */
brsmemset( (UDINT) &open, 0, sizeof(open) );
brsmemset( (UDINT) &close, 0, sizeof(close) );
brsmemset( (UDINT) &server, 0, sizeof(server) );
brsmemset( (UDINT) &getipaddr, 0, sizeof(getipaddr) );
brsmemset( (UDINT) &client, 0, sizeof(client) );
welcome_message = (USINT*) "Shreenathji Industries\nPress\n'1' for first message\n'2' for second message\n'3' to abort connection\n";
first_message = (USINT*) "Hi test\n";
second_message = (USINT*) "Hi Donald Duck\n";
server.enable = 1;
getipaddr.enable = 1;
getipaddr.pDevice = (UDINT) "IF2";
getipaddr.pIPAddr = (UDINT) myip;
getipaddr.Len = sizeof(myip);
step = SERVER_OPEN;
p_step_str = "INIT_UP";
p_step_str_old = 0;
}
/* --------------------------------------------------------------------------------------------------------
cyclic code
*/
char dd=0;
void _CYCLIC Zyklisch( void ){
int i;
CfgGetIPAddr( &getipaddr );
if( getipaddr.status == 0 ){
getipaddr.enable = 0;
}
switch( step ){
case SERVER_OPEN:
p_step_str = "TcpOpen()";
open.enable = 1;
open.pIfAddr = 0;
open.port = PORT;
open.options = tcpOPT_REUSEADDR ;
TcpOpen( &open );
if( open.status == 0 ){
server.ident = open.ident;
server.backlog = 10;
server.pIpAddr = (UDINT) tempstring;
step = SERVER_RUNNING;
}
else if( open.status != ERR_FUB_BUSY ){
brsstrcpy( (UDINT) errstring, (UDINT) "error TcpOpen()" );
error = open.status;
step = SERVER_ERROR;
}
break;
case SERVER_RUNNING:
p_step_str = "server is running";
TcpServer( &server );
if( server.status == 0 ){ /* a new client has connected */
for( i = 0; i < MAX_CLIENTS; ++i ){
if( !client[i].connected ){
client[i].connected = 1;
brsstrcpy( (UDINT) client[i].addr, (UDINT) tempstring );
client[i].recv.ident = server.identclnt;
client[i].portclnt = server.portclnt;
client[i].recv.enable = 1;
client[i].recv.pData = (UDINT) client[i].receive_buffer;
client[i].recv.datamax = sizeof(client[i].receive_buffer);
client[i].recv.flags = 0;
client[i].send.ident = server.identclnt;
//client[i].send.pData = (UDINT) welcome_message;
//client[i].send.datalen = brsstrlen( (UDINT) welcome_message );
//client[i].send.enable = 1;
client[i].close.enable = 0;
client[i].close.ident = server.identclnt;
break;
}
}
}
/* Noodle through all clients */
for( i = 0; i < MAX_CLIENTS; ++i ){
/* receive data... */
TcpRecv( &client[i].recv );
if( client[i].recv.status == tcpERR_NOT_CONNECTED || client[i].recv.status == tcpERR_INVALID_IDENT || client[i].recv.status == tcpERR_SYSTEM ){
client[i].close.enable = 1;
}
else if( client[i].recv.status == 0 ){
if( client[i].recv.recvlen != 0 ){
client[i].data_received = 1; /* Flag: Data has been received */
}
else if( client[i].recv.recvlen == 0 ){ /* Remote station has closed write direction */
client[i].close.enable = 1;
}
}
/* Process input data ...*/
if( client[i].data_received && client[i].send.enable == 0 ){ /* Data has arrived and send buffer is free */
client[i].data_received = 0;
if(!strcmp(client[i].addr,WARPING_KP_IP)) sel_ip=WARPING_KP;
else if(!strcmp(client[i].addr,BEAMING_KP_IP)) sel_ip=BEAMING_KP;
else if(!strcmp(client[i].addr,MAIN_PANEL_IP)) sel_ip=MAIN_PANEL;
else if(!strcmp(client[i].addr,LEASING_PANEL_IP)) sel_ip=LEASING_PANEL;
else if(!strcmp(client[i].addr,HYDRAULIC_PANEL_IP)) sel_ip=HYDRAULIC_PANEL;
else if(!strcmp(client[i].addr,DEBUG_PORT_IP)) sel_ip=DEBUG_PORT;
else sel_ip=250;
switch(sel_ip)
{
case WARPING_KP:
// if(client[i].recv.recvlen==39 && client[i].receive_buffer[0]=='$' && client[i].receive_buffer[1]==',' && client[i].receive_buffer[2]=='K')
{
Warping_Keypad_KEYS[0]= client[i].receive_buffer[4]-0x30;
Warping_Keypad_KEYS[1]= client[i].receive_buffer[6]-0x30;
Warping_Keypad_KEYS[2]= client[i].receive_buffer[8]-0x30;
Warping_Keypad_KEYS[3]= client[i].receive_buffer[10]-0x30;
Warping_Keypad_KEYS[4]= client[i].receive_buffer[12]-0x30;
Warping_Keypad_KEYS[5]= client[i].receive_buffer[14]-0x30;
Warping_Keypad_KEYS[6]= client[i].receive_buffer[16]-0x30;
Warping_Keypad_KEYS[7]= client[i].receive_buffer[18]-0x30;
Warping_Keypad_KEYS[8]= client[i].receive_buffer[20]-0x30;
Warping_Keypad_KEYS[9]= client[i].receive_buffer[22]-0x30;
Warping_Keypad_KEYS[10]= client[i].receive_buffer[24]-0x30;
Warping_Keypad_KEYS[11]= client[i].receive_buffer[26]-0x30;
Warping_Keypad_KEYS[12]= client[i].receive_buffer[28]-0x30;
Warping_Keypad_KEYS[13]= client[i].receive_buffer[30]-0x30;
Warping_Keypad_KEYS[14]= client[i].receive_buffer[32]-0x30;
//34 & 35 Are CRC
}
for(dd=0;dd<15;dd++)
{
Warping_Keypad_LED[dd]=Warping_Keypad_KEYS[dd];
}
GenBuf[0]='$';
GenBuf[1]=',';
GenBuf[2]='L';
GenBuf[3]=',';
GenBuf[4]=Warping_Keypad_LED[0]+0x30;
GenBuf[5]=',';
GenBuf[6]=Warping_Keypad_LED[1]+0x30;
GenBuf[7]=',';
GenBuf[8]=Warping_Keypad_LED[2]+0x30;
GenBuf[9]=',';
GenBuf[10]=Warping_Keypad_LED[3]+0x30;
GenBuf[11]=',';
GenBuf[12]=Warping_Keypad_LED[4]+0x30;
GenBuf[13]=',';
GenBuf[14]=Warping_Keypad_LED[5]+0x30;
GenBuf[15]=',';
GenBuf[16]=Warping_Keypad_LED[6]+0x30;
GenBuf[17]=',';
GenBuf[18]=Warping_Keypad_LED[7]+0x30;
GenBuf[19]=',';
GenBuf[20]=Warping_Keypad_LED[8]+0x30;
GenBuf[21]=',';
GenBuf[22]=Warping_Keypad_LED[9]+0x30;
GenBuf[23]=',';
GenBuf[24]=Warping_Keypad_LED[10]+0x30;
GenBuf[25]=',';
GenBuf[26]=Warping_Keypad_LED[11]+0x30;
GenBuf[27]=',';
GenBuf[28]=Warping_Keypad_LED[12]+0x30;
GenBuf[29]=',';
GenBuf[30]=Warping_Keypad_LED[13]+0x30;
GenBuf[31]=',';
GenBuf[32]=Warping_Keypad_LED[14]+0x30;
GenBuf[33]=',';
GenBuf[34]='x';
GenBuf[35]='x';
GenBuf[36]=',';
GenBuf[37]='#';
GenBuf[38]='\n';
//strcpy(first_message,"Dat_Reced\n");
client[i].send.pData = (UDINT) GenBuf;
client[i].send.datalen = brsstrlen( (UDINT) client[i].send.pData );
client[i].send.enable = 1;
//strcpy(first_message,"WARPING_KP\n");
//client[i].send.pData = (UDINT) first_message;
//client[i].send.datalen = brsstrlen( (UDINT) client[i].send.pData );
//client[i].send.enable = 1;
break;
case BEAMING_KP:
strcpy(first_message,"BEAMING_KP\n");
client[i].send.pData = (UDINT) first_message;
client[i].send.datalen = brsstrlen( (UDINT) client[i].send.pData );
client[i].send.enable = 1;
break;
case MAIN_PANEL:
strcpy(first_message,"MAIN_PANEL\n");
client[i].send.pData = (UDINT) first_message;
client[i].send.datalen = brsstrlen( (UDINT) client[i].send.pData );
client[i].send.enable = 1;
break;
case LEASING_PANEL:
strcpy(first_message,"DEBUG_PORT\n");
client[i].send.pData = (UDINT) first_message;
client[i].send.datalen = brsstrlen( (UDINT) client[i].send.pData );
client[i].send.enable = 1;
break;
case HYDRAULIC_PANEL:
strcpy(first_message,"HYDRAULIC_PANEL\n");
client[i].send.pData = (UDINT) first_message;
client[i].send.datalen = brsstrlen( (UDINT) client[i].send.pData );
client[i].send.enable = 1;
break;
case DEBUG_PORT:
#ifdef AT
//Warping
//$,A,K1,K2,…K15,CRC,# – Received by PLC
//$,B,L1,L2…L15,CRC,# – Send By PLC
if(client[i].receive_buffer[0]==‘$’ && client[i].receive_buffer[1]==‘,’ && client[i].receive_buffer[2]==‘K’)
{
/*GenBuf[0]= client[i].receive_buffer[4]-0x30;
GenBuf[1]= client[i].receive_buffer[6]-0x30;
GenBuf[2]= client[i].receive_buffer[8]-0x30;
GenBuf[3]= client[i].receive_buffer[10]-0x30;
GenBuf[4]= client[i].receive_buffer[12]-0x30;
GenBuf[5]= client[i].receive_buffer[14]-0x30;
GenBuf[6]= client[i].receive_buffer[16]-0x30;
GenBuf[7]= client[i].receive_buffer[18]-0x30;
GenBuf[8]= client[i].receive_buffer[20]-0x30;
GenBuf[9]= client[i].receive_buffer[22]-0x30;
GenBuf[10]= client[i].receive_buffer[24]-0x30;
GenBuf[11]= client[i].receive_buffer[26]-0x30;
GenBuf[12]= client[i].receive_buffer[28]-0x30;
GenBuf[13]= client[i].receive_buffer[30]-0x30;
GenBuf[14]= client[i].receive_buffer[32]-0x30;
GenBuf[15]= client[i].receive_buffer[34]; //CRC
GenBuf[15]= client[i].receive_buffer[35]; //CRC
*/
Warp_On_Pb= client[i].receive_buffer[4]-0x30;
//Warp_Cente= client[i].receive_buffer[6]-0x30;
Warp_Fwd1= client[i].receive_buffer[8]-0x30;
Warp_Rev1= client[i].receive_buffer[10]-0x30;
Tscr_Uc_Slow= client[i].receive_buffer[12]-0x30;
Cr_W_Start= client[i].receive_buffer[14]-0x30;
Cr_W_Stop= client[i].receive_buffer[16]-0x30;
Cr_W_Inch= client[i].receive_buffer[18]-0x30;
WB_Mc_Left= client[i].receive_buffer[20]-0x30;
WB_Mc_Righ= client[i].receive_buffer[22]-0x30;
Reset_On_Pb= client[i].receive_buffer[24]-0x30;
Creel_Left_side= client[i].receive_buffer[26]-0x30;
Creel_Right_side= client[i].receive_buffer[28]-0x30;
Lease_Up= client[i].receive_buffer[30]-0x30;
Lease_Dn= client[i].receive_buffer[32]-0x30;
}
//sprintf(GenBuf,"$,B,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,CC,#\0",O_Warp_ON,Warp_Centre_Led,O_Warp_Fwd,O_Warp_Rev,UC_Slow,Warp_Start_Led,Warp_Stop_Led,Warp_Inch_Led,Warp_Left_Led,Warp_Right_Led,O_Mc_Reset,Creel_Left_Led,Creel_Right_Led,Lease_Up_Led,Lease_Dn_Led);
GenBuf[0]='$';
GenBuf[1]=',';
GenBuf[2]='A';
GenBuf[3]=',';
GenBuf[4]=O_Warp_ON+0x30;
GenBuf[5]=',';
GenBuf[6]=Warp_Centre_Led+0x30;
GenBuf[7]=',';
GenBuf[8]=O_Warp_Fwd+0x30;
GenBuf[9]=',';
GenBuf[10]=O_Warp_Rev+0x30;
GenBuf[11]=',';
GenBuf[12]=UC_Slow+0x30;
GenBuf[13]=',';
GenBuf[14]=Warp_Start_Led+0x30;
GenBuf[15]=',';
GenBuf[16]=Warp_Stop_Led+0x30;
GenBuf[17]=',';
GenBuf[18]=Warp_Inch_Led+0x30;
GenBuf[19]=',';
GenBuf[20]=Warp_Left_Led+0x30;
GenBuf[21]=',';
GenBuf[22]=Warp_Right_Led+0x30;
GenBuf[23]=',';
GenBuf[24]=O_Mc_Reset+0x30;
GenBuf[25]=',';
GenBuf[26]=Creel_Left_Led+0x30;
GenBuf[27]=',';
GenBuf[28]=Creel_Right_Led+0x30;
GenBuf[29]=',';
GenBuf[30]=Lease_Up_Led+0x30;
GenBuf[31]=',';
GenBuf[32]=Lease_Dn_Led+0x30;
GenBuf[33]=',';
GenBuf[34]='#';
GenBuf[35]='\0';
//strcpy(first_message,"Dat_Reced\n");
client[i].send.pData = (UDINT) GenBuf;
client[i].send.datalen = brsstrlen( (UDINT) client[i].send.pData );
client[i].send.enable = 1;
/*strcpy(first_message,"DEBUG_PORT\n");
client[i].send.pData = (UDINT) first_message;
client[i].send.datalen = brsstrlen( (UDINT) client[i].send.pData );
client[i].send.enable = 1;*/
#endif
break;
default:
strcpy(first_message,"UNKNOWN_PORT\n");
client[i].send.pData = (UDINT) first_message;
client[i].send.datalen = brsstrlen( (UDINT) client[i].send.pData );
client[i].send.enable = 1;
break;
}
}
/*
switch( client[i].receive_buffer[0] ){
case '1':
client[i].send.pData = (UDINT) first_message;
client[i].send.datalen = brsstrlen( (UDINT) client[i].send.pData );
client[i].send.enable = 1;
break;
case '2':
// O_Warp_ON = Warp_On_Pb;
// O_Warp_ON1 = DIGITAL_OUTPUT[0];
GenBuf[0]='$';
GenBuf[1]=',';
GenBuf[2]= Warp_On_Pb+0x30;
GenBuf[3]=',';
GenBuf[4]=DIGITAL_OUTPUT[1]+0x30;
GenBuf[5]=',';
GenBuf[6]='#';
GenBuf[7]='\n';
GenBuf[8]='\0';
client[i].send.pData = (UDINT) GenBuf;
client[i].send.datalen = brsstrlen( (UDINT) client[i].send.pData );
client[i].send.enable = 1;
if(tt==0) tt=1;
else
{
tt=0;
}
MachineData.W_A_Man_Bi=tt;
break;
case '3':
client[i].send.pData = (UDINT) "bye, bye\n";
client[i].send.datalen = brsstrlen( (UDINT) client[i].send.pData );
client[i].send.enable = 1;
client[i].close.enable = 1;
break;
}
}*/
/* send output data */
TcpSend( &client[i].send );
if( client[i].send.status == 0 ){ /* sent successfully */
client[i].send.enable = 0;
}
else if( (client[i].send.status != ERR_FUB_BUSY) && (client[i].send.status != ERR_FUB_ENABLE_FALSE) ){ /* error */
client[i].send.enable = 0;
}
/* Release socket when connection is lost */
TcpClose( &client[i].close );
if( client[i].close.enable == 1 && client[i].close.status == 0 ){
brsmemset( (UDINT) &client[i], 0, sizeof(CLIENT_typ) );
}
} /* end for( i = 0; i < MAX_CLIENTS... */
break;
case SERVER_ERROR:
p_step_str = "error";
break;
}
if( p_step_str != p_step_str_old ){
p_step_str_old = p_step_str;
brsstrcpy( (UDINT) step_str, (UDINT) p_step_str );
}
}
/* --------------------------------------------------------------------------------------------------------
is called on overload of the task
*/
void _EXIT Exit( void ){
int i;
for( i = 0; i < MAX_CLIENTS; ++i ){
do
TcpClose( &client[i].close );
while( client[i].close.status == ERR_FUB_BUSY );
}
do {
close.enable = 1;
close.ident = open.ident;
close.how = 0;
TcpClose( &close );
}
while( close.status == ERR_FUB_BUSY );
}
Thanks & Regards,
Shrenik