首页 文章

使用udp在java和simulink之间传输双精度数

提问于
浏览
6

我需要在Java程序和Simulink模型之间传输十进制值,为此我使用UDP套接字,它们在java方面没有问题 . 在Simulink中,我能够使用“Stream Output”块发送值,但是从java接收时会出现问题! 'Stream input'块没有收到任何东西 . 我正在使用标准设备UDP protocole,使用正确的本地UDP端口,地址是'localhost . 请告诉我如何正确地使用udp在simulink中接收双倍,或者甚至用其他方法,什么是传输数据 . 提前致谢 . 这里有一些代码:

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

在主要方法中:

localSend(myMessage, 9005);

'Input Stream'块的'Board setup'是Simulink如下:
enter image description here

这是我如何从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;
            }
 }

和在Simulink中设置"Output Stream"块:
enter image description here

2 回答

  • 0

    另一种解决方案,在Simulink中输入流 . 只需在java中为每条消息添加一个终止符“\ n” .

  • 0

    我做了一个技巧 . 'Packet Input' Simulink块,'ASCII Decode',
    blocks connection

    我调整这两个块的参数如下:

    the 'Packet Input' block

    the 'ASCII Decoder

    在java方面,我用这种方法重新格式化'double':

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

    简要说明:数据包输入块每次接收7个ASCII,并且在java中我重新格式化double,组合7个字符(包括点和减号) .

    希望这能有所帮助 .

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

相关问题