本帖最后由 cookiedemo1019 于 2020-11-19 13:30 编辑
本人最近尝试使用NUC977与PLC之间进行通信, 测试PC端使用模拟软件,Modbus Poll和Modbus Slave测试,但是发现一个奇怪的问题:
1. PLC作为Server,NUC977作为Client,同时Modbus Poll检测通信结果,可以通信;
2. NUC977作为Server,PLC作为Client,同时Modbus Poll检测通信结果,可以通信;
3. Nuc977作为Server,另一个NUC977作为Client,作为Client的NUC977读取不到数据,但是Modbus Poll可以正确读取到结果,通信失败。
想请教一下有没有使用过libmodbus的大神,给点建议,这中间哪里出了问题??附上NUC977的Modbus代码。
PS:PLC和PC模拟测均是使用很久的厂家提供代码,应该不存在问题。http://whycan.com/t_5568.html%20(%E5%87%BA%E5%A4%84:%E5%93%87%E9%85%B7%E5%BC%80%E5%8F%91%E8%80%85%E7%A4%BE%E5%8C%BA%E3%80%90Nuvoton%20N32905/N32926/NUC972/N9H20/N9H26/N9H30%E3%80%91)]WhyCan Forum(哇酷开发者社区)同步帖子[/url]
Server端代码
void *modbusTcp_process(void *arg)
{
uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
int master_socket;
int rc;
fd_set refset;
fd_set rdset;
/* Maximum file descriptor number */
int fdmax;
ctx = modbus_new_tcp("192.168.150.159", 502);
printf("Modbus Context Created OK!\n");
mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS,
MODBUS_MAX_WRITE_BITS,
MODBUS_MAX_READ_REGISTERS,
MODBUS_MAX_WRITE_REGISTERS);
if (mb_mapping == NULL)
{
fprintf(stderr, "Failed to allocate the mapping: %s\n",
modbus_strerror(errno));
modbus_free(ctx);
exit(-1);
}
server_socket = modbus_tcp_listen(ctx, NB_CONNECTION);
if (server_socket == -1)
{
fprintf(stderr, "Unable to listen TCP connection\n");
modbus_free(ctx);
exit(-1);
}
signal(SIGINT, close_sigint);
/* Clear the reference set of socket */
FD_ZERO(&refset);
/* Add the server socket */
FD_SET(server_socket, &refset);
/* Keep track of the max file descriptor */
fdmax = server_socket;
for (;;)
{
rdset = refset;
if (select(fdmax + 1, &rdset, NULL, NULL, NULL) == -1)
{
perror("Server select() failure.");
close_sigint(1);
}
/* Run through the existing connections looking for data to be
* read */
for (master_socket = 0; master_socket <= fdmax; master_socket++)
{
if (!FD_ISSET(master_socket, &rdset))
{
continue;
}
if (master_socket == server_socket)
{
/* A client is asking a new connection */
socklen_t addrlen;
struct sockaddr_in clientaddr;
int newfd;
/* Handle new connections */
addrlen = sizeof(clientaddr);
memset(&clientaddr, 0, sizeof(clientaddr));
newfd = accept(server_socket, (struct sockaddr *)&clientaddr, &addrlen);
if (newfd == -1)
{
perror("Server accept() error");
}
else
{
FD_SET(newfd, &refset);
if (newfd > fdmax)
{
/* Keep track of the maximum */
fdmax = newfd;
}
printf("New connection from %s:%d on socket %d\n",
inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port, newfd);
}
}
else
{
modbus_set_socket(ctx, master_socket);
rc = modbus_receive(ctx, query);
if (rc > 0)
{
update_modbusmapping(mb_mapping);
modbus_reply(ctx, query, rc, mb_mapping);
}
else if (rc == -1)
{
/* This example server in ended on connection closing or
* any errors. */
printf("Connection closed on socket %d\n", master_socket);
close(master_socket);
/* Remove from reference set */
FD_CLR(master_socket, &refset);
if (master_socket == fdmax)
{
fdmax--;
}
}
}
}
}
}
Client端代码void *modbusTcp_process(void *arg){
modbus_t *ctx;
char index = 0;
float fl1;
int i=0;
/* TCP 127.0.0.1 port 502*/
ctx = modbus_new_tcp("192.168.150.159", 502);
if (modbus_set_slave(ctx, 1) == -1)
{
fprintf(stderr, "Invalid slave ID\n");
modbus_free(ctx);
exit(-1);
}
if (modbus_connect(ctx) == -1)
{
fprintf(stderr, "Modbus connect failed: %s\n",
modbus_strerror(errno));
modbus_free(ctx);
exit(-1);
}
while(1){
modbus_read_registers(ctx, WT_START_ADDR,
24, modbusDataBuf);
printf("Read hold rigister %d.\n",sizeof(modbusDataBuf));
//ModbudDataPrase(modbusDataBuf);
ModbusReceived = 1;
//debug_LoadData_print();
for(i=0;i<12;i++)
printf("modbusDataBuf%d %7d\n",i,modbusDataBuf[i]);
index++;
}
/*turn off connect*/
modbus_close(ctx);
modbus_free(ctx);
}
|