RS485 Modbus RTU using PC com port in Twincat 3 issues

Kroket

Member
Join Date
Apr 2024
Location
NL
Posts
4
I'm trying to control a device via MODBUS RTU and the ModbusRtuMasterV2_PcCOM in Twincat 3. I've configured a device with the right com port and KL6xx1 mode emulation. However I can't get the device to react to a registers write. While the command: 01 10 00 F6 00 02 04 01 01 ff 6c bd works within a MODBUS master program. I'm using the following code withing Twincat to try to send the same command. Does anyone have suggestions on how to get this working? Is it something I need to configure or is it something wrong in the code below?

Code:
PROGRAM MAIN
VAR
    MotorMaster: ModbusRtuMasterV2_PcCOM;
    Mb_ID: BYTE := 1;
    MbRead_Quanti: WORD;
    MbRead_Addr: WORD;
    Data_Read: ARRAY[1..10] OF INT;
    Enable_Read: BOOL;
    Data_Write: ARRAY[0..3] OF BYTE := [16#01, 16#01, 16#FF, 16#FF];
    Enable_Write: BOOL;
    MbError: BOOL;
    MbErrorID: Tc2_ModbusRTU.MODBUS_ERRORS;
    MbWrite_Quanti: WORD := 2;
    MbWrite_Addr: BYTE := 246;
END_VAR

Code:
MotorMaster.WriteRegs(
    UnitID:= Mb_ID,
    Quantity:= MbWrite_Quanti,
    MBAddr:= MbWrite_Addr,
    cbLength:= SIZEOF(Data_Write),
    pMemoryAddr:= ADR(Data_Write),
    Execute:=Enable_Write,
    Timeout:= T#1S,
    Error=> MbError,
    ErrorId=> MbErrorID,
    cbRead=>
);
 
the Execute parameter needs to have a rising edge; are you providing that?
Yes, if I toggle Enable_Write it now gives me a MODBUSERROR_CRC, this is interesting since I don't even see any communication when I measure with my scope or look at the indicition lights of my usb to RS485 converter. I'm sure the COM port, Baudrate, stopbits, parity, and databits are set properly.

I did link the InData, OutData, SerStatus, and SetCrtl and the COM port is busy because I can't access it from other programs while TwinCat is running.
 
can you connect the COM port where the ModbusRtuMasterV2_PcCOM block is writing data to another PC's COM port and look at the data being sent? If the other PC could run a Modbus Slave, then you should see the incoming data.
 
can you connect the COM port where the ModbusRtuMasterV2_PcCOM block is writing data to another PC's COM port and look at the data being sent? If the other PC could run a Modbus Slave, then you should see the incoming data.
I had to arrange another USB converter, but I've tried your suggestions. I can't see any data being transmitted. I've also tried using the other COM device now as master connected to Twincat but no luck either. While if I use one as master and the other in slave using a different program I do see the transmission to my motor.
 
I had to arrange another USB converter, but I've tried your suggestions. I can't see any data being transmitted. I've also tried using the other COM device now as master connected to Twincat but no luck either. While if I use one as master and the other in slave using a different program I do see the transmission to my motor.
Modbus RTU is a one-master, one-or-more-slaves network. I suspect the motor is the slave, so when you have a PC program that can act as Modbus master (there are many available only, some free, some free with pay for extra features, some pay only, and it is not difficult to write one from scratch), and the wiring is correct, then you should be able to read data from and write data to the motor Modbus slave; it sounds like that is happening now. You will not be able to connect the PC Modbus master to the Twincat Modbus master: masters do not communicate with each other, only with slaves. There may be a ModbusRtuSlaveV2_PcCOM instruction/function in Twincat, but you would only use that in the current situation to diagnose serial pinout issues.

Hopefully the Modbus master program on the PC also displays the data transmitted (usually "Tx") and received (usually ("Rx").

Once you have that working, try connecting a PC program that can act as a Modbus slave, and connect that to the Twincat Modbus master; even if you don't configure the PC Modbus slave to have the data the Twincat Modbus master is requesting, you will see the bytes of the request from Twincate and can compare them to the bytes sent by the PC Modbus master. That comparison is the diagnostic for the next step; if you don't know what to do with it, then post the results here (PC Modbus master to motor Modbus slave; Twincat Modbus master to PC Modbus slave).

Also, is this application using Modbus RTU over RS-232 or over RS-485?
 
Thanks, I am trying to set up a motor over Modbus RTU RS-485 with the motor as slave with adress 1.

When I run a mosbus master program (com4) I am able to control the motor, and when I am running a modbus slave program (com2) I am able to monitor that communication.

Once I try to run this setup and replace the Modbus master program with Twincat I can's seem to get communication. The motor does'nt start and I can't see any data on the modbus slave program.

As you suggested I tried to connect Twincat to a modbus slave program on my PC, but I've the same result here. No communication. This is with the motor physically disconnected.
 

Attachments

  • Screenshot 2024-04-22 192500.png
    Screenshot 2024-04-22 192500.png
    111.4 KB · Views: 16
  • Twincat master to modbus slave.png
    Twincat master to modbus slave.png
    203.5 KB · Views: 16
  • Twincat as master - motor slave 1 - com2 as slave 2 V2.png
    Twincat as master - motor slave 1 - com2 as slave 2 V2.png
    183.5 KB · Views: 13

Similar Topics

I am trying to figure out some communication specifics for a Modbus RTU network that I will be integrating with and could use some help to make...
Replies
9
Views
2,117
I'm getting no response from a device I'm trying to communicate to with a Beckhoff IPC. I'm not sure why it's not working. Attached are pictures...
Replies
0
Views
644
HI guys: I am attaching several diagram from several sources, it seems to be that there is no such a wiring standard for modbus RS485 RTU. In some...
Replies
24
Views
4,609
So I'm working on an old project I did several years ago. It has 12 nodes on a modbus RTU network. I'm going back for an expansion. I'm going to...
Replies
5
Views
1,914
Need a sanity check here. It is possible to have redundant Modbus RTU master, but only one master is actively polling at a time? The background...
Replies
4
Views
2,575
Back
Top Bottom