[ 
https://issues.apache.org/jira/browse/PLC4X-300?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17357324#comment-17357324
 ] 

Christofer Dutz commented on PLC4X-300:
---------------------------------------

Hi Juan,

In general there are multiple protocols: Modbus TCP and Modbus RTU (In reality 
there will probably be more, but these are the general families of protocols) 
... They are almost identical from the payload and the logic side. It's only 
that TCP and RTU have slightly different container packets. Both are already 
modeled in PLC4X. We only need to put together the parts. I intentionally 
didn't jump in and do it as I think this is an interesting thing to be 
implemented by newcomers. The project greatly benefits from more people able to 
write drivers.

In general there is also a 3rd option ... Modbus RTU over TCP which uses the 
Modbus RTU packet format but replaces the serial connection with a TCP 
connection. This is pretty useful when making a device that only supports 
Modbus RTU online-capable by adding a serial to tcp bridge.

So In general we would have to rename the "modbus" driver "modbus-tcp" and 
create a new one (by copying the tcp versoin) and naming that "modbus-rtu" then 
we could do all the plumbing in the new driver.

If your company however is in immediate need for such a driver and you aren't 
willing or able to become active yourself or to wait till it's implemented by 
someone else, you can always reach out to some folks on the list (I'm currently 
working on PROFINET the same way but might be able to do this as soon as that's 
done) and have them implement it for you as a paid gig.

Chris

> Plc4x support for RTU/Serial communication for modbus protocol.
> ---------------------------------------------------------------
>
>                 Key: PLC4X-300
>                 URL: https://issues.apache.org/jira/browse/PLC4X-300
>             Project: Apache PLC4X
>          Issue Type: Bug
>          Components: Driver-Modbus, examples
>    Affects Versions: 0.8.0
>            Reporter: Purushotham YB
>            Priority: Major
>
> Hi,
>    I am using plc4x for one of our projects to develop a module which uses 
> plc4x to establish communication with a sensor device using modbus RTU/Serial 
> communication.
> I am able to communicate with the device with TCP transport but not able to 
> do with RTU/Serial communication.
> I tried to look for some example/documentation on how to use RTU/Serial 
> communication in plc4x but could not find any information in the website or 
> the web.
> Also looking at the documentation at 
> [https://plc4x.apache.org/users/protocols/modbus.html] it is not clear 
> whether modbus supports serial transport as it only lists tcp and udp.
>  
> |Compatible Transports:| * {{tcp}} (Default Port: 502)
>  * {{udp}} (Default Port: 502)|
> Kindly request to clarify if serial is supported in modbus? if so could you 
> please point me to an example/documentation which shall be used to understand 
> how to use it for serial communication.
> Anyways I tried to use the library/driver plc4j-transport-serial  
> [https://plc4x.apache.org/users/transports/serial.html]
> to communicate with device in which the connection is established but it 
> fails to read the data with following WANING.
>  
> {{2021-06-03-18:13:51.814 [nioEventLoopGroup-2-1] WARN  
> io.netty.channel.nio.NioEventLoop - Selector.select() returned prematurely 
> 512 times in a row; rebuilding Selector 
> org.apache.plc4x.java.transport.serial.SerialPollingSelector@28ecdc0d.}}
> {{2021-06-03-18:13:59.630 [nioEventLoopGroup-2-1] WARN  
> io.netty.channel.nio.NioEventLoop - Selector.select() returned prematurely 
> 512 times in a row; rebuilding Selector 
> org.apache.plc4x.java.transport.serial.SerialPollingSelector@11c9a1fa.}}
> {{My Sample code is as follows.}}
>  
> {{ private void plcRtuReader() {}}
> {{ // unit-identifier=1&}}
> {{ String connectionString =}}
> {{ "modbus:serial://COM5?unit-identifier=1&baudRate=19200&stopBits=" + 
> SerialPort.ONE_STOP_BIT}}
> {{ + "&parityBits="}}
> {{ + SerialPort.NO_PARITY + "&dataBits=8";}}
> {{ System.out.println("URL:" + connectionString);}}
> {{ try (PlcConnection plcConnection = new 
> PlcDriverManager().getConnection(connectionString)) {}}
> {{ if (!plcConnection.getMetadata().canRead()) {}}
> {{ System.out.println("This connection doesn't support reading.");}}
> {{ return;}}
> {{ }}}
> {{ PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();}}
> {{ builder.addItem("value-2", "input-register:1[2]");}}
> {{PlcReadRequest readRequest = builder.build();}}
> {{// CompletableFuture<? extends PlcReadResponse> asyncResponse = 
> readRequest.execute();}}
> {{ PlcReadResponse response = readRequest.execute().get();}}
> {{ for (String fieldName : response.getFieldNames()) {}}
> {{ if (response.getResponseCode(fieldName) == PlcResponseCode.OK) {}}
> {{ int numValues = response.getNumberOfValues(fieldName);}}
> {{ // If it's just one element, output just one single line.}}
> {{ if (numValues == 1) {}}
> {{ System.out.println("Value[" + fieldName + "]: " + 
> response.getObject(fieldName));}}
> {{ }}}
> {{ // If it's more than one element, output each in a single row.}}
> {{ else {}}
> {{ System.out.println("Value[" + fieldName + "]:");}}
> {{ for (int i = 0; i < numValues; i++) {}}
> {{ System.out.println(" - " + response.getObject(fieldName, i));}}
> {{ }}}
> {{ }}}
> {{ }}}
> {{ // Something went wrong, to output an error message instead.}}
> {{ else {}}
> {{ System.out.println(}}
> {{ "Error[" + fieldName + "]: " + 
> response.getResponseCode(fieldName).name());}}
> {{ }}}
> {{ }}}
> {{ System.exit(0);}}
> {{ } catch (PlcConnectionException e) {}}
> {{ e.printStackTrace();}}
> {{ } catch (Exception e) {}}
> {{ e.printStackTrace();}}
> {{ }}}
> {{ }}}
> Thanks a lot for your help.
> Regards,
> Purushotham



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to