赞
踩
sudo apt-get install socat
socat -d -d pty,b115200 pty,b115200
ls /dev/pts/
sudo apt-get install minicom
sudo minicom -D /dev/pts/3
minicom -D /dev/pts/1 -H
minicom -D /dev/pts/1 -b B115200
Ctrl+A->Z->E
Ctrl+A->W
Ctrl+A->C
Ctrl+A->X
https://github.com/stephane/libmodbus/
sudo apt-get install dh-autoreconf
./autogen.sh
./configure --host=arm-linux-gnueabihf --prefix=$(pwd)/__install
make
(如果遇到C++版本问题请升级交叉编译器)make install
file libmodbus.so.5.1.0
libmodbus.so.5.1.0: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=d30e9af8a0ccc675f8549abbfa88e8df5574b196, with debug_info, not stripped
cd /usr/lib
mv /root/libmodbus.so.5.1.0 ./
ln -s libmodbus.so.5.1.0 libmodbus.so.5
./libmodbus.exe: /lib/libstdc++.so.6: version `GLIBCXX_3.4.21’ not found (required by ./libmodbus.exe)
/usr/bin/ld: 找不到 -lmodbus collect2: error: ld returned 1 exit status
./a.out: error while loading shared libraries: libmodbus.so.5: cannot open shared object file: No such file or directory
将库文件的路径添加到 /etc/ld.so.conf 文件中,并运行 sudo ldconfig 命令来更新库缓存。例如,如果库文件位于 /home/lux/OpenSource/libmodbus/__install/lib 目录中,您可以将以下行添加到 /etc/ld.so.conf 文件的末尾:
/home/lux/OpenSource/libmodbus/__install/lib
保存文件后,运行 sudo ldconfig 命令。
#include "modbus_tcp.hpp" #include <unistd.h> int main(int argc, char *argv[]) { uint8_t read_buffer[1024]; uint8_t write_buffer[1024]; uint16_t read_buffer_16[3]; uint16_t write_buffer_16[1024]; uint8_t ret = 0; modbus::modbus_tcp modbus("192.168.2.200",60000); //创建一个modbus_tcp对象 for (int i = 0; i < 10; i++) { ret = modbus.read_coil_status(0x0000, 5, read_buffer);//00 01 00 00 00 06 01 01 67 89 00 05 if (ret > 0) { printf("read_coil_status:%d\r\n",ret); for (int j = 0; j < ret; j++) { printf("%02x ",read_buffer[j]); } printf("\r\n"); } usleep(1000*100); ret = modbus.read_input_status(0x0000, 5, read_buffer);//00 01 00 00 00 06 01 01 67 89 00 05 if (ret > 0) { printf("read_input_status:%d\r\n",ret); for (int j = 0; j < ret; j++) { printf("%02x ",read_buffer[j]); } printf("\r\n"); } usleep(1000*100); ret = modbus.read_holding_register(0x0000, 5, read_buffer_16);//00 01 00 00 00 06 01 01 67 89 00 05 if (ret > 0) { printf("read_holding_register:%d\r\n",ret); for (int j = 0; j < ret; j++) { printf("%02x ",read_buffer_16[j]); } printf("\r\n"); } usleep(1000*100); ret = modbus.read_input_register(0x0000, 5, read_buffer_16);//00 01 00 00 00 06 01 04 67 89 00 05 if (ret > 0) { printf("read_input_register:%d\r\n",ret); for (int j = 0; j < ret; j++) { printf("%02x ",read_buffer_16[j]); } printf("\r\n"); } usleep(1000*100); // printf("write_single_coil:%d\r\n",modbus.write_single_coil(0x0000, 0x1234)); // usleep(1000*100); // printf("write_single_register:%d\r\n",modbus.write_single_register(0x0000, 0x1234));//0x06:写单个保持寄存器: 00 01 00 00 00 06 01 06 67 89 12 34 // usleep(1000*100); // printf("write_multiple_coil:%d\r\n",modbus.write_multiple_coil(0x0000, 5, write_buffer));//0x0F(15):写多个线圈: 00 01 00 00 00 08 01 0F 67 89 00 05 01 1F // usleep(1000*100); // printf("write_multiple_registers:%d\r\n",modbus.write_multiple_registers(0x0000, 5, write_buffer_16));//0x10(16):写多个保持寄存器: 00 01 00 00 00 11 01 10 67 89 00 05 0A 00 00 00 00 00 00 00 00 00 05 // usleep(1000*100); } usleep(1000*500); }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。