Как открыть последовательное соединение с помощью библиотеки jamod? - PullRequest
0 голосов
/ 04 февраля 2019

Я пытаюсь написать программу с использованием библиотеки jamod.У меня проблема с открытием соединения.Я использовал пример с сайта.

import java.net.*;
import java.io.*;
import net.wimpi.modbus.*;
import net.wimpi.modbus.msg.*;
import net.wimpi.modbus.io.*;
import net.wimpi.modbus.net.*;
import net.wimpi.modbus.util.*;


public class Test {

public static void main(String[] args) {
    /* 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
    ModbusCoupler modbusCoupler=null;
    /* Variables for storing the parameters */
    String portname= "COM5"; //the name of the serial port to be used
    int unitid = 1; //the unit identifier we will be talking to
    int ref = 2; //the reference, where to start reading from
    int count = 2; //the count of IR's to read
    SerialParameters params =null;

         modbusCoupler = ModbusCoupler.getReference();
         modbusCoupler.setUnitID(0);
         modbusCoupler.setMaster(true); 
    params = new SerialParameters();
    params.setPortName(portname);
    params.setBaudRate(9600);
    params.setDatabits(8);
    params.setParity("None");
    params.setStopbits(1);
    params.setEncoding(Modbus.SERIAL_ENCODING_RTU);
    params.setEcho(true);


     try {
         con = new SerialConnection(params);
         con.open();
         req = new ReadInputRegistersRequest(ref, count);
         req.setUnitID(unitid);
         req.setHeadless();
         trans = new ModbusSerialTransaction(con);
         trans.setRequest(req);
         trans.execute();
          res = (ReadInputRegistersResponse) trans.getResponse();
          for (int n = 0; n < res.getWordCount(); n++) {
            System.out.println("Word " + n + "=" + res.getRegisterValue(n));
          }
          con.close();  
        } catch (Exception ex) {
          ex.printStackTrace();
        }

}

}

Я получаю сообщение об ошибке:

java.lang.Exception 
at net.wimpi.modbus.net.SerialConnection.open(SerialConnection.java:91)
at Test.main(Test.java:43)

Похоже, у меня неправильные параметры, но я использовал тестовую программу и знаю, что они хороши.

Есть идеи, что может быть не так?

...