山高疑日近,海阔觉天低

新大洋调试日记

1 apt无法连接国内源

问题的根本原因是 apt源是https的,改成HTTP的就可以了

cat << EOF > /etc/apt/sources.list
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security multiverse
EOF

2 给板子配置IP DNS

Ubunt可以直接用yaml文件配置IP

cat << EOF > /etc/netplan/01-network-manager-all.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses: [10.10.12.7/24,192.168.1.150/24]
      gateway4: 10.10.12.250
EOF

cat << EOF > /etc/resolv.conf
nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 8.8.8.8
nameserver 8.8.4.4
EOF

3 程序自启动

cat << EOF > /etc/rc.local
#!/bin/bash -e
#ifconfig eth0 10.10.12.7 broadcast 10.10.12.255 netmask 255.255.255.0
#sleep 3
ifconfig eth1 192.168.10.9 broadcast 192.168.10.255 netmask 255.255.255.0
sleep 5
/root/frp/frpc -c /root/frp/frpc.ini >/dev/null &
energymain >/dev/console &
exit 0
EOF

4 内网穿透

cat << EOF > ~/frp/frpc.ini
[common]
server_addr = <ip>
server_port = 30100

[ssh2]
type = tcp
local_ip = 127.0.0.1
local_port = 26254
remote_port = 30103

5 传感器配置

1 IMS205 GPS  4800
$GPDTM,W84,,0.0,N,0.0,E,+0.0,W84*44
$GPGGA,014557.00,3215.5370,N,11935.3466,E,1,09,01,+0029,M,+005,M,,*4A
$GPGLL,3215.5370,N,11935.3466,E,014557.00,A,A*67
$GPRMB,A,,,,,,,,,,,,,A*4A
$GPRMC,014557.00,A,3215.5370,N,11935.3466,E,000.0,309.2,080325,07.,W,A*2A
$GPVTG,309.2,T,309.2,M,000.0,N,000.0,K,A*23
$GPZDA,014557.00,08,03,2025,00,00*6A
$GPAPB,A,A,,,N,,,,T,,,T,,,A*67
2 IMS206 AIS  自动识别仪 38400
!AIVDM,1,1,,A,36:`B;@P00`RNQ@BLFa3bQvIP000,0*0B
!AIVDO,1,1,,,1815WE1000`SKoNBMFwt56:H04;L,0*6A
!AIVDM,1,1,,A,36:d1oh00L8T<ITBOicS?wv00000,0*10 
!AIVDM,1,1,,B,B6:aPkP0>R8q?@4VgjrNCwv41P06,0*49
!AIVDM,1,1,,A,H6:d810PtpLP4pLQDwK3P000000,2*37
!AIVDM,1,1,,B,B6:bMw@0:B8p9p4Vda`hCwv41P06,0*44

3 IMS204 SPEED LOG  计程仪 4800
$VDVBW,-0.01,,A,,,V,,V,,V*74

4 IMS203 ECHO SOUNDER 测深仪 4800

$SDDBT,22.2,f,6.8,M,3.7,F*3E
$SDDPT,6.8,0,10*6A
$PJRCU,SD,6.8,0,10,,3,FWD*12
$PJRCS,DPT,6.8,0,10,,3,FWD*43
$PJRCL,SD,9,V,V,V,V,V,V,V,V,V,V,V,V,V*13

5 IMS202 SPEED & WIND DIRECTION 风速风向 4800
$WIMWV,011,R,003,N,A*20
7 IMS208  ELECTRONIC INCLINOMETER 电子倾斜 9600  
$PFEC,GPatt,,+02.2,-00.6*66

6 modbustcp 测试

#include "/opt/FriendlyARM/toolchain/6.4-aarch64/usr/include/modbus/modbus.h"
#include 
#include 
#include 
#include 
#include   // 用于 std::stoi
///opt/FriendlyARM/toolchain/6.4-aarch64/usr/lib
//aarch64-cortexa53-linux-gnu-g++ modbus2.cpp  -o modbus  -L/opt/FriendlyARM/toolchain/6.4-aarch64/usr/lib -l modbus

int main(int argc, char *argv[]) {
    // 设置 Modbus TCP 连接的 IP 和端口
    const char *ip = "192.168.1.101"; // Modbus TCP 服务器的 IP 地址
    int port = 504;               // 默认 Modbus TCP 端口是 502
    if (argc != 4) {
        std::cerr << "Usage: " << argv[0] << " <0|1>  " << std::endl;
        return 1;  // 返回错误码
    }
    int ntype=std::stoi(argv[1]);
    int start=std::stoi(argv[2]);
    int nums=std::stoi(argv[3]);
    // 创建一个新的 Modbus TCP 连接
    modbus_t *mb = modbus_new_tcp(ip, port);
    if (mb == nullptr) {
        std::cerr << "Unable to create Modbus TCP context" << std::endl;
        return -1;
    }
    // 设置 Modbus slave 地址 (unit_id),例如 1
    uint8_t slave_address = 3;
    modbus_set_slave(mb, slave_address);
    // 连接到 Modbus TCP 服务器
    if (modbus_connect(mb) == -1) {
        std::cerr << "Connection failed: " << modbus_strerror(errno) << std::endl;
        modbus_free(mb);
        return -1;
    }

    // 读取 Modbus 服务器的寄存器(例如读取 10 个保持寄存器,起始地址为 0)
    if(ntype==0)
    {
        unsigned char tab_reg[1024];
        int rc = modbus_read_bits(mb, start, nums, tab_reg);  // 读取寄存器,0 为起始地址,10 为数量
        if (rc == -1) {
            std::cerr << "Modbus read coil failed: " << modbus_strerror(errno) << std::endl;
            modbus_close(mb);
            modbus_free(mb);
            return -1;
        }
        if (nums < rc)
        {
            std::cerr << "Modbus read coil failed: need " << nums << "actrul: " << rc << std::endl;
            nums = rc;
        }
        // 打印读取的寄存器值
        std::cout << "Read Modbus registers:" << rc << std::endl;
        for (int i = 0; i < nums; i++)
        {
            std::cout << "Coil " << i + start << ": " << (int)tab_reg[i] << std::endl;
        }
    }
    else
    {
        uint16_t tab_reg[1024];
        int rc = modbus_read_registers(mb, start, nums, tab_reg);  // 读取寄存器,0 为起始地址,10 为数量
        if (rc == -1) {
            std::cerr << "Modbus read failed: " << modbus_strerror(errno) << std::endl;
            modbus_close(mb);
            modbus_free(mb);
            return -1;
        }
    
        // 打印读取的寄存器值
        std::cout << "Read Modbus registers:" << rc << std::endl;
        for (int i = 0; i < nums; i++)
        {
            std::cout << "Register " << i + start << ": " << tab_reg[i] << std::endl;
        }
    }
    // 关闭连接并释放资源
    modbus_close(mb);
    modbus_free(mb);
    return 0;
}
赞(0) 打赏
未经允许不得转载:Mr.Zhang » 新大洋调试日记

评论 抢沙发

你的打赏是我的动力

登录

找回密码

注册