함수는 간단한데.. 예제가 이상하게 만들어 놔서 헷갈렸네..

아무튼 modbus_mapping_new() 함수의 인자들은 

coil / discrete input / holding register / input register 에 대한 변수를 몇개까지 유지하냐에 대한 값을 받는다.

 

modbus_mapping_t modbus_mapping_new(int nb_bits, int nb_input_bits, int nb_registers, int nb_input_registers);

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

 

libmodbus의 test 에 생성된 unit-test.h 소스에서 추출하고

 29 const uint16_t UT_BITS_ADDRESS = 0x130;
 30 const uint16_t UT_BITS_NB = 0x25;
 31 const uint8_t UT_BITS_TAB[] = { 0xCD, 0x6B, 0xB2, 0x0E, 0x1B };
 32
 33 const uint16_t UT_INPUT_BITS_ADDRESS = 0x1C4;
 34 const uint16_t UT_INPUT_BITS_NB = 0x16;
 35 const uint8_t UT_INPUT_BITS_TAB[] = { 0xAC, 0xDB, 0x35 };
 36
 37 const uint16_t UT_REGISTERS_ADDRESS = 0x160;
 38 const uint16_t UT_REGISTERS_NB = 0x3;
 39 const uint16_t UT_REGISTERS_NB_MAX = 0x20;
 40 const uint16_t UT_REGISTERS_TAB[] = { 0x022B, 0x0001, 0x0064 };

 56 const uint16_t UT_INPUT_REGISTERS_ADDRESS = 0x108;
 57 const uint16_t UT_INPUT_REGISTERS_NB = 0x1;
 58 const uint16_t UT_INPUT_REGISTERS_TAB[] = { 0x000A };

 

최신 문서에서 보는데 오히려 더 헷갈린다.

/* The first value of each array is accessible from the 0 address. */
mb_mapping = modbus_mapping_new(BITS_ADDRESS + BITS_NB,
                                INPUT_BITS_ADDRESS + INPUT_BITS_NB,
                                REGISTERS_ADDRESS + REGISTERS_NB,
                                INPUT_REGISTERS_ADDRESS + INPUT_REGISTERS_NB);

[링크 : https://libmodbus.org/docs/v3.1.6/modbus_mapping_new.html]

 

아래처럼 수정하고

        mb_mapping = modbus_mapping_new(10,10,10,10);

 

modbus poll 프로그램에서 아래와 같이 실제 설정된 크기보다 크게 읽도록 하니

 

illegal data address 라고 에러가 발생한다.

 

아무튼.. 메모리 시작 번지는 의미가 없고 그냥 0번지 부터 해당 크기 만큼 응답하게 되는 듯?

 

+

[링크 : https://www.codetd.com/ko/article/12030369]

[링크 : https://m.blog.naver.com/ssundong0_0/221385568015]

[링크 : https://intrepidgeeks.../libmodbus-source-code-analysis-1-basic-framework-key-data-structure-and-interface]

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

libmodbus pi 함수들  (0) 2022.05.10
libmodbus poll 적용  (0) 2022.05.04
modbus tcp  (0) 2022.05.04
libmodbus 예제 프로그램  (0) 2022.05.04
libmodbus tcp 예제  (0) 2022.05.04
Posted by 구차니