import import import import import import net.wimpi.modbus.ModbusCoupler; net.wimpi.modbus.io.ModbusSerialTransaction; net.wimpi.modbus.msg.ReadInputRegistersRequest; net.wimpi.modbus.msg.ReadInputRegistersResponse; net.wimpi.modbus.net.SerialConnection; net.wimpi.modbus.util.SerialParameters; public class SerialAITest2 { public static void main(String[] args) { //if (args.length < 4) { // System.out.println("not enough args"); // System.exit(1); //}else{ try { System.out.println("hello"); /* The important instances of the classes mentioned before */ SerialConnection con = null; //the connection ModbusSerialTransaction trans = null; //the transaction ReadInputRegistersRequest req = null; //the request ReadInputRegistersResponse res = null; //the response /* Variables for storing the parameters */ String portname= "COM1"; //the name of the serial port to be used int unitid = 1; //the unit identifier we will be talking to int ref = 1259; //the reference, where to start reading from int count = 1; //the count of IR's to read int repeat = 1; //a loop for repeating the transaction boolean isopen = false; // 2. Set master identifier //ModbusCoupler.createModbusCoupler(null); //ModbusCoupler.getReference().setMaster(master); I added this in //ModbusCoupler.getReference().setMaster(true); // ModbusCoupler.getReference().setUnitID(1); 3. Setup serial parameters SerialParameters params = new SerialParameters(); params.setPortName("COM1"); params.setBaudRate(9600); params.setDatabits(8); params.setParity("None"); params.setStopbits(1); params.setEncoding("RTU"); params.setEcho(false); System.setProperty("net.wimpi.modbus.debug", "true"); 4. Open the connection con = new SerialConnection(params); System.out.println("Connection..." + con.toString()); con.open(); isopen = con.isOpen(); System.out.println("Serial port status..." + isopen); // 5. Prepare a request req = new ReadInputRegistersRequest(ref, count); // // req.setUnitID(unitid); req.setHeadless(); // 6. Prepare a transaction trans = new ModbusSerialTransaction(con); trans.setRequest(req); 7. Execute the transaction repeat times int k = 0; do { trans.execute(); res = (ReadInputRegistersResponse) trans.getResponse(); for (int n = 0; n < res.getWordCount(); n++) { System.out.println("Word " + n + "=" + res.getRegisterValue(n)); } k++; } while (k < repeat); 8. Close the connection con.close(); } catch (Exception ex) { ex.printStackTrace(); } //}//else }//main }//class SerialAITest // //
Please download to view
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
...

teste

by tiagorrcb

on

Report

Category:

Documents

Download: 0

Comment: 0

41

views

Comments

Description

Download teste

Transcript

import import import import import import net.wimpi.modbus.ModbusCoupler; net.wimpi.modbus.io.ModbusSerialTransaction; net.wimpi.modbus.msg.ReadInputRegistersRequest; net.wimpi.modbus.msg.ReadInputRegistersResponse; net.wimpi.modbus.net.SerialConnection; net.wimpi.modbus.util.SerialParameters; public class SerialAITest2 { public static void main(String[] args) { //if (args.length < 4) { // System.out.println("not enough args"); // System.exit(1); //}else{ try { System.out.println("hello"); /* The important instances of the classes mentioned before */ SerialConnection con = null; //the connection ModbusSerialTransaction trans = null; //the transaction ReadInputRegistersRequest req = null; //the request ReadInputRegistersResponse res = null; //the response /* Variables for storing the parameters */ String portname= "COM1"; //the name of the serial port to be used int unitid = 1; //the unit identifier we will be talking to int ref = 1259; //the reference, where to start reading from int count = 1; //the count of IR's to read int repeat = 1; //a loop for repeating the transaction boolean isopen = false; // 2. Set master identifier //ModbusCoupler.createModbusCoupler(null); //ModbusCoupler.getReference().setMaster(master); I added this in //ModbusCoupler.getReference().setMaster(true); // ModbusCoupler.getReference().setUnitID(1); 3. Setup serial parameters SerialParameters params = new SerialParameters(); params.setPortName("COM1"); params.setBaudRate(9600); params.setDatabits(8); params.setParity("None"); params.setStopbits(1); params.setEncoding("RTU"); params.setEcho(false); System.setProperty("net.wimpi.modbus.debug", "true"); 4. Open the connection con = new SerialConnection(params); System.out.println("Connection..." + con.toString()); con.open(); isopen = con.isOpen(); System.out.println("Serial port status..." + isopen); // 5. Prepare a request req = new ReadInputRegistersRequest(ref, count); // // req.setUnitID(unitid); req.setHeadless(); // 6. Prepare a transaction trans = new ModbusSerialTransaction(con); trans.setRequest(req); 7. Execute the transaction repeat times int k = 0; do { trans.execute(); res = (ReadInputRegistersResponse) trans.getResponse(); for (int n = 0; n < res.getWordCount(); n++) { System.out.println("Word " + n + "=" + res.getRegisterValue(n)); } k++; } while (k < repeat); 8. Close the connection con.close(); } catch (Exception ex) { ex.printStackTrace(); } //}//else }//main }//class SerialAITest // //
Fly UP