首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用udp在java和simulink之间传输双倍

使用udp在java和simulink之间传输双倍
EN

Stack Overflow用户
提问于 2016-03-30 14:01:42
回答 2查看 1K关注 0票数 6

我需要在Java程序和Simulink模型之间传递十进制值,这样我才能使用UDP套接字,它们在java方面是没有问题的。在Simulink中,我可以使用'Stream‘块发送值,但是问题在从java接收时出现了!“流输入”块没有接收任何东西。我使用的是标准设备UDP协议,具有正确的本地UDP端口,地址是'localhost。请告诉我如何在simulink中正确地接收udp,甚至其他方法,传输数据有什么关系。谢谢。下面是一些代码:

代码语言:javascript
复制
localSocket = new DatagramSocket(9010);

..。

代码语言:javascript
复制
 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);
}

在主要方法方面:

代码语言:javascript
复制
localSend(myMessage, 9005);

“输入流”块的“板设置”为Simulink,如下所示:

下面是我如何从Simulink (该方法)接收数据:

代码语言:javascript
复制
    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中设置“输出流”块:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-05 08:15:36

我做了个小把戏。‘包输入’Simulink块,和'ASCII解码‘,

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

在java端,我用以下方法“重新格式化”了double:

代码语言:javascript
复制
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 I中重新格式化由7个字符(包括点和减号)组成的双字符。

希望这能帮到别人。

更新:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-05-10 18:30:09

另一种解决方案,在Simulink中输入流。只需向来自java的每条消息添加一个终止符"\n“即可。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36311156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档