I am programming using Java for a data acquisition project. I completed the interface to the device part in Python, but it's way too heavy on the processing.
I have been converting it to Java (newbie!) and was able to send and receive the setup data, which are all ASCII.
Once I ask for the data to start flowing, it's like an open faucet.
I can understand the first 4 character but then I don't.
53 31 5C B5 61 99 AB 8B A9 85 91 89 55 85 (Hex)
S 1 This is the echo to say 'starting'
5C B5 61 99 AB 8B A9 85 91 89 55 85 (Hex)
This is the data I need and how it was sent. I can sniff it on the network monitor.
83 49 92 65533 97 65533 65533 65533 65533 (dec)
I have tried various methods, but currently:
Socket socket=new Socket(ipAddress , portNumber); //Create Socket
inputStream=socket.getInputStream();
inputStreamReader=new InputStreamReader(inputStream); // Tried (inputStream,"UTF-8")
stringBuffer=new StringBuffer();
public static StringBuffer recMsg(InputStreamReader inputStreamReader, StringBuffer stringBuffer) throws IOException {
int x;
stringBuffer.setLength(0);
while(true)
{
x=inputStreamReader.read();
stringBuffer.append((char)x);
//System.out.println("Ready to read:2 "+ inputStreamReader.ready());
if (stringBuffer.length() >= 500) break;
if (!inputStreamReader.ready()) break; //
}
System.out.println("Response "+ stringBuffer);
System.out.println(" --- ");
return stringBuffer;
}
PS in Python I just used
s.connect((host, port))
s.recv(2048)
new InputStreamReader(inputStream, "US-ASCII");
. – sorifiendReader
.ready()
is not a test for end of stream: it has few correct uses, and this isn't one of them. – user207421inputStreamReader.read()
will return-1
on EOF, eg:while ((x=inputStreamReader.read()) != -1) { stringBuffer.append((char)x); if (stringBuffer.length() >= 500) break; }
– Remy Lebeau