Results 1 to 19 of 19
  1. #1
    aceman is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Question Java or not to java, that is the question.

    Hello fellow java people. I am starting a project that needs to implement the modbus protocol over a serial line(rs-485). I have so far installed RXTX and it works fine in java. But as far as I can tell most methods to read a stream are buffered, and they block until data is available. I don't think this will work correctly, because the protocol has certain timing requirements. I think I need a lower level access to the data coming off the serial line. For example, the protocol defines a message frame as no more that 256 bytes. Each byte MUST arrive no later than 750 Microseconds apart (@ 38.4K baud) . The protocol specs say if a byte arrives later that that, the frame is considered corrupt, and should be discarded. An End of Frame is signaled by a 1750 Microsecond pause, then frame is then considered good and can be sent for further processing. Since im new to java, I don't know if Java is a good choice for this low level timing and communications part of my project.
    So for PLAN B, I was thinking of doing this low level stuff in C as a deamon and use a socket or a pipe to connect to my java program.

    What direction should I choose?

    Thanks in advance
    Aceman

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Java or not to java, that is the question.

    I'm not sure you're interpreting the timing requirements correctly.
    Get in the habit of using standard Java naming conventions!

  3. #3
    aceman is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default Re: Java or not to java, that is the question.

    Quote Originally Posted by kjkrum View Post
    I'm not sure you're interpreting the timing requirements correctly.
    I am, I have all the documentation for modbus, the requirements are clearly described. My question was a java question, not a modbus question.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java or not to java, that is the question.

    Quote Originally Posted by aceman View Post
    Each byte MUST arrive no later than 750 Microseconds apart (@ 38.4K baud) . The protocol specs say if a byte arrives later that that, the frame is considered corrupt, and should be discarded. An End of Frame is signaled by a 1750 Microsecond pause, then frame is then considered good and can be sent for further processing.
    I'm afraid there is a timing issue: while the software (Java) offers even nano second timing accuracy, those lousy MS Windows clock drivers don't even offer milli second accuracy (+- 5msecs is the best they can do). This is not a Java issue. it's the way the OS implemented the clock driver. MS Windows is a show stopper here but Linux can't do much better (1 milli second accuracy). A proper modbus implementation is quite demanding on the hardware; again, this is not a Java issue; you'll have the same issues if you were using C or even assembly language.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    aceman is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default Re: Java or not to java, that is the question.

    Linux can't do much better (1 milli second accuracy).
    A brief look a high res timers in Linux seem to give 1ns accuracy. I already wrote code for the slave devices running on atmel microcontrollers. There you have access to powerful timers with interrupts which makes this very easy.
    At the end of the day do I have to be so fussy about frame timings? No. I can already read and write to my slaves. But if I am to adhere to the protocol specs, thees timings are required.
    So I am going to mull it over.. Any insight as how to implement timers in Java would be appreciated however.

    Thanks for your time.
    Ace

  6. #6
    aceman is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default Re: Java or not to java, that is the question.

    Actually I have to take back some of what I said in my last reply. The way to know the frame ends is after 1.75 Ms delay of no data. However InputStream.read blocks if there is no more data. (This is a binary stream). So I am not sure how to get this to work. Is there a way to do non blocking reads from a stream?

    Ace

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Java or not to java, that is the question.

    You can do non-blocking I/O with the NIO library (java.nio (Java Platform SE 7 )). If the OS provides adequate timing precision, you might be able to achieve what you want on one of the various realtime JVMs. I can't recommend any particular one.
    Get in the habit of using standard Java naming conventions!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java or not to java, that is the question.

    Quote Originally Posted by aceman View Post
    A brief look a high res timers in Linux seem to give 1ns accuracy.
    Take a better look; on most kernels time is measured in 'jiffies' which is 0.001s; only hardware timers (RTC) can measure time in smaller granularities but they are very slowly consulted (by software) so you can forget about a 1ns accuracy; if you don't believe me, check Google; it knows all about high resolution timers ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,776
    Rep Power
    5

    Default Re: Java or not to java, that is the question.

    Bam, I open my browser and I learn something new already. Never knew they were called jiffies.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java or not to java, that is the question.

    Quote Originally Posted by gimbal2 View Post
    Bam, I open my browser and I learn something new already. Never knew they were called jiffies.
    Yep, I wanted to make it a happy Monday for you all, so I decided to throw in a couple of jiffies ;-) The OP is too optimistic about high resolution timing; PCs can only do 1ms at best (and only recently) and that darn modbus demands a lot from the hardware (it's a silly, old fashioned technology imho, because all it does is read data over serial lines and usb superceded it by huge steps).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    aceman is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default Re: Java or not to java, that is the question.

    Quote Originally Posted by JosAH View Post
    (it's a silly, old fashioned technology imho, because all it does is read data over serial lines and usb superceded it by huge steps).

    kind regards,

    Jos
    Yes, I am aware modbus is very old technology. But just because something is old does not make it useless. (Ask my father ) USB is far better but It can't run over 1000 feet with multi-drop like modbus/rs-485 can. Ethernet can of course but I chose against it. This is all apart of a home automation/sensor project. The devices need very little bandwidth. I want to sense: Oil levels in my oil tanks. Electricty usage and load, interface to my home made weather station, water usage, thermostat setting, etc. I did my homework and the main goal is to keeps costs down. I want to spend no more than $10-$20 dollars total for each slave device. I considered other options like ARM cortex with wifly or ethernet, that's overkill for most of my slaves. Using Xbee with my controllers, that drives up the cost. Using a Ethernet module with my controllers, that too drives up the cost and hogs what little resources the microcontroller has to implement the TCP/IP stack. So I felt this was a simple solution. rs-485 tranceviers are about $3.00, are differential, they support long runs with multidrop. I chose java so I can use the jWebsocket server to connect to browser based clients to view and control the devices in (near) real time.
    I found something called libmodbus that works on Linux, and will take a look at the sources to see how he handles the low level communication side of things.

    Thanks to everyone offering help and ideas. I am sure I will have more questions once I start writing the java code in earnest.
    Best Regards,
    Aceman

  12. #12
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Java or not to java, that is the question.

    You could always try it on a PC. Even if the specs demand very strict timing, it would not be surprising if a looser implementation still worked. Especially if occasional errors are acceptable.
    Get in the habit of using standard Java naming conventions!

  13. #13
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: Java or not to java, that is the question.

    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java or not to java, that is the question.

    I don't fully understand why it should be a modbus implementation; why not have the 'master' ask the 'slaves' for the information periodically over that rs/485 wire? A simple softare protocol would be fine ... if no answer comes in from one of the 'slaves' you could register the fact and ask again a bit later.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    aceman is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default Re: Java or not to java, that is the question.

    Quote Originally Posted by SurfMan View Post

    Thanks! Free beer for you!

  16. #16
    aceman is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default Re: Java or not to java, that is the question.

    Quote Originally Posted by kjkrum View Post
    You could always try it on a PC. Even if the specs demand very strict timing, it would not be surprising if a looser implementation still worked. Especially if occasional errors are acceptable.
    Agreed. I took a look at libmodbus, and he uses very relaxed timings (.5 seconds). So I guess there is no need to follow the specs to the letter. At the end of the day each frame has a CRC so if it comes in bad it will get caught by the CRC check.

  17. #17
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: Java or not to java, that is the question.

    Quote Originally Posted by aceman View Post
    Thanks! Free beer for you!
    Hey, it's close community here. I expect you to give drinks to the entire house! :D
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  18. #18
    aceman is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default Re: Java or not to java, that is the question.

    Quote Originally Posted by SurfMan View Post
    I must thank you again. I got jamod working. You saved me a great deal of work! Even in jamod they mention the RTU mode works on a best effort basis, because of the timings I talked about - but it seems to be working fine for me so far. I can always implement ASCII mode transfer if I have to.. Now I can move on to the logic part and getting things to work as a jWebsocket plugin. Then the javascript client side.. yadda yadda. O my what have I gotten myself into!

    It didn't even occur to me to search for a modbus java implementation I figured who other than me would want to support such an old protocol in a new language.

    Thanks again to you and all who helped me.
    Ace

  19. #19
    aceman is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default Re: Java or not to java, that is the question.

    Quote Originally Posted by SurfMan View Post
    Hey, it's close community here. I expect you to give drinks to the entire house! :D
    Well so far ,since I got jamod working things are progressing. So
    FREE DRINKS FOR EVERYONE!

Similar Threads

  1. Java Question
    By Pinky4Free in forum Advanced Java
    Replies: 8
    Last Post: 12-29-2012, 09:21 AM
  2. Replies: 2
    Last Post: 03-20-2012, 01:28 PM
  3. Java question?
    By Zeronitic in forum New To Java
    Replies: 1
    Last Post: 02-21-2012, 08:35 PM
  4. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 04:01 PM
  5. java question
    By joe98 in forum New To Java
    Replies: 7
    Last Post: 03-23-2011, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •