本帖最后由 likai214 于 2017-6-22 11:37 编辑
馈线终端项目(FTU)目前已将k60代码移植到k64上了,mqx也从原来的4.0升级到4.1,各项通讯模块都正常运行了。
但是rtcs(tcp)k64服务器模式建立多链路时出现响应不及时的情况,现象如下:
1. 双网口,一路lan8720,一路enc424,任意一路网口单独建立一条tcp连接通讯均正常;
2.建立2条tcp链路(不论是一个网口还是通过两个网口):第一条链路建立后可以正常通讯;之后建立第二条链路成功,此时主站召收该链路数据无响应,如果此时主站召收第一条链路数据,第一条、第二条链路数据同时回传主站;此后变成第二条链路可以正常通讯,第一条链路主站召收无响应,也只能通过主站召收第二条链路,第一条、第二条链路数据同时回传主站;之后再变成第一条链路通讯正常,第二条链路被“”阻塞“”,来回交替。
之前在k60,mqx4.0上可以建立4条链路通讯正常,有可能是我移植应用层代码的时候出错,也有可能是rtcs配置还有参数没设置对,望指点,谢谢。
/*===============================================================================================================================
函 数:uint_8 byTcpServerIec104Start(void)
功 能:初始化rtcs协议栈,绑定网络参数
参 数:无
返回值:无
---------------------------------------------------------------------------------------------------------------------------------
更 新:2015-10-21 10:46:09
===============================================================================================================================*/
uint8_t byTcpServerIec104Start(void)
{
uint32_t error, phy_addr[BSP_ENET_DEVICE_COUNT];
uint32_t dwHostNetMask[BSP_ENET_DEVICE_COUNT]={0}, dwHostGateWay[BSP_ENET_DEVICE_COUNT]={0};
IPCFG_IP_ADDRESS_DATA ip_data[BSP_ENET_DEVICE_COUNT];
DisableInterrupts;
_RTCSTASK_stacksize = 3000;
error = RTCS_create();
if (error != RTCS_OK)
{
return TCPIP_ERROR;
}
_IP_forward = FALSE;
bySys_CfgRead(ECFG_HOSTIP, ECFG_LEN_HOSTIP, (uint8_t *)&g_dwIpAddr[0]);
bySys_CfgRead(ECFG_HOSTNETMASK, ECFG_LEN_HOSTNETMASK, (uint8_t *)&dwHostNetMask[0]);
bySys_CfgRead(ECFG_HOSTGATEWAY, ECFG_LEN_HOSTGATEWAY, (uint8_t *)&dwHostGateWay[0]);
ip_data[0].ip = g_dwIpAddr[0];
ip_data[0].mask = dwHostNetMask[0];
ip_data[0].gateway = dwHostGateWay[0];
phy_addr[0]=0;
ENET_get_mac_address (phy_addr[0], g_dwIpAddr[0], gaddress[0]);
ipcfg_init_device (phy_addr[0], gaddress[0]);
error = ipcfg_bind_staticip (phy_addr[0], &ip_data[0]);
if (error != RTCS_OK)
{
return TCPIP_ERROR;
}
bySys_CfgRead(ECFG_HOSTIP_SLAVE, ECFG_LEN_HOSTIP, (uint8_t *)&g_dwIpAddr[1]);
bySys_CfgRead(ECFG_HOSTNETMASK_SLAVE, ECFG_LEN_HOSTNETMASK, (uint8_t *)&dwHostNetMask[1]);
bySys_CfgRead(ECFG_HOSTGATEWAY_SLAVE, ECFG_LEN_HOSTGATEWAY, (uint8_t *)&dwHostGateWay[1]);
ip_data[1].ip = g_dwIpAddr[1];
ip_data[1].mask = dwHostNetMask[1];
ip_data[1].gateway = dwHostGateWay[1];
phy_addr[1]=1;
ENET_get_mac_address (phy_addr[1], g_dwIpAddr[1], gaddress[1]);
ipcfg_init_device (phy_addr[1], gaddress[1]);
error = ipcfg_bind_staticip (phy_addr[1], &ip_data[1]);
if (error != RTCS_OK)
{
return TCPIP_ERROR;
}
EnableInterrupts;
return TCPIP_OK;
}
/*===============================================================================================================================
函 数:void bySocketCheckPriority(uint8_t num)
功 能:Socket链路优先级校验
参 数:num:当前连接或断开链路号
返回值:无
---------------------------------------------------------------------------------------------------------------------------------
更 新:2016-12-22 19:33:00
===============================================================================================================================*/
void ServerTcpProcess(void)
{
uint8_t i, num, counts, hing_priority;
uint16_t rlen, wI;
uint32_t error, length, option;
static sockaddr_in raddr, server_sock_v4;
my_sock_v4[0] = socket(AF_INET, SOCK_STREAM, 0);
if (my_sock_v4[0] == RTCS_SOCKET_ERROR)
{
_task_block();
}
option = 1;
error = setsockopt(my_sock_v4[0], SOL_TCP, OPT_KEEPALIVE, &option, sizeof(option));
if (error != RTCS_OK)
{
_task_block();
}
option = 180000;
error = setsockopt(my_sock_v4[0], SOL_TCP, OPT_CONNECT_TIMEOUT, &option, sizeof(option));
if (error != RTCS_OK)
{
_task_block();
}
option = TRUE;
error = setsockopt(my_sock_v4[0], SOL_TCP, OPT_NO_NAGLE_ALGORITHM, &option, sizeof(option));
if (error != RTCS_OK)
{
_task_block();
}
server_sock_v4.sin_port = 2404;
server_sock_v4.sin_addr.s_addr = INADDR_ANY;
server_sock_v4.sin_family = AF_INET;
error = bind(my_sock_v4[0], (const sockaddr *)&server_sock_v4 , sizeof(server_sock_v4));
if (error != RTCS_OK)
{
_task_block();
}
error = listen(my_sock_v4[0], 0);
if (error != RTCS_OK)
{
_task_block();
}
socket_fset[0] = my_sock_v4[0];
sock_count = 1;
Socket_Num_List[0].sount = 0;
Socket_Num_List[0].use = 1;
Socket_Num_List[0].current_sock = my_sock_v4[0];
for (;;)
{
active_sock = RTCS_selectset(socket_fset, sock_count, 300);
if (active_sock == my_sock_v4[0])
{
rlen = sizeof(raddr);
active_sock = accept(my_sock_v4[0], (sockaddr *)&raddr, &rlen);
if(active_sock==RTCS_SOCKET_ERROR)
{
if((++counts) > 5)
{
T_event_flag |= 0x100; //rtcs协议栈故障 本地复位
}
}
else if(sock_count < SOCKET_INIT_NUM)
{
counts = 0;
hing_priority = SOCKET_INIT_NUM - 2;
socket_fset[sock_count++] = active_sock;
for(wI=1;wI<SOCKET_INIT_NUM;wI++)
{
if(!Socket_Num_List[wI].use) //当前链表未使用
{
if(0 == Socket_Num_List[wI].priority)
{
num = wI;
break;
}
else if(hing_priority > Socket_Num_List[wI].priority)
{
hing_priority = Socket_Num_List[wI].priority;
num = wI;
}
}
}
register SOCKET_STRUCT_PTR socket_ptr = (SOCKET_STRUCT_PTR)active_sock;
Socket_Num_List[num].sount = num;
Socket_Num_List[num].use = 1;
Socket_Num_List[num].current_sock = active_sock;
bySocketCheckPriority(num);
for(i=0;i<BSP_ENET_DEVICE_COUNT;i++)
{
if(socket_ptr->LOCAL_IPADDRESS == g_dwIpAddr)
{
Socket_Num_List[num].current_device = i;
break;
}
}
byProIec104Reset(Socket_Num_List[num].sount-1,P104_SOCKET_CONNECT);
byIecRamWdUlg(2,1,0,0,NULL);
}
}
else if((active_sock != RTCS_SOCKET_ERROR) && (active_sock))
{
for(num=1; num<SOCKET_INIT_NUM; num++)
{
if(active_sock == Socket_Num_List[num].current_sock)
{
break;
}
}
length = recv(active_sock, g_abyTcpServerIec104Mem, SOCKET_BUF_LEN, 0);
if((length <= 0) || (length == RTCS_SOCKET_ERROR))
{
byMYSocket_Close(num-1);
}
else
{
for (wI=0; wI<length; wI++)
{
vProIec104RcvByte(Socket_Num_List[num].sount-1, g_abyTcpServerIec104Mem[wI]);
}
}
}
}
}
|
|