我正在尝试将 Modrssim(Modbus 模拟器)与 java 类进行通信。我正在向 modsim 发送查询,它会对此做出响应。但是当我尝试阅读回复时,我无法获得正确的字符。
这是我的代码:
import java.net.*;
import java.io.*;
class client
{
public static void main(String args[]) throws IOException
{
Socket s=null;
BufferedReader b=null;
char data[] ={0,0,0,0,0,6,1,2,0,2,0,0};
try
{
s=new Socket("199.199.51.7",502);
OutputStreamWriter writer = new OutputStreamWriter(s.getOutputStream());
BufferedWriter bw = new BufferedWriter(writer);
System.out.println("Sending Data....");
bw.write(data);
bw.flush();
b=new BufferedReader(new InputStreamReader(s.getInputStream()));
}
catch(UnknownHostException u)
{
System.err.println("I don't know host");
System.exit(0);
}
String inp;
System.out.println("Receiving......");
while((inp=b.readLine())!=null)
{
System.out.println(inp);
System.out.println("dONE");
}
b.close();
s.close();
}
}
作为回应,我在 modsim 中得到 00 00 00 00 00 03 01 02 00。MOdsim 中的响应图像在这里:
但在阅读时,它显示了一颗心和两个笑脸。收到的数据: