|
发表于 2014-3-21 15:17:37
|
显示全部楼层
/**
* @brief This function implements the tcp_err callback function (called
* when a fatal tcp_connection error occurs.
* @param arg: pointer on argument parameter
* @param err: not used
* @retval None
*/
/*
luodefu - If a connection is aborted because of an error, the application is
alerted of this event by the err callback. Errors that might abort a connection
are when there is a shortage of memory. The callback function to be called is
set using the tcp_err() function.
The error callback function does not get the pcb passed to it as a parameter
since the pcb may already have been deallocated.
*/
static void ModbusTCPClient_error(void *arg, err_t err)
{
struct ModbusTCPClient_Struct *pModbusTCPClient;
LWIP_UNUSED_ARG(err);
pModbusTCPClient = (struct ModbusTCPClient_Struct *)arg;
/* luodefu - 三种情况调用error函数:
(1)连接失败 :调用tcp_connect后,大概20秒报连接出错,其中arg参数
就是tcp_arg(tpcb, pModbusTCPClient)注册的pModbusTCPClient参数;
(2)服务器关闭:实际测试表明,如果modbus server关闭连接,lwip也会调用此函
数,这时释放tpcb和pModbusCmd资源;
(3)网络断线重连:这种情况比较特殊,arg为NULL,所以需要检测arg是否为NULL,
否则会导致系统hard fault异常。
*/
if ( (pModbusTCPClient != NULL) &&
(pModbusTCPClient->state != ES_NOT_CONNECT) ) {
/* Cloce ModbusTCP Client */
/* The error callback function does not get the pcb passed to it as a
parameter since the pcb may already have been deallocated. */
/* pModbusTCPClient->pcb也许已经被释放 */
pModbusTCPClient->state = ES_NOT_CONNECT;//重置为未连接状态
pModbusTCPClient->pcb = NULL;
if (pModbusTCPClient->pModbusCmd != NULL) {
/* there is a remaining pbuf (chain) */
pbuf_free(pModbusTCPClient->pModbusCmd);
pModbusTCPClient->pModbusCmd = NULL;
}
}
}
|
|