public void receiveData( String ip, String paramName, String paramNo, int paramBote, int paramLength, int parmParity, int parmStopBit, int parmDelay) { try { try { m = new Mongo(ip, 27017); db = m.getDB(paramName); // db.authenticate("test", "123".toCharArray()); } catch (UnknownHostException ex) { ex.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } final DBCollection coll = db.getCollection("DATAIN"); final DBCollection collout = db.getCollection("DATAOUT"); DBCollection meta = db.getCollection("META"); // 记录数据字段 final Map map1 = new HashMap(); final Map map2 = new HashMap(); Map map00 = new HashMap(); Map map01 = new HashMap(); Map map02 = new HashMap(); Map map03 = new HashMap(); final Map m_ai_max = new HashMap(); final Map m_ai_min = new HashMap(); final Map m_ao_max = new HashMap(); final Map m_ao_min = new HashMap(); DBCursor cursor = meta.find(); while (cursor.hasNext()) { // 记录数据类型 DBObject dbo = cursor.next(); String name = dbo.get("_id").toString(); String type = dbo.get("type").toString(); String addr = dbo.get("addr").toString(); Double max = (Double) dbo.get("max"); Double min = (Double) dbo.get("min"); if (type.equals("AI")) { map00.put(name, addr); m_ai_max.put(name, max); m_ai_min.put(name, min); } if (type.equals("DI")) { map01.put(name, addr); } if (type.equals("AO")) { map02.put(name, addr); } if (type.equals("DO")) { map03.put(name, addr); } } map1.put("AI", map00); map1.put("DI", map01); map2.put("AO", map02); map2.put("DO", map03); SerialParameters params = new SerialParameters(); params.setCommPortId(paramNo); params.setBaudRate(paramBote); params.setDataBits(paramLength); params.setParity(parmParity); params.setStopBits(parmStopBit); ModbusFactory modbusFactory = new ModbusFactory(); slave = modbusFactory.createRtuSlave(params); slave.addProcessImage(getModscanProcessImage(1, map00, coll)); slave.addProcessImage(getModscanProcessImage(2, map01, coll)); slave.addProcessImage(getModscanProcessImage(3, map02, collout)); slave.addProcessImage(getModscanProcessImage(4, map03, collout)); new Thread( new Runnable() { public void run() { try { slave.start(); // JOptionPane.showMessageDialog(f, "通讯连接成功!"); } catch (ModbusInitException e) { e.printStackTrace(); } } }) .start(); ScheduledExecutorService timerRead = Executors.newScheduledThreadPool(1); timerRead.scheduleAtFixedRate( new Runnable() { public void run() { new TaskRead(map1, m_ai_max, m_ai_min, coll, slave); } }, 500, parmDelay, TimeUnit.MILLISECONDS); ScheduledExecutorService timerWrite = Executors.newScheduledThreadPool(2); timerWrite.scheduleAtFixedRate( new Runnable() { public void run() { new TaskWrite(map2, m_ao_max, m_ao_min, collout, slave); } }, 500, parmDelay, TimeUnit.MILLISECONDS); } catch (Exception ex) { } }
public static void main(String[] args) throws Exception { SerialParameters serialParameters = new SerialParameters(); serialParameters.setCommPortId("COM1"); serialParameters.setPortOwnerName("Numb nuts"); serialParameters.setBaudRate(9600); IpParameters ipParameters = new IpParameters(); ipParameters.setHost("localhost"); ModbusFactory modbusFactory = new ModbusFactory(); // ModbusMaster master = modbusFactory.createRtuMaster(serialParameters, false); // ModbusMaster master = modbusFactory.createAsciiMaster(serialParameters); ModbusMaster master = modbusFactory.createTcpMaster(ipParameters, false); // ModbusMaster master = modbusFactory.createUdpMaster(ipParameters); try { master.init(); int slaveId = 31; // readCoilTest(master, slaveId, 98, 200); // readCoilTest(master, slaveId, 99, 200); // readDiscreteInputTest(master, slaveId, 100, 2); // readDiscreteInputTest(master, slaveId, 449, 72); // readHoldingRegistersTest(master, slaveId, 9, 125); // readHoldingRegistersTest(master, slaveId, 9, 120); // readInputRegistersTest(master, slaveId, 0, 1); // readInputRegistersTest(master, slaveId, 14, 8); // writeCoilTest(master, slaveId, 1, true); // writeCoilTest(master, slaveId, 110, true); // writeRegisterTest(master, slaveId, 1, 1); // writeRegisterTest(master, slaveId, 14, 12345); // readExceptionStatusTest(master, slaveId); // reportSlaveIdTest(master, slaveId); // writeCoilsTest(master, slaveId, 50, new boolean[] {true, false, false, true, false}); // writeCoilsTest(master, slaveId, 115, new boolean[] {true, false, false, true, false}); // writeRegistersTest(master, slaveId, 300, new short[] {1, 10, 100, 1000, 10000, // (short)65535}); // writeRegistersTest(master, slaveId, 21, new short[] {1, 10, 100, 1000, 10000, // (short)65535}); // writeMaskRegisterTest(master, slaveId, 26, 0xf2, 0x25); // readCoilTest(master, slaveId, 9, 5); // readCoilTest(master, slaveId, 10, 5); // readDiscreteInputTest(master, slaveId, 10, 6); // readDiscreteInputTest(master, slaveId, 10, 5); // readHoldingRegistersTest(master, slaveId, 9, 7); // readHoldingRegistersTest(master, slaveId, 10, 5); // readInputRegistersTest(master, slaveId, 0, 1); // readInputRegistersTest(master, slaveId, 10, 5); // writeCoilTest(master, slaveId, 8, true); // writeCoilTest(master, slaveId, 11, true); // writeRegisterTest(master, slaveId, 1, 1); // writeRegisterTest(master, slaveId, 14, 12345); // readExceptionStatusTest(master, slaveId); // reportSlaveIdTest(master, slaveId); // writeCoilsTest(master, slaveId, 11, new boolean[] {false, true, false, false, true}); // writeCoilsTest(master, slaveId, 10, new boolean[] {false, true, false, false, true}); // writeRegistersTest(master, slaveId, 11, new short[] {(short)65535, 1000, 100, 10, 1}); // writeRegistersTest(master, slaveId, 10, new short[] {(short)65535, 1000, 100, 10, 1}); // writeMaskRegisterTest(master, slaveId, 9, 0xf2, 0x25); // writeMaskRegisterTest(master, slaveId, 10, 0xf2, 0x25); // Automatic WriteMaskRegister failover test // ModbusLocator locator = new ModbusLocator(slaveId, RegisterRange.HOLDING_REGISTER, 15, // (byte)2); // System.out.println(master.getValue(locator)); // master.setValue(locator, true); // System.out.println(master.getValue(locator)); // master.setValue(locator, false); // System.out.println(master.getValue(locator)); // BatchRead<String> batch = new BatchRead<String>(); // batch.addLocator("hr1", new ModbusLocator(31, RegisterRange.HOLDING_REGISTER, 80, // DataType.TWO_BYTE_BCD)); // batch.addLocator("hr2", new ModbusLocator(31, RegisterRange.HOLDING_REGISTER, 81, // DataType.FOUR_BYTE_BCD)); // BatchResults<String> results = master.send(batch); // System.out.println(results.getValue("hr1")); // System.out.println(results.getValue("hr2")); ModbusLocator locator = new ModbusLocator( slaveId, RegisterRange.HOLDING_REGISTER, 50, DataType.EIGHT_BYTE_INT_UNSIGNED); master.setValue(locator, 10000000); System.out.println(master.getValue(locator)); } finally { master.destroy(); } }