mkm*_*mkm 6 java sockets matlab network-programming simulink
我需要在Java程序和Simulink模型之间传输十进制值,为此我使用UDP套接字,它们在java方面没有问题.在Simulink中,我能够使用"Stream Output"块发送值,但是从java接收时会出现问题!'Stream input'块没有收到任何东西.我正在使用标准设备UDP protocole,使用正确的本地UDP端口,地址是'localhost.请告诉我如何正确地使用udp在simulink中接收双倍,或者甚至用其他方法,什么是传输数据.提前致谢.这里有一些代码:
localSocket = new DatagramSocket(9010);
Run Code Online (Sandbox Code Playgroud)
...
public static void localSend(String msg,int PORT) throws Exception{
DatagramPacket sendPacket = null,encPacket=null;
try {
sendPacket = new DatagramPacket(msg.getBytes(), msg.getBytes().length, InetAddress.getLocalHost(), PORT);
} catch (Exception e) {
System.out.printf("Error!");
}
localSocket.send(sendPacket);
}
Run Code Online (Sandbox Code Playgroud)
在主要方法中:
localSend(myMessage, 9005);
Run Code Online (Sandbox Code Playgroud)
这是我如何从Simulink ins Java(方法)接收数据:
public static String localReceive() throws Exception{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
int count=0;
try {
localSocket.receive(receivePacket);
return new String(receivePacket.getData(),receivePacket.getOffset(),receivePacket.getLength());
} catch (SocketTimeoutException socketTimeoutException) {
return defaultValue;
}
}
Run Code Online (Sandbox Code Playgroud)
我做了一个伎俩。“数据包输入”Simulink 模块和“ASCII 解码”,

我调整这两个块的参数如下:
在java方面,我使用以下方法“重新格式化”双精度:
public static String reformat(String str){
double d = 0;
DecimalFormat df=null;
try {
d = Double.parseDouble(str);
} catch (NumberFormatException numberFormatException) {
return "0.00000";
}
if(d>=0){
String[] sp=str.split("\\.");
if(sp[0].length()==0)
df= new DecimalFormat("0.00000");
if(sp[0].length()==1)
df= new DecimalFormat("0.00000");
if(sp[0].length()==2)
df= new DecimalFormat("00.0000");
if(sp[0].length()==3)
df= new DecimalFormat("000.000");
if(sp[0].length()==4)
df= new DecimalFormat("0000.00");
if(sp[0].length()==5)
df= new DecimalFormat("00000.0");
}
else{
String[] sp=str.split("\\.");
if(sp[0].length()==1)
df= new DecimalFormat("0.0000");
if(sp[0].length()==2)
df= new DecimalFormat("0.0000");
if(sp[0].length()==3)
df= new DecimalFormat("00.000");
if(sp[0].length()==4)
df= new DecimalFormat("000.00");
if(sp[0].length()==5)
df= new DecimalFormat("0000.0");
if(sp[0].length()==6)
df= new DecimalFormat("000000");
}
try {
return df.format(d);
} catch (Exception e) {
return "0.00000";
}
}
Run Code Online (Sandbox Code Playgroud)
简而言之:数据包输入块每次接收 7 个 ASCII,并且在 java 中,我将 double 重新格式化为 7 个字符的组合(包括点和减号)。
希望这对某人有帮助。
更新:
some self explanatory extra code:
//somewhere before:
//Global variables
String defaultValue="0",ip="xxx.xx.xx.xx";
DatagramSocket remoteSocket,localSocket;
byte[] receiveArray,receiveKey,receiveSig,sendSig;
int remoteSendPort=xxx,localSendport=xxx,
remoteReceivePort=xxx,localReceivePort=xxx;
String feedback,control_val,ReceivedTimeStamp;
InetAddress IPAddress;
...
//receive message from the other java program in pc2
public void remoteMsgSend(byte[] msg,InetAddress IPAddress, int PORT) {
try {
DatagramPacket sendPacket = null;
try {
sendPacket = new DatagramPacket(msg, msg.length, IPAddress, PORT);
} catch (Exception e) {
System.out.printf("Error! check ports");
}
remoteSocket.send(sendPacket);
} catch (IOException ex) {
System.out.println("IOException! remote send");
}
}
//receive message from the other java program in pc2
public String remoteMsgReceive() {
DatagramPacket receivePacket = new DatagramPacket(receiveArray, receiveArray.length);
byte[] r1;
int count=0,len,offset;
try {
remoteSocket.receive(receivePacket);
r1=receivePacket.getData();
len=receivePacket.getLength();
offset=receivePacket.getOffset();
r1=Arrays.copyOfRange(r1, offset, len);
remoteOk=true;
return new String(r1);
} catch (Exception ex) {
// System.out.println("remote receive time out: " +ex.getMessage());
remoteOk=false;
return defaultValue;
}
}
//send data to matlab on this pc
public void localSend(String msg,int PORT) {
DatagramPacket sendPacket = null;
try {
sendPacket = new DatagramPacket(msg.getBytes(), msg.getBytes().length, InetAddress.getLocalHost(), PORT);
} catch (Exception e) {
System.out.printf("Error! check ports");
}
try {
localSocket.send(sendPacket);
} catch (IOException ex) {
System.out.println("localsend error");
}
}
//receive data from Matlab on this pc
public String localReceive() {
DatagramPacket receivePacket = new DatagramPacket(receiveArray, receiveArray.length);
String rec;
try {
localSocket.receive(receivePacket);
rec=new String(receivePacket.getData(),receivePacket.getOffset(),receivePacket.getLength());
localOk=true;
return rec;
} catch (Exception ex) {
// System.out.println("local receive time out " +ex.getMessage());
localOk=false;
return defaultValue;
}
}
Run Code Online (Sandbox Code Playgroud)