Advertisement
Guest User

jmod

a guest
Jun 10th, 2015
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. /**
  2. * Copyright 2002-2010 jamod development team
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. **/
  16.  
  17.  
  18.  
  19. import net.wimpi.modbus.Modbus;
  20. import net.wimpi.modbus.ModbusCoupler;
  21. import net.wimpi.modbus.net.ModbusTCPListener;
  22. import net.wimpi.modbus.procimg.*;
  23. import net.wimpi.modbus.util.SerialParameters;
  24.  
  25.  
  26. /**
  27. * Class implementing a simple Modbus TCP slave.
  28. * A simple process image is available to test
  29. * functionality and behaviour of the implementation.
  30. *
  31. * @author Galen Collins
  32. * @version @version@ (@date@)
  33. */
  34. public class ModbusTcpSlave {
  35.  
  36. public static void main(String[] args) {
  37.  
  38. ModbusTCPListener listener = null;
  39. SimpleProcessImage spi = new SimpleProcessImage();
  40. int port = Modbus.DEFAULT_PORT;
  41.  
  42. if (Modbus.debug) System.out.println("jModbus Modbus TCP Slave");
  43. if (args != null && args.length >= 1) {
  44. port = Integer.parseInt(args[0]);
  45. }
  46.  
  47. try {
  48.  
  49. //1. Prepare a process image
  50. spi = new SimpleProcessImage();
  51. spi.addDigitalOut(new SimpleDigitalOut(true));
  52. spi.addDigitalOut(new SimpleDigitalOut(false));
  53. spi.addDigitalIn(new SimpleDigitalIn(false));
  54. spi.addDigitalIn(new SimpleDigitalIn(true));
  55. spi.addDigitalIn(new SimpleDigitalIn(false));
  56. spi.addDigitalIn(new SimpleDigitalIn(true));
  57. spi.addRegister(new SimpleRegister(251));
  58. spi.addInputRegister(new SimpleInputRegister(45));
  59.  
  60. //2. Create the coupler and set the slave identity
  61. ModbusCoupler.getReference().setProcessImage(spi);
  62. ModbusCoupler.getReference().setMaster(false);
  63. ModbusCoupler.getReference().setUnitID(1);
  64.  
  65. //3. Create a listener with 3 threads
  66. listener = new ModbusTCPListener(3);
  67. listener.setPort(port);
  68. listener.start();
  69.  
  70. } catch (Exception ex) {
  71. ex.printStackTrace();
  72. }
  73. }//main
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement