'2022/05/03'에 해당되는 글 2건

  1. 2022.05.03 libmodbus
  2. 2022.05.03 modbus tcp 테스트 툴

 

$ apt-cache search libmodbus
libmodbus5 - library for the Modbus protocol
libmodbus-dev - development files for the Modbus protocol library

 

libmodbus에서 기본으로 제공하는 예제인데

127.0.0.1의 1502번 포트로 접속을 해서 0번 address에 5바이트를 읽어 오도록 하는 명령이다

modbus로는 0x03(read holding register) 명령으로 5바이트 읽는 건데..

이게 modbus tcp의 master 라고 해야하나?

#include <stdio.h>
#include <modbus.h>

int main(void) {
  modbus_t *mb;
  uint16_t tab_reg[32];

  mb = modbus_new_tcp("127.0.0.1", 1502);
  modbus_connect(mb);

  /* Read 5 registers from the address 0 */
  modbus_read_registers(mb, 0, 5, tab_reg);

  modbus_close(mb);
  modbus_free(mb);
}

[링크 : https://libmodbus.org/documentation/]

[링크 : https://libmodbus.org/]

 

Client

The Modbus protocol defines different data types and functions to read and write them from/to remote devices. The following functions are used by the clients to send Modbus requests:

Server

The server is waiting for request from clients and must answer when it is concerned by the request. The libmodbus offers the following functions to handle requests:

[링크 : https://libmodbus.org/docs/v3.0.8/]

 

SYNOPSIS

int modbus_receive(modbus_t *ctx, uint8_t *req);

DESCRIPTION

The modbus_receive() function shall receive an indication request from the socket of the context ctx. This function is used by Modbus slave/server to receive and analyze indication request sent by the masters/clients.
If you need to use another socket or file descriptor than the one defined in the context ctx, see the function modbus_set_socket(3).
 

[링크 : https://libmodbus.org/docs/v3.0.8/modbus_receive.html]

 

SYNOPSIS

*int modbus_reply(modbus_t *ctx, const uint8_t *req, int req_length, modbus_mapping_t *mb_mapping);

DESCRIPTION

The modbus_reply() function shall send a response to received request. The request req given in argument is analyzed, a response is then built and sent by using the information of the modbus context ctx.
If the request indicates to read or write a value the operation will done in the modbus mapping mb_mapping according to the type of the manipulated data.
If an error occurs, an exception response will be sent.
This function is designed for Modbus server.
 

[링크 : https://libmodbus.org/docs/v3.0.8/modbus_reply.html]

 

MODBUS-TCP 통신규격에는 마스터(Client)와 슬레이브(Server)의 역할이 나누어져 있습니다. 슬레이브(Server)는 마스터(Client)가 요청하는 데이터에 대해 응답을 해줍니다. 주로 마스터(Client)에는 산업용터치 HMI 기기, 또는 PC 와 같은 상위 기기가 위치합니다. 그리고 슬레이브(Server)에는 TCPPORT 나 PLC 등이 위치합니다.
슬레이브(Server)는 상위기기에서 요청하는 동작만을 하는 수동적인 위치에 있습니다. 반면 마스터(Client)쪽에서는 원하는 데이터를 읽어오거나, 원하는 데이터를 기입하는 등 적극적으로 슬레이브(Server) 기기를 다루어 주어야 합니다.

[링크 : http://comfilewiki.co.kr/ko/doku.php?id=tcpport:modbus-tcp_프로토콜이란:index]

[링크 : https://gosuway.tistory.com/374]

 

+

int main(void)
{
  int i;
  int s = -1;
  modbus_t *ctx;
  modbus_mapping_t *mb_mapping;
  
  ctx = modbus_new_tcp("127.0.0.1", 1502);
  //    modbus_set_debug(ctx, TRUE); 
  
  mb_mapping = modbus_mapping_new(0, 0, 500, 500);
  if (mb_mapping == NULL) {
    fprintf(stderr, "Failed to allocate the mapping: %s\n",
    modbus_strerror(errno));
    modbus_free(ctx);
    return -1;
  }
  
  s = modbus_tcp_listen(ctx, 1);
  modbus_tcp_accept(ctx, &s);
  
  for (;;) {
    uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
    int rc;
    
    rc = modbus_receive(ctx, query);
    printf("SLAVE: regs[] =\t");
    for(i = 1; i != 11; i++) { // looks like 1..n index
      printf("%d ", mb_mapping->tab_registers[i]);
    }
    printf("\n");
    
    if (rc > 0) {
      /* rc is the query size */
      modbus_reply(ctx, query, rc, mb_mapping);
    } else if (rc == -1) {
      /* Connection closed by the client or error */
      break;
    }
  }
  
  printf("Quit the loop: %s\n", modbus_strerror(errno));
  
  if (s != -1) {
    close(s);
  }
  modbus_mapping_free(mb_mapping);
  modbus_close(ctx);
  modbus_free(ctx);
  
  return 0;
}

[링크 : https://github.com/pjmaker/libmodbus-wee-example/blob/master/slave.c]

'프로그램 사용 > libmodbus' 카테고리의 다른 글

libmodbus 예제 프로그램  (0) 2022.05.04
libmodbus tcp 예제  (0) 2022.05.04
modbus tcp 테스트 툴  (0) 2022.05.03
modbus tcp library  (0) 2022.04.25
modbus 프로토콜  (0) 2015.09.16
Posted by 구차니

윈도우용 바이너리는 하나인데. 아마 32비트일 것 같고

linux용 바이너리는 arm/aarch64/x86/x64 용을 제공한다.

 

modbus TCP 라고해서 slave Address가 사라지는건 아닌가 보네

-m tcp        MODBUS/TCP protocol (default otherwise)
-a #          Slave address (1-255 for serial, 0-255 for TCP, 1 is default)\n
-r #          Start reference (1-65536, 1 is default)
-c #          Number of values to read (1-125, 1 is default), optional for writing (use -c 1 to force FC5 or FC6)

 

GUI는 아니지만 license가 일반적인 사용에는 완전 free 라서 유용하게 쓸 수 있을 지도 모르겠다

[링크 : https://www.modbusdriver.com/modpoll.html]

'프로그램 사용 > libmodbus' 카테고리의 다른 글

libmodbus 예제 프로그램  (0) 2022.05.04
libmodbus tcp 예제  (0) 2022.05.04
libmodbus  (0) 2022.05.03
modbus tcp library  (0) 2022.04.25
modbus 프로토콜  (0) 2015.09.16
Posted by 구차니