2014-09-15 3 views
0

jamod 2 (wimpi.modbus)를 사용하여 모드 버스 데이터를 읽을 수 없습니다 신청.</p> <p>내가 내 자바에서 시리얼 포트를 통해 모드 버스 RTU를 통해 모드 버스 슬레이브 장치에서 데이터를 읽으려고 한 내가 자바 직렬 포트 프로그래밍에 새로운 오전

저는 modbus 프로토콜을 읽기 위해 Jamod 자바 라이브러리를 사용하고 있습니다.

내 응용 프로그램이 장치에서 전체 Modbus 응답을받지 못했습니다. 참조를 위해 내 Java 코딩 및 오류 로그를 찾으십시오.

오류의 원인을 알려주는 사람이 누구든 나에게 제안 할 수 있습니다.

<br/> 

**ERROR**<br/> 
Clear input: 02 c2 c1<br/> 
Sent: 01 04 03 e8 00 05 b0 79 <br/> 
Last request: 01 04 03 e8 00 05 b0 79<br/> 
CRC Error in received frame: 0 bytes: <br/> 
Response: 01 84 <br/> 
net.wimpi.modbus.ModbusIOException: I/O exception - failed to read<br/> 
at net.wimpi.modbus.io.ModbusRTUTransport.readResponse(ModbusRTUTransport.java:163)<br/> 
at net.wimpi.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:187)<br/> 
at modbusnewapplication.ModbusConnection.main(ModbusConnection.java:8<br/> 


Modbus Program <br/> 
---------------<br/> 

package modbusnewapplication;<br/> 

import java.io.;<br/> 
import javax.comm.;<br/> 
import net.wimpi.modbus.ModbusCoupler;<br/> 
import net.wimpi.modbus.io.ModbusSerialTransaction;<br/> 
import net.wimpi.modbus.msg.ReadInputRegistersRequest;<br/> 
import net.wimpi.modbus.msg.ReadInputRegistersResponse;<br/> 
import net.wimpi.modbus.net.SerialConnection;<br/> 
import net.wimpi.modbus.util.SerialParameters;<br/> 

public class ModbusConnection {<br/> 

public static void main(String[] args) {<br/> 

// if (args.length < 4) {<br/> 
// System.out.println("not enough args");<br/> 
// System.exit(1);<br/> 
// }else{<br/> 
try {<br/> 

    System.out.println("Serial Port Connection");<br/><br/> 
    /* The important instances of the classes mentioned before */<br/> 
    SerialConnection con = null; //the connection<br/> 
    ModbusSerialTransaction trans = null; //the transaction<br/> 
    ReadInputRegistersRequest req = null; //the request<br/> 
    ReadInputRegistersResponse res = null; //the response<br/> 

    // **1 Variables for storing the parameters** <br/> 
    String portname= "COM1"; //the name of the serial port to be used<br/> 
    int unitid = 1; //the unit identifier we will be talking to<br/> 
    int ref = 1000; //the reference, where to start reading from<br/> 
    int count = 5; //the count of IR's to read<br/> 
    int repeat = 1; //a loop for repeating the transaction <br/> 
    boolean isopen = false;<br/><br/> 

    **// 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**<br/> 
    SerialParameters params = new SerialParameters();<br/> 
    params.setPortName("COM1");<br/> 
    params.setBaudRate(9600);<br/> 
    params.setDatabits(8);<br/> 
    params.setParity("None");<br/> 
    params.setStopbits(1);<br/> 
    params.setEncoding("RTU");<br/> 
    params.setEcho(false);<br/> 

    System.setProperty("net.wimpi.modbus.debug", "true");<br/> 
    **// 4. Open the connection**<br/> 
    con = new SerialConnection(params); 
    System.out.println("Connection..." + con.toString()); 
    con.open(); 

    isopen = con.isOpen();<br/> 
    System.out.println("Serial port status..." + isopen);<br/> 

    **// 5. Prepare a request<br/>** 
    req = new ReadInputRegistersRequest(ref, count);<br/> 
    req.setUnitID(unitid);<br/> 
    req.setHeadless();<br/> 

    **// 6. Prepare a transaction<br/>** 
    trans = new ModbusSerialTransaction(con);<br/> 
    trans.setRequest(req);<br/> 

    **// 7. Execute the transaction repeat times<br/>** 
    int k = 0;<br/> 
    do { <br/> 
    trans.execute();<br/> 
    res = (ReadInputRegistersResponse) trans.getResponse();<br/> 
    for (int n = 0; n < res.getWordCount(); n++) {<br/> 
    System.out.println("Word " + n + "=" + res.getRegisterValue(n));<br/> 
    }<br/> 
    k++;<br/> 
    } while (k < repeat);<br/> 

    **// 8. Close the connection**<br/> 
    con.close();<br/> 

    } catch (Exception ex) {<br/> 
     ex.printStackTrace();<br/> 
    }<br/> 
    //}//else<br/> 
    }//main 

    } 

답변

0

당신은 Thread.sleep(500) 라이브러리 jamod의 방법 request 사이 response 같은 방법을 추가해야합니다. 아마도 이런 종류의 오류는 보통 응답 데이터의 길이에 기인합니다. jamod 라이브러리는 응답 데이터의 긴 크기를 고려하지 않았습니다. 따라서 직렬 인터페이스에서 모든 데이터가 수신 될 때까지 기다려야합니다. 그렇지 않으면 모든 데이터가 수신되지 않기 때문에 CRC 검사가 실패하고 오류가 발생합니다.