protocol
This commit is contained in:
1 parent
4e80a72d24
commit
9f3b8cf65a
24 files changed
+619
-3018
No files matched your search
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* @file modbustcpslave.c
|
||||
* @brief Modbus TCP从站服务 + Modbus TCP转RTU转发
|
||||
*/
|
||||
|
||||
#include "modbustcpslave.h"
|
||||
#include "socket.h"
|
||||
#include "wizchip_conf.h"
|
||||
#include "modbusSlave.h"
|
||||
#include "common.h"
|
||||
// ============================================================================
|
||||
// 全局变量
|
||||
// ============================================================================
|
||||
|
||||
uint8_t loopback_tcps_modbus_rxbuf[1024];
|
||||
uint8_t loopback_tcps_modbus_txbuf[1024];
|
||||
|
||||
static uint16_t commled_off_timeout = 0;
|
||||
static uint8_t comm_count;
|
||||
|
||||
void update_modbusTCP_timeout() {
|
||||
if (commled_off_timeout < 0xffff) {
|
||||
commled_off_timeout++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Modbus TCP服务主函数,W5500socket主循环处理
|
||||
* @param sn: socket编号
|
||||
* @param port: 监听端口 (通常502)
|
||||
* @return 0/正表示正常,负表示错误
|
||||
*/
|
||||
int32_t loopback_tcps_modbus(uint8_t sn, uint16_t port) {
|
||||
|
||||
int32_t ret;
|
||||
uint16_t size = 0, sentsize = 0;
|
||||
|
||||
#ifdef _LOOPBACK_DEBUG_
|
||||
uint8_t destip[4];
|
||||
uint16_t destport;
|
||||
#endif
|
||||
|
||||
switch (getSn_SR(sn)) {
|
||||
case SOCK_ESTABLISHED:
|
||||
if (getSn_IR(sn) & Sn_IR_CON) {
|
||||
#ifdef _LOOPBACK_DEBUG_
|
||||
getSn_DIPR(sn, destip);
|
||||
destport = getSn_DPORT(sn);
|
||||
printf("%d:Connected - %d.%d.%d.%d : %d\r\n", sn, destip[0],
|
||||
destip[1], destip[2], destip[3], destport);
|
||||
#endif
|
||||
setSn_IR(sn, Sn_IR_CON);
|
||||
}
|
||||
|
||||
// 接收请求
|
||||
if ((size = getSn_RX_RSR(sn)) > 0) {
|
||||
comm_count++;
|
||||
commled_off_timeout = 0;
|
||||
if (size > DATA_BUF_SIZE) {
|
||||
size = DATA_BUF_SIZE;
|
||||
}
|
||||
ret = recv(sn, loopback_tcps_modbus_rxbuf, size);
|
||||
if (ret <= 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 处理Modbus TCP请求
|
||||
uint16_t rxbuf_size = handleModbusTcp(loopback_tcps_modbus_rxbuf, loopback_tcps_modbus_txbuf);
|
||||
if (rxbuf_size > 0) {
|
||||
sentsize = 0;
|
||||
// 分片发送直到完成
|
||||
while (rxbuf_size != sentsize) {
|
||||
ret = send(sn, loopback_tcps_modbus_txbuf + sentsize, rxbuf_size - sentsize);
|
||||
if (ret < 0) {
|
||||
close(sn);
|
||||
return ret;
|
||||
}
|
||||
sentsize += ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SOCK_CLOSE_WAIT:
|
||||
#ifdef _LOOPBACK_DEBUG_
|
||||
//printf("%d:CloseWait\r\n",sn);
|
||||
#endif
|
||||
if ((ret = disconnect(sn)) != SOCK_OK) {
|
||||
return ret;
|
||||
}
|
||||
#ifdef _LOOPBACK_DEBUG_
|
||||
printf("%d:Socket Closed\r\n", sn);
|
||||
#endif
|
||||
break;
|
||||
|
||||
case SOCK_INIT:
|
||||
#ifdef _LOOPBACK_DEBUG_
|
||||
printf("%d:Listen, TCP server loopback, port [%d]\r\n", sn, port);
|
||||
#endif
|
||||
if ((ret = listen(sn)) != SOCK_OK) {
|
||||
return ret;
|
||||
}
|
||||
break;
|
||||
|
||||
case SOCK_CLOSED:
|
||||
#ifdef _LOOPBACK_DEBUG_
|
||||
//printf("%d:TCP server loopback start\r\n",sn);
|
||||
#endif
|
||||
if ((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn) {
|
||||
return ret;
|
||||
}
|
||||
#ifdef _LOOPBACK_DEBUG_
|
||||
//printf("%d:Socket opened\r\n",sn);
|
||||
#endif
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
Reference in new issue
Block a user