Kopieren Sie den Codecode wie folgt:
gnu.io.* importieren;
import java.util.*;
java.io.* importieren;
öffentliche Klasse CommTest
{
static CommPortIdentifier portId;
statische Enumeration portList;
static int bauds[] = { 9600, 19200, 57600, 115200 }; //Ermitteln Sie die vom Port unterstützte Baudrate
public static void main(String[] args)
{
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("SMS-Geräte-Port-Verbindungstest...");
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println("Serieller Port gefunden: " + portId.getName());
for (int i = 0; i < bauds.length; i++)
{
System.out.print(" Trying at " + bauds[i] + "...");
versuchen
{
SerialPort serialPort;
InputStream inStream;
OutputStream outStream;
int c;
String-Antwort;
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');
versuchen
{
Thread.sleep(1000);
}
Catch (Ausnahme e)
{
}
Antwort = "";
c = inStream.read();
while (c != -1)
{
Antwort += (char) c;
c = inStream.read();
}
if (response.indexOf("OK") >= 0)
{
versuchen
{
System.out.print("Geräteinformationen abrufen...");
outStream.write('A');
outStream.write('T');
outStream.write('+');
outStream.write('C');
outStream.write('G');
outStream.write('M');
outStream.write('M');
outStream.write('/r');
Antwort = "";
c = inStream.read();
while (c != -1)
{
Antwort += (char) c;
c = inStream.read();
}
System.out.println(" Gerät gefunden: " + Response.replaceAll("//s+OK//s+", "").replaceAll("/n", "").replaceAll("/r", " "));
}
Catch (Ausnahme e)
{
System.out.println("Kein Gerät gefunden!");
}
}
else System.out.println("Kein Gerät gefunden!");
serialPort.close();
}
Catch (Ausnahme e)
{
System.out.println("Kein Gerät gefunden!");
}
}
}
}
}
}