socat
- 安装
sudo apt-get install socat
- 创建终端
socat -d -d pty,b115200 pty,b115200
- 查看终端
ls /dev/pts/
minicom
- 安装
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
交叉编译
- 生成配置文件
./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)
Ubuntu编译
/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 命令。
测试
tcp_client
#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);
}