예제 #1
0
    public TaskRead(Map map1, Map m_max, Map m_min, DBCollection coll, ModbusSlaveSet slave) {
      try {
        //// 此处只要点击开始就能保存数据
        /// 方案:利用数据源发送单个布尔型变量(1,0)交替,链接成功,先判断是否链接成功,然后在保存数据
        synchronized (slave) {
          Map map = new HashMap();
          Set set1 = map1.entrySet();
          Iterator it1 = set1.iterator();
          while (it1.hasNext()) {
            Map.Entry<String, Map<String, String>> entry =
                (Map.Entry<String, Map<String, String>>) it1.next();
            Map<String, String> map2 = entry.getValue();
            for (Iterator it2 = map2.entrySet().iterator(); it2.hasNext(); ) {
              Map.Entry<String, String> entry2 = (Map.Entry<String, String>) it2.next();
              String name = entry2.getKey().toString();
              String paramAddr = entry2.getValue().toString();
              int fun = (int) (Double.parseDouble(paramAddr));
              if (paramAddr.substring(0, 1).equals("4")) {
                Short d4 = slave.getProcessImage(1).getInputRegister(fun % 10000);
                double dmax = (Double) m_max.get(name);
                double dmin = (Double) m_min.get(name);
                double dValue = 0;
                if (d4 >= 0) {
                  dValue = dmax * d4 / 32000;
                } else {
                  dValue = dmin * d4 / (-32000);
                }
                map.put(name, dValue);
              }
              if (paramAddr.substring(0, 1).equals("3")) {
                Short d3 = slave.getProcessImage(1).getHoldingRegister(fun % 10000);
                double dmax = (Double) m_max.get(name);
                double dmin = (Double) m_min.get(name);
                double dValue = 0;

                if (d3 >= 0) {
                  dValue = dmax * d3 / 32000;
                } else {
                  dValue = dmin * d3 / (-32000);
                }
                map.put(name, dValue);
              }
              if (paramAddr.substring(0, 1).equals("2")) {
                map.put(name, slave.getProcessImage(2).getInput(fun % 10000));
              }
              if (paramAddr.substring(0, 1).equals("1")) {
                Boolean a = slave.getProcessImage(2).getCoil(fun % 10000 - 1);
                map.put(name, a);
              }
            }
          }

          Calendar calendar = Calendar.getInstance();
          Date dd = calendar.getTime();

          BasicDBObject doc = new BasicDBObject();
          doc.put("_id", dd);

          Set set = map.entrySet();
          Iterator it = set.iterator();
          while (it.hasNext()) {
            Map.Entry<String, String> entry1 = (Map.Entry<String, String>) it.next();
            doc.put(entry1.getKey(), entry1.getValue());
          }
          coll.insert(doc);
        }
      } catch (Exception ex) {
      }
    }
예제 #2
0
    public TaskWrite(Map map1, Map m_max, Map m_min, DBCollection coll, ModbusSlaveSet slave) {

      try {
        synchronized (slave) {
          Calendar calener = Calendar.getInstance();
          Date d1 = calener.getTime();
          Date d2 = new Date(calener.getTime().getTime() - 5000);
          BasicDBObject b2 = new BasicDBObject();
          b2.put("$gte", d2);
          b2.put("$lte", d1);
          DBCursor cursor =
              coll.find(new BasicDBObject("_id", b2)).sort(new BasicDBObject("_id", -1)).limit(1);
          while (cursor.hasNext()) {
            DBObject dbo = cursor.next();
            Set set1 = map1.entrySet();
            Iterator it1 = set1.iterator();
            while (it1.hasNext()) {
              Map.Entry<String, Map<String, String>> entry =
                  (Map.Entry<String, Map<String, String>>) it1.next();
              Map<String, String> map2 = entry.getValue();
              for (Iterator it2 = map2.entrySet().iterator(); it2.hasNext(); ) {
                Map.Entry<String, String> entry2 = (Map.Entry<String, String>) it2.next();
                String name = entry2.getKey().toString();
                String paramAddr = entry2.getValue().toString();
                int fun = (int) (Double.parseDouble(paramAddr));
                if (paramAddr.substring(0, 1).equals("4")) {
                  double value = Double.parseDouble(dbo.get(name).toString());
                  double dmax = (Double) m_max.get(name);
                  double dmin = (Double) m_min.get(name);
                  double dValue = 0;
                  if (value > dmax || value < dmin) {
                    if (value >= 0) {
                      dValue = 32000 * (int) (value / dmax);
                    }
                    if (value < 0) {
                      dValue = -32000 * (int) (value / dmin);
                    }
                    // slave.getProcessImage(3).setInputRegister(fun % 10000, (short) dValue);
                  } else { /// 参数超限报警
                    JOptionPane.showMessageDialog(null, "参数超限");
                    slave.stop();
                  }
                }
                if (paramAddr.substring(0, 1).equals("3")) {
                  double value = Double.parseDouble(dbo.get(name).toString());
                  double dmax = (Double) m_max.get(name);
                  double dmin = (Double) m_min.get(name);
                  double dValue = 0;
                  if (value > dmax || value < dmin) {
                    if (value >= 0) {
                      dValue = 32000 * (int) (value / dmax);
                    }
                    if (value < 0) {
                      dValue = -32000 * (int) (value / dmin);
                    }
                    // slave.getProcessImage(3).setHoldingRegister(fun % 10000, (short) dValue);
                  } else { /// 参数超限报警
                    JOptionPane.showMessageDialog(null, "参数超限");
                    slave.stop();
                  }
                  ;
                }
                if (paramAddr.substring(0, 1).equals("2")) {
                  String value = dbo.get(name).toString();
                  /// slave.getProcessImage(4).setInput(fun % 10000, Boolean.valueOf(value));
                }
                if (paramAddr.substring(0, 1).equals("1")) {
                  String value = dbo.get(name).toString();
                  // slave.getProcessImage(4).setCoil(fun % 10000, Boolean.valueOf(value));
                }
              }
            }
          }
        }
      } catch (Exception ex) {
      }
    }
예제 #3
0
  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) {
    }
  }