Modbus TCP经常出现读不到数据的问题

hidafa 2013-08-14 04:37:02
服务器是正常的,但Modbus TCP经常出现读不到数据的问题。
麻烦大家看看这个类是不是有问题?
另外哪位有稳定的Modbus TCP通讯类,提高一下,谢谢。
谢谢。


package net.wimpi.modbus.facade;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ModbusConnect {
//instance attributes
private Socket m_Socket;
private int m_Timeout = ModbusTCPMaster.TIMEOUT;
private boolean m_Connected;
private DataInputStream m_Input = null; //input stream
private DataOutputStream m_Output = null; //output stream
private InputStream m_InputStream = null; //wrap into filter input
private OutputStream m_OutputStream = null; //wrap into filter output


/**
* Constructs a <tt>TCPMasterConnection</tt> instance
* with a given destination address.
*
* @param adr the destination <tt>InetAddress</tt>.
*/
public ModbusConnect() {
}//constructor

/**
* Opens this <tt>TCPMasterConnection</tt>.
*
* @throws Exception if there is a network failure.
*/
public void connect(String IP)
throws Exception {
if(!m_Connected) {
try{
m_Socket = new Socket(IP, 502);
m_Socket.setSoTimeout(1000);
new ModbusConnect().prepare();
m_Connected = true;
}catch (Exception e) {
m_Connected = false;
}
}
}//connect

/**
* Closes this <tt>TCPMasterConnection</tt>.
*/
public void close() {
if(m_Connected) {
try {
m_InputStream.close();
m_OutputStream.close();
m_Output.close();
m_Input.close();
m_Socket.close();
} catch (IOException ex) {
}
m_Connected = false;
}
}//close


/**
* Returns the timeout for this <tt>TCPMasterConnection</tt>.
*
* @return the timeout as <tt>int</tt>.
*/
public int getTimeout() {
return m_Timeout;
}//getTimeout

/**
* Sets the timeout for this <tt>TCPMasterConnection</tt>.
*
* @param timeout the timeout as <tt>int</tt>.
*/
public void setTimeout(int timeout) {
m_Timeout = timeout;
try {
m_Socket.setSoTimeout(m_Timeout);
} catch (IOException ex) {
//handle?
}
}//setTimeout


/**
* Tests if this <tt>TCPMasterConnection</tt> is connected.
*
* @return <tt>true</tt> if connected, <tt>false</tt> otherwise.
*/
public boolean isConnected() {
return m_Connected;
}//isConnected

public Socket getM_Socket() {
return m_Socket;
}

public void setM_Socket(Socket socket) {
m_Socket = socket;
}

public void prepare() throws IOException{
if(m_Socket != null){
m_InputStream = m_Socket.getInputStream();
m_OutputStream = m_Socket.getOutputStream();
m_Output = new DataOutputStream(m_OutputStream);
m_Input = new DataInputStream(m_InputStream );

}
}

public List read(int unitvar,int startAddress,int length,int number) throws UnknownHostException, IOException{

List list = new ArrayList();
if(m_Output != null){
synchronized (m_Output) {

int number1 = 0;
int number2 = 0;
if(number > 255 ){
number1 = number/256;
number2 = number - (number1*256);
}else{
number2 = number;
}
int startAddress1 = 0;
int startAddress2 = 0;
if(startAddress > 255 ){
startAddress1 = startAddress/256;
startAddress2 = startAddress - (startAddress1*256);
}else{
startAddress2 = startAddress;
}
byte[] b = new byte[12];
b[0] = (byte)number1;
b[1] = (byte)number2;

b[2] = (byte)0;
b[3] = (byte)0;

b[4] = (byte)0;
b[5] = (byte)6;

b[6] = (byte)unitvar;
b[7] = (byte)3;

b[8] = (byte)startAddress1;
b[9] = (byte)startAddress2;

b[10] = (byte)0;
b[11] = (byte)length;

try{
m_Output.write(b);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
close();
}
m_Output.flush();
int readBufferNumber = length*2 + 9;
byte[] readBuffer = new byte[readBufferNumber];


int nIndex = 0;
int nTotalLen = readBuffer.length;
int nReadLen = 0;

try{
while (nIndex < nTotalLen)
{
nReadLen = m_Input.read(readBuffer, nIndex, nTotalLen - nIndex);

if (nReadLen > 0)
{
nIndex = nIndex + nReadLen;
}
else
{
break;
}
}

}catch (Exception e) {
// TODO: handle exception
Date date = new Date();
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
java.sql.Time sqlTime = new java.sql.Time(date.getTime());
String t = sqlDate.toString() + " " + sqlTime.toString();

System.out.println("read error >>> Time:" + t
+ " IP:" + m_Socket.getRemoteSocketAddress()
+ " unitvar:" + unitvar
+ " startAddress:" + startAddress
+ " length:" + length
+ " number:" + number);
e.printStackTrace();
close();
}


for (int i = 9; i < readBuffer.length; i++) {
Integer var = Byte.valueOf(readBuffer[i]).intValue();
if(var < 0) var = var + 256;
list.add(var);
}

}

}

return list;
}

}
...全文
1690 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

25,985

社区成员

发帖
与我相关
我的任务
社区描述
高性能WEB开发
社区管理员
  • 高性能WEB开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧