Help with MODBUS over Serial

381 views
Skip to first unread message

Jake Dempsey

unread,
Mar 24, 2014, 12:13:52 PM3/24/14
to java...@googlegroups.com
Thanks again for all the support and help so far.. its really has been awesome!

We are trying to connect to a Scanner 2000 MicroEFM device using a serial connection. I've uploaded the manual for reference.

What I am trying to figure out is how do I connect to the device in Java to the serial port and issue a MODBUS command to read a registry.  Appendix D of the document shows the decimal and hex register numbers and the data type that is returned.  The device can communicate vis RTU Modbus.  I have tried my best to read about modbus and have also downloaded a sort of modbus tester that will connect to the device and read a registry.

Unfortunately I cant figure out how to convert that knowledge into some actual code to issue the modbus command, retrieve the result, and convert to the specific data type.

Can someone help me out?
Scanner 2000 Hardware Manual copy.pdf

Nikita Kapitonov

unread,
Mar 24, 2014, 1:11:37 PM3/24/14
to java...@googlegroups.com
Hello Jake,

what's the particular problem? I had no problems implementing modbus, the only thing I've just copied from outer source was CRC calculation.

You just open the stream

    private void open() {
        try {
            String conString = "comm:" + CHANNEL + ";baudrate=" + BAUDRATE + ";blocking=off;autocts=off;autorts=off";
            cc = (CommConnection) Connector.open(conString);
            is = cc.openInputStream();
            os = cc.openOutputStream();
        } catch (IOException ex) {
        }
    }

and then write some bytes to it, firstly create an array (this is my particular example, not a standart Modbus command)
byte[] frame = new byte[]{modbusAddr, COMMAND_READ_RAM_BIT, RAM_ADR_Hi, RAM_ADR_Lo, positionOfBitInByte, 0, 0};

then you fill the last two bytes with proper CRC

then you just send it to the stream

    byte[] buffer;
    int readBytes;
    private void sendViaCommConnection(byte[] data) throws IOException {
        os.write(data);
        os.flush();
        readBytes = is.read(buffer);
    }

Also I should notice, I think I've found some Modbus lib when there was such a task, maybe you'll find it too. Now I don't remember it's name, I've just used their CRC calculating algorithm.



--
javacint group - http://www.javacint.com/
---
You received this message because you are subscribed to the Google Groups "Cinterion Java enabled chips support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javacint+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joschka Thurner

unread,
Mar 24, 2014, 3:05:51 PM3/24/14
to java...@googlegroups.com
Hi Jake,

is the physical layer wired properly (sometimes devices have A/B switched for no good reason, and you might need bias/terminating resistors)? Do you have a ready-made TC65i terminal with RS485 port, are you building one yourself or are you using a rs232 converter? If you have pure RS485, chances are you will need to drive some control pins for sending and receiving mode. It can be helpfull to do some first testing with a rs485/rs232 converter connected to a PC and some simple modbus test tool (e.g. qmodmaster).
Message has been deleted

Jake Dempsey

unread,
Mar 27, 2014, 11:08:56 AM3/27/14
to java...@googlegroups.com, nikita.k...@teplomonitor.ru
The issue I am having right now it just making a simple connection to the device.  It keeps throwing a javacall_serial_open: couldnt open port.

However when I look at teh Simply Modbus tester I have I can connect to it just fine on COM3 and issue a command using the tool.

The device is connect to my machine at the moment using a serial to usb adapter and when I run my sample midlet app on the emulator it shows COM3 in the microedition.comports property but I just can get it to connect.  Im just trying a simple:

(CommConnection)Connector.open("comm:COM3;baudrate=9600")

that throws the couldnt open port exception.

Jake Dempsey

unread,
Mar 27, 2014, 11:30:39 AM3/27/14
to java...@googlegroups.com, nikita.k...@teplomonitor.ru
Ok I think i have the connection issue resolved.. now trying to figure out how to 'send' a message to the serial connection.

Nikita Kapitonov

unread,
Mar 27, 2014, 11:30:38 AM3/27/14
to java...@googlegroups.com
Hello Jake!

It must be some kind of mistake. As I know, there is no COM3, only COM0 and COM1 in Cinterion chips.

Jake Dempsey

unread,
Mar 27, 2014, 11:37:15 AM3/27/14
to java...@googlegroups.com, nikita.k...@teplomonitor.ru
Nikita, right now I'm just trying through a j2me emulator.. once I get that working I will move to using the serial connection for the tc65i.

I still can not figure out how to create the proper message to send over the connection.

For example I am trying to read the first register being 48453 and read only 1 register. It is a 16bit register.  The modbus command my test tool sends is:

01 03 02 42 8B C8 83

I dont understand how to convert that message into the byte array that will get sent.. Sorry, Im really new at this.

Jake Dempsey

unread,
Mar 27, 2014, 11:42:59 AM3/27/14
to java...@googlegroups.com, nikita.k...@teplomonitor.ru
Ugh sorry.. The Modbus request being sent is: 01 03 21 04 00 01 CF F7

Nikita Kapitonov

unread,
Mar 27, 2014, 12:35:03 PM3/27/14
to java...@googlegroups.com
You cannot test hardware-specific features on emulator, maybe serial connection don't work on it.
Actually I would rather not test anything on emulator, better use on-chip debugger or even 'println("smth")' after every second line.

I thought I've explained that in detail before:

and then write some bytes to it, firstly create an array (this is my particular example, not a standart Modbus command)
byte[] frame = new byte[]{modbusAddr, COMMAND_READ_RAM_BIT, RAM_ADR_Hi, RAM_ADR_Lo, positionOfBitInByte, 0, 0};

then you fill the last two bytes with proper CRC

So you can just make an array of your bytes, what's the problem? http://lmgtfy.com/?q=java+how+to+initialize+a+byte+array&l=1
byte[] frame = new byte[] {(byte) 0x01, (byte) 0x03, (byte) 0x21, (byte) 0x04, (byte) 0x00, (byte) 0x01, (byte) 0xCF, (byte) 0xF7};

then follow the remaining procedure steps.

Jake Dempsey

unread,
Mar 27, 2014, 3:12:38 PM3/27/14
to java...@googlegroups.com, nikita.k...@teplomonitor.ru
Nikita,

Thanks for the help, we were able to make a connection, issue a modbus command, and get the bytes back and convert to the float... thanks!

Jake

dug patel

unread,
May 20, 2014, 6:46:43 AM5/20/14
to java...@googlegroups.com, nikita.k...@teplomonitor.ru
Hello Jake and Nikita,

Thank you in advance.

I have work on ESH6 module and I am try to serial communication with ezmeter with modbus protocol.

I write below code to connect and send modbus request but i don't know what is wrong m getting always response byte 0. meter connected on ASC1.


   String strCOM = "comm:COM1;baudrate=9600;bitsperchar=8;stopbits=1;parity=even;blocking=on;autocts=off;autorts=off";
   commConn = (CommConnection)Connector.open(strCOM);
   inStream  = commConn.openInputStream();
   outStream = commConn.openOutputStream();

 I have create one modbus request byte array with crc
 byte[] frame = new byte[] {(byte) 0x01, (byte) 0x03, (byte) 0x03, (byte) 0xE8, (byte) 0x00, (byte) 0x02, (byte) 0xE5, (byte) 0xBB};

     outStream.write(frame1);
     outStream.flush();

    int readBytes = inStream.read(buffer);
    System.out.println("Read Integer : "+readBytes");

I always get 0 byte in response.
Please help me what is wrong in code.

Thanks

Nikita Kapitonov

unread,
Jun 23, 2014, 11:08:31 AM6/23/14
to dug patel, java...@googlegroups.com
Please write to the community, maybe the question is interesting for somebody else, or some other person will answer too.

You don't have to do anything special to use "even" parity, your example is ok.

Some strange things though:

 byte[] frame = new byte[] {(byte) 0x01, (byte) 0x03, (byte) 0x03, (byte) 0xE8, (byte) 0x00, (byte) 0x02, (byte) 0xE5, (byte) 0xBB}; //You have incorrect checksum here, you can check it for example at http://www.lammertbies.nl/comm/info/crc-calculation.html

     outStream.write(frame1); // frame1, not frame - different variables



2014-06-23 9:32 GMT+04:00 dug patel <dugpat...@gmail.com>:
Hello Nikita,



Still m not reading the data from meter.

Can you tell me if parity is even then i need to write more code for read MODBUS data?

I have write below code. but m not get response from meter.


   String strCOM = "comm:COM1;baudrate=9600;
bitsperchar=8;stopbits=1;parity=even;blocking=on;autocts=off;autorts=off";
   commConn = (CommConnection)Connector.open(strCOM);
   inStream  = commConn.openInputStream();
   outStream = commConn.openOutputStream();

 I have create one modbus request byte array with crc
 byte[] frame = new byte[] {(byte) 0x01, (byte) 0x03, (byte) 0x03, (byte) 0xE8, (byte) 0x00, (byte) 0x02, (byte) 0xE5, (byte) 0xBB};

     outStream.write(frame1);
     outStream.flush();

    int readBytes = inStream.read(buffer);
    System.out.println("Read Integer : "+readBytes");

Please help me M knew in modbus protocol.

Thanks in advance.

Regards,
Dug Patel






On Thu, May 22, 2014 at 12:50 PM, dug patel <dugpat...@gmail.com> wrote:
Hello Nikita,

Thanks for replay,

yes, Parity is "Even", I seen on meter label.
On label baudrate : 9600 and parity is : Even

Thanks


On Wed, May 21, 2014 at 6:05 PM, Nikita Kapitonov <nikita.k...@teplomonitor.ru> wrote:
Hello Dug!

>> ;parity=even

Are you sure you have "even" parity?
Reply all
Reply to author
Forward
0 new messages