Transfer doubles between java and simulink with udp

I need to pass decimal values ​​between the Java program and the Simulink model, for this I use UDP sockets, they are not a problem in the java side. In Simulink, I can send values ​​using the "Output Stream" block, but the problem occurs when getting from java! block "Input stream" does not receive anything. I use standard UDP protocole devices with the correct local UDP port and the address is "localhost". Please tell me how to get a dual connection in simulink correctly using udp or even using other methods, what is the matter of data transfer. thanks in advance. here are some codes:

localSocket = new DatagramSocket(9010); 

...

  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); } 

and in the main method:

 localSend(myMessage, 9005); 

The “board setup” of the “Input stream” block is Simulink, as shown below: enter image description here

Here is how I get data from Simulink ins Java (method):

  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; } } 

and setting the block "Output stream" in Simulink: enter image description here

+6
source share
2 answers

I did the trick. "Input Package", Simulink block and "ASCII Decode", blocks connection

i configure the parameters of these two blocks as follows:

block

ASCII decoder

and in the java side I reformat the double using this method:

 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"; } } 

in short: the packet input block gets 7 ASCII every time, and in java I reformat the double, which should be combined from 7 characters (including the period and minus).

Hope this helps someone.

update:

 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; } } 
0
source

Another solution, with input stream in Simulink. Just add the terminator "\ n" to each post from java.

0
source

All Articles