Below is the code:
/**************************************************************************
control_x_x_x.c
**************************************************************************/
// Set a default of declaring all local variables “auto” (on stack)
#class auto
#define DCRTCP_DEBUG
#define DCRTCP_VERBOSE
#define TCPCONFIG 1
#define _PRIMARY_STATIC_IP “192.168.10.10”
#define _PRIMARY_NETMASK “255.255.255.0”
#define MY_GATEWAY “192.168.10.1”
#define MY_NAMESERVER “192.168.10.1”
#define PORT_EXT 23
#define MAX_TCP_SOCKET_BUFFERS 20
#define DPM_A31 “192.168.10.31”
#define DPM_PORT 502
#memmap xmem
#use “dcrtcp.lib”
#use “BLxS2xx.lib”
#use “sdflash.lib”
#define VERSION “1.2.f”
#define END_TEXT “End of debug”
// Global vars
tcp_Socket socket_dpm_31, socket_ext;
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void main()
{
struct tm t;
long error_count = 0;
long max_reading_time_31 = 0;
long start_time = 0;
long active_loop_count = 0;
int j, rc, write_status = 0;
int bytes_read, total_bytes_read = 0;
char buffer[100];
word iface;
char t_mac_buf[6], t_mac_addr[40];
longword ip_dpm_31;
int a_count;
char store31[10];
char buffer31[10];
int temp_bytes_31, bytes_read_31;
// Initialize the controller
brdInit();
// Start network and wait for interface to come up (or error exit).
sock_init_or_exit(1);
// get MAC address for the rabbit
for (iface = 0; iface < IF_MAX; ++iface)
if (is_valid_iface(iface))
{
printf(“Interface %u is valid.
“);
if (!ifconfig(iface, IFG_HWA, t_mac_buf, IFS_END))
{
sprintf(t_mac_addr,”
\rHardware address is %02x:%02x:%02x:%02x:%02x:%02x”,
t_mac_buf[0],t_mac_buf[1],t_mac_buf[2],
t_mac_buf[3],t_mac_buf[4],t_mac_buf[5]);
printf("
%s", t_mac_addr);
}
else
{
printf("
No hardware address for this interface");
}
}
while(1)
{
// Costate #1
costate
{
if(!tcp_listen(&socket_ext,PORT_EXT,0,0,NULL,0))
{
printf("
\rERROR: Failed to open ext connection on port %d", PORT_EXT);
abort;
}
while(!sock_established(&socket_ext) && sock_bytesready(&socket_ext)==-1)
{
waitfor(DelayMs(10)); // probably can be longer --------------------------------------------------
tcp_tick(NULL);
}
tcp_tick(NULL);
waitfor(DelayMs(100));
//sock_write(&socket_ext,"
ERROR “,sizeof(”
ERROR ")-1);
}
// Costate #2
costate
{
start_time = MS_TIMER;
// DPM A 31 connection initialization
ip_dpm_31 = resolve(DPM_A31);
tcp_open(&socket_dpm_31, 0, ip_dpm_31, DPM_PORT, NULL);
printf("Waiting for connection DPM 31...
“);
a_count = 0;
do
{
waitfor(DelayMs(20));
a_count++;
tcp_tick(NULL);
printf(”
establish dpm socket count: %d", a_count);
} while(((!sock_established(&socket_dpm_31) && sock_bytesready(&socket_dpm_31)==-1)) && ((a_count > 0) && (a_count < 20)));
if (a_count == 20)
{
printf("Connection failed DPM …
");
}
else
{
printf("Connection received DPM 31…
");
}
// Reading loop
do
{
start_time = MS_TIMER;
waitfor(DelayMs(20));
bytes_read_31 = 0;
write_status = 0;
write_status = sock_write(&socket_dpm_31,"*1B1\r\l",sizeof("*1B1\r\l")-1);
tcp_tick(NULL);
if (write_status == -1)
{
printf("
\rERROR on socket: Unabled to write to socket, will try again…“);
waitfor(DelayMs(500));
write_status = 0;
tcp_tick(NULL);
write_status = sock_write(&socket_dpm_31, “*1B1\r\l”, sizeof(”*1B1\r\l") - 1);
if(write_status == -1)
{
printf("
\r=== ERROR on socket: Unabled to write to socket on retry");
}
}
if(write_status != -1)
{
a_count = 0;
do
{
tcp_tick(NULL);
waitfor(DelayMs(20));
temp_bytes_31 = sock_bytesready(&socket_dpm_31);
a_count++;
} while ((temp_bytes_31 == -1) && (a_count < 10));
bytes_read_31 = sock_fastread(&socket_dpm_31,buffer31,sizeof(buffer31)-1);
}
if(bytes_read_31 > 0)
{
buffer31[bytes_read_31]=0;
}
else // MODIFIED FOR TEST
{
error_count = error_count + 1;
mktm(&t, dc_timestamp);
printf("
\r%02d:%02d:%02d
“, t.tm_hour, t.tm_min, t.tm_sec);
printf(”
== buffer31 bytes read: %d", bytes_read_31);
}
if(active_loop_count > 10000)
{
active_loop_count = 0;
}
else
{
active_loop_count = active_loop_count + 1;
if(active_loop_count % 23 == 0)
{
printf("
ERROR_COUNT = %d
", error_count);
}
}
} while(1);
sock_close(&socket_dpm_31);
waitfor(DelayMs(50)); // Make sure the socket is completely closed
} // end costate fast DPM bank A
} // end while
}