How to configure Java to transfer data between code and matlab over UDP network

3 visualizzazioni (ultimi 30 giorni)
I am needing to transfer double data types to and from one computer using java and another computer running a matlab simulation over UDP/IP. I am correctly receiving the data from the matlab PC to the java PC but the matlab PC is simply not receiving the data sent from the PC running the java code. I am sure that the java code is sending the data because I tested it by sending the data to my phone through a UDP app in which the data was received as intended. I am not sure however if port number and IP in the code is getting the data to where it needs to go. The data sent from the Java to the PC is a value represented as a string that is reformatted into 7-digit ASCII using the reformat function shown in the code. It is then sent to Packet Input block in matlab that is then put into an ASCII decoder which converts the ASCII into a double. I have added the code below as well as relevant images.
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.net.InetAddress;
import java.text.DecimalFormat;
class receiver
{
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";
}
}
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(1024);
byte[] receiveData = new byte[6];
byte[] sendData = new byte[6];
byte[] remoteIp = new byte[] {(byte)146, (byte)201, (byte)69, (byte)164};
String testValue = "9.99999";
String testFormatted;
//serverSocket.connect(InetAddress.getByAddress(remoteIp), 1600);
if(serverSocket.isConnected()){
System.out.println("Connected to remote port " + serverSocket.getPort() + "\nConnected to local port " + serverSocket.getLocalPort() + "\nListening...");
}
while(true)
{
testFormatted = reformat(testValue);
DatagramPacket sendPacket = new DatagramPacket(testFormatted.getBytes(), testFormatted.getBytes().length, InetAddress.getByAddress(remoteIp), 26);
serverSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
ByteBuffer bb = ByteBuffer.wrap(receivePacket.getData(), 0, receivePacket.getLength());
bb.order(ByteOrder.LITTLE_ENDIAN);
byte data = bb.get();
System.out.println(data);
}
}
}

Risposte (1)

Gao Yang
Gao Yang il 5 Feb 2020
Hello,sir! Have you found ways to solve this problem? I met the similar problem when coding in Python. I am looking forward to your reply. Very thanks.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by