Copy Code code as follows:
Import gnu.io.*;
Import java.util.*;
Import java.io.*;
public class CommTest
{
Static Commportidentifier Portid;
Static enumeration portlist;
static int bauds[] = {9600, 19200, 57600, 115200}; The baud rate supported by the detection port
public static void Main (string[] args)
{
Portlist = Commportidentifier.getportidentifiers ();
SYSTEM.OUT.PRINTLN ("SMS Device port Connection test ...");
while (Portlist.hasmoreelements ())
{
Portid = (commportidentifier) portlist.nextelement ();
if (portid.getporttype () = = commportidentifier.port_serial)
{
SYSTEM.OUT.PRINTLN ("Find serial:" + portid.getname ());
for (int i = 0; i < bauds.length; i++)
{
System.out.print ("trying at" + bauds[i] + "...");
Try
{
SerialPort SerialPort;
InputStream instream;
OutputStream OutStream;
int C;
String response;
SerialPort = (serialPort) portid.open ("Smslibcommtester", 1971);
Serialport.setflowcontrolmode (serialport.flowcontrol_rtscts_in);
Serialport.setserialportparams (Bauds[i], serialport.databits_8, Serialport.stopbits_1, SerialPort.PARITY_NONE);
Instream = Serialport.getinputstream ();
OutStream = Serialport.getoutputstream ();
Serialport.enablereceivetimeout (1000);
c = Instream.read ();
while (c!=-1)
c = Instream.read ();
Outstream.write (' A ');
Outstream.write (' T ');
Outstream.write (' \ R ');
Try
{
Thread.Sleep (1000);
}
catch (Exception e)
{
}
Response = "";
c = Instream.read ();
while (c!=-1)
{
Response + = (char) c;
c = Instream.read ();
}
if (Response.indexof ("OK") >= 0)
{
Try
{
System.out.print ("Get device information ...");
Outstream.write (' A ');
Outstream.write (' T ');
Outstream.write (' + ');
Outstream.write (' C ');
Outstream.write (' G ');
Outstream.write (' M ');
Outstream.write (' M ');
Outstream.write (' \ R ');
Response = "";
c = Instream.read ();
while (c!=-1)
{
Response + = (char) c;
c = Instream.read ();
}
SYSTEM.OUT.PRINTLN ("Discovery Device:" + response.replaceall ("\\s+ok\\s+", ""). ReplaceAll ("\ n", ""). ReplaceAll ("\ R", ""));
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("No equipment found!");
}
}
else System.out.println ("No device Found!");
Serialport.close ();
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("No equipment found!");
}
}
}
}
}
}