First datastream works, second datastream hangs
Hi People, i am kind of new here and relatively new to JAVA,
Im connecting to a telemetric device. To send/receive data im using a string of hex instructions (see code)
Protocol:
- send "startsession" code
->receive serialnr + some other data
- send "instruction" code
- >receive general data of digital inputs, analog inputs jada jada..
So far it works to get online (after startsesion the light goes on)
And i receive data (maybe to few data but the first few bytes are ok)
Then..the program hangs when i try to write the second file with the println(input2.readLine()); function...
Can somebody please help me?
import java.awt.*;//abstract windowing toolkit
import java.net.*;//networking
import java.io.*;//DataIOStreams
publicclass Opnieuwwcextends Frame{
TextArea display=new TextArea();
//* loading hex data into byte array (yes i did the math correct :P ) *//
int adres = 0 ;
int ident = 0;
int instr = 0;
int len = 8;
int crcl = 37;
int crcr = 25;
byte byteadres =(byte) adres;
byte byteident =(byte) ident;
byte byteinstr = (byte) instr;
byte bytelen =(byte) len;
byte bytecrcl = (byte)crcl;
byte bytecrcr = (byte)crcr;
int adreslive = 0;
int identlive = 1;
int instrlive = 20;
int lenlive = 0;
int crcllive = 94;// & 0xff;
int crcrlive = 228;// & 0xff;
byte byteadreslive =(byte) adreslive;
byte byteidentlive =(byte) identlive;
byte byteinstrlive = (byte) instrlive;
byte bytelenlive =(byte) lenlive;
byte bytecrcllive = (byte)crcllive;
byte bytecrcrlive = (byte)crcrlive;
int crclstop = 180 & 0xff;
int crcrstop = 227 & 0xff;
byte byteclstop =(byte) crclstop;
byte bytecrstop =(byte) crcrstop;
byte bytepassa =(byte) 0x53;
byte bytepassb =(byte) 0x6d;
byte bytepassc =(byte) 0x61;
byte bytepassd =(byte) 0x72;
byte bytepasse =(byte) 0x74;
byte bytepassf =(byte) 0x58;
byte bytepassg =(byte) 0x4d;
byte bytepassh =(byte) 0x53;
byte[] startsessie ={0x00,0x00,0x00,0x08,0x53,0x6d,0x61,0x72,0x74,0x58,0x4d,0x53,0x25,0x19};
byte[] stopsessie ={0x00,0x00,0x0d,0x08,0x53,0x6d,0x61,0x72,0x74,0x58,0x4d,0x53,byteclstop,bytecrstop};
byte[] livedata ={byteadreslive,byteidentlive,byteinstrlive,bytelenlive,bytecrcllive,bytecrcrlive};
//* loaded hex data into byte array (yes i did the math correct :P ) *//
publicvoid Startses()//This part of the program starts the connection with the telemetric device
{
try{
System.out.println("Startsessie...");
Socket client=new Socket("192.168.2.100",8000);
DataOutputStream output=new DataOutputStream(client.getOutputStream());
// BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));
FileOutputStream fos =new FileOutputStream ("test.txt");//maak "test.txt" aan
output.write(stopsessie);/*Stop first? mark07 - lemon: i use this as a reset*/
output.flush();
try{ Thread.sleep(3000);}//3000 millisecs
catch(InterruptedException ex){}
output.write(startsessie);/*Start second? mark07 - lemon: this is where it starts the session and should return its serialnr. which it does properly...*/
output.flush();
BufferedReader input =new BufferedReader(new InputStreamReader(client.getInputStream()));
output.write(livedata);
final PrintWriter pw =new PrintWriter (fos);
if (input !=null){
pw.print(input.readLine());/* ...here*... so far so good..*/
}
pw.flush();
pw.close();
fos.close();
output.close();
input.close();
client.close();
}
catch (IOException ex)
{ex.printStackTrace();
}
}
publicvoid Getlive()// here im sending a "getdata" instruction and i can see the device respond (led) but somewhere here the program hangs
{
try{
System.out.println("Verbinding maken met...");
Socket client2=new Socket("192.168.2.100",8000);
System.out.println("Verbinding gemaakt met...gethostdinges");
DataOutputStream output2 =new DataOutputStream(client2.getOutputStream());
BufferedReader input2 =new BufferedReader(new InputStreamReader(client2.getInputStream()));
FileOutputStream fos2 =new FileOutputStream ("testing.txt");
output2.write(livedata);/*Make sure this isn't messing up the program~mark07*/
String temp2 ="";
String bb ="";
PrintWriter pw2 =new PrintWriter (fos2);
// THIS readLINE part hangs my program i think... i tried with an if-loop and: pw2.println(input2.read()); but and THEN i get a little data but corrupted...im not even sure if its comming from the telemetric device or some unclosed.. or overflushed buffer :(..help..sos
while (input2 !=null)
{
pw2.println(input2.readLine());
}
pw2.close();
// the program doesnt run as far as here, because this instruction closes the connection (led stays on) //
output2.write(stopsessie);// close the connection *doesnt matter if i close it or not acording to the protocol*
output2.flush();
pw2.flush();
pw2.close();
fos2.close();
output2.close();
client2.close();
}
catch (IOException ex)
{ex.printStackTrace();
}
System.out.println("Ende ...");
}
//main to run the programs :)
publicstaticvoid main(String[] args)throws java.io.FileNotFoundException, java.io.EOFException, java.io.IOException
{System.out.println("Verbinding maken met XMS...");
Opnieuwwc c =new Opnieuwwc();
c.Startses();
c.Getlive();
}
}
//this is what i received from input on first println and its perfect..i think...the serial is there:00 "?#932-02-05803
If tried: without loop as well ofcourse...and system.out.println function.....all...hang :(
I did found a bug report on the println fuction, but my implementation doesnt work..
Here is the link: http://developer.apple.com/technotes/tn/tn1157.html
All help is highly apreciated!!
Message was edited by:
Lemon_dehollande
Message was edited by:
Lemon_dehollande
Message was edited by:
Lemon_dehollande

