openplc Linux 使用modbus RTU 从机通讯

发布于:2024-04-20 ⋅ 阅读:(25) ⋅ 点赞:(0)

1.Linux 环境下,openplc 默认使用的是modbus tcp协议通信。

        想要使用串口 modbus rtu 通讯可以通过在runtime中添加SlaveDevices从机设备

 2.添加设备,分配地址。

        左边添加串口配置,右边是需要通讯的地址,从机地址都是从100开始,100以内是主机使用。地址最好连续分配使用。

         Discrete Inputs (%IX100.0):bit操作,离线输入,主机读从机。

        Coils (%QX100.0):bit操作,线圈状态,主机写从机。

        Input Registers (%IW100):16位数据,输入寄存器,主机只读从机。

        Holding Registers - Read (%IW100):16位数据,保持寄存器,主机读从机。

        Holding Registers - Write (%QW100):16位数据,保持寄存器,主机写从机。

3.COM port串口扫描不出来问题。

        串口比如 /dev/ttySTM2 有驱动但是在添加设备时扫描不出来。可以修改网页代码手动添加设备名就行了。 

        网页html源码在webserver.py中,

        3.1 先修改add-modbus-device 页面, 1133行,把 设备扫描屏蔽,手动添加串口名。

         3.2 修改modbus-edit-device编辑页面,1271行,也是一样,直接屏蔽,手动添加。

4. 添加地址。 

        如下:添加 QX100.0 主机bit写从机,IX100.0 主机bit读从机。

        IW100 主机16bit只读从机,QW100 主机16bit写从机,QW101主机16bit写从机。

 5. 添加代码测试程序。

if tcpGet = 1 then
  tcpGet := 0;
  
  ledSet := 1;
  gpioSet :=1;
  
  dacSet := 1024;
  
  qx100 := 1;
  qx100_1 := 1;
  qw100 := 45;
  qw101 := 45;
  
  gpioRead0 := gpioRead;
  adcRead0 := adcRead;
  ix100st := ix100;
  iw100st := iw100;
  {
    printf("/***/\n");
    printf("/**/\n");
    printf("/*/\n");
    printf("/*********gpio10=%d, adc=%f, ix100.0=%d*******/\n",GetFbVar(GPIOREAD0),GetFbVar(ADCREAD0)/100.0,GetFbVar(IX100ST));
    printf("/*********iw100=%d*******/\n",GetFbVar(IW100ST));
    printf("/*/\n");
    printf("/**/\n");
    printf("/***/\n");
  }
elsif tcpGet = 2 then
  tcpGet := 0;
  
  ledSet := 0;
  gpioSet :=0;
  
  dacSet := 3072;
  
  qx100 := 0;
  qx100_1 := 0;
  qw100 := 78;
  qw101 := 78;
  
  gpioRead0 := gpioRead;
  adcRead0 := adcRead;
  ix100st := ix100;
  iw100st := iw100;
  {
    printf("/***/\n");
    printf("/**/\n");
    printf("/*/\n");
    printf("/*********gpio10=%d, adc=%f, ix100.0=%d*******/\n",GetFbVar(GPIOREAD0),GetFbVar(ADCREAD0)/100.0,GetFbVar(IX100ST));
    printf("/*********iw100=%d*******/\n",GetFbVar(IW100ST));
    printf("/*/\n");
    printf("/**/\n");
    printf("/***/\n");
  }
end_if;

6. modbus rtu从机设备测试。

        下载 MThings-下载 

配置串口信息