0% found this document useful (0 votes)
661 views

Sorce Code Auto Detect Device

The document appears to be Java code for testing communication ports and sending messages. It imports various Java libraries for logging, serial communication, and message sending. The main class defines methods for finding available serial ports, testing a port for success in sending a message, and returning the name of the first port that successfully sends a message.

Uploaded by

Alan Tea
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
661 views

Sorce Code Auto Detect Device

The document appears to be Java code for testing communication ports and sending messages. It imports various Java libraries for logging, serial communication, and message sending. The main class defines methods for finding available serial ports, testing a port for success in sending a message, and returning the name of the first port that successfully sends a message.

Uploaded by

Alan Tea
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

package com.cubepro.util; import import import import java.util.Collection; java.util.Collections; java.util.Enumeration; java.util.

Formatter;

import org.apache.log4j.Logger; import org.smslib.helper.CommPortIdentifier; import com.cubepro.general.CommonConstants; import com.cubepro.util.SendMessage; public class CommPortTester { private static final String _NO_DEVICE_FOUND = " no device found"; private final static Formatter _formatter = new Formatter(System.out); private static Logger log = Logger.getLogger(CommPortTester.class); static CommPortIdentifier portId; static Enumeration<CommPortIdentifier> portList; static int bauds[] = { 9600, 14400, 19200, 28800, 33600, 38400, 56000, 57600, 115200 }; public static final String MAINCLASS = "org.smslib.Service"; public CommPortTester() throws Exception { Class.forName(MAINCLASS); } /** * Wrapper around {@link CommPortIdentifier#getPortIdentifiers()} to be * avoid unchecked warnings. */ private static Enumeration<CommPortIdentifier> getCleanPortIdentifiers() { return CommPortIdentifier.getPortIdentifiers(); } public String testAndQualifyPort() throws Exception { String status = CommonConstants.MODEM_STATUS_ERROR; SendMessage sendMessage = new SendMessage(); log.debug("\nSearching for devices..."); portList = getCleanPortIdentifiers(); while (portList.hasMoreElements()) { portId = portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { _formatter.format("%nFound port: %-5s%n", portId.getName()); try { if(portId.getName() boolean comPortSuccess = sendMessage.doIt(portId.getName()); if(comPortSuccess == true){ return portId.getName(); } } catch (final Exception e) {

log.debug(" Modem error occured -",e); } } } log.debug("\nTest complete."); return status; } public static void main(String[]args){ try{ CommPortTester tester = new CommPortTester(); tester.testAndQualifyPort(); }catch(Exception e){ e.printStackTrace(); } } }

================================================================================ =========================================== One of the example is; import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; static OutputStream outputStream; static InputStream inputStream; FileInputStream fis = null; SerialPort serialPort; Thread readThread; int ch; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM6")) { System.out.println("checked"); // if (portId.getName().equals("/dev/term/a")) { SimpleRead reader = new SimpleRead(); } } } } public SimpleRead() { try { serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); } catch (PortInUseException e) {System.out.println(e);} try { inputStream = serialPort.getInputStream();

} catch (IOException e) {System.out.println(e);} try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {System.out.println(e);} serialPort.notifyOnDataAvailable(true); serialPort.notifyOnCarrierDetect(true); serialPort.notifyOnDataAvailable(true); serialPort.notifyOnBreakInterrupt(true); serialPort.notifyOnCTS(true); serialPort.notifyOnDSR(true); serialPort.notifyOnFramingError(true); serialPort.notifyOnOutputEmpty(true); serialPort.notifyOnOverrunError(true); serialPort.notifyOnParityError(true); serialPort.notifyOnRingIndicator(true); try { serialPort.setSerialPortParams(9600,SerialPort.DAT ABITS_8,SerialPort.STOPBITS_1 ,SerialPort.PARITY_NO NE); } catch (UnsupportedCommOperationException e) {System.out.println(e);} readThread = new Thread(this); readThread.start(); } public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) {System.out.println(e);} } public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) { case SerialPortEvent.BI: System.out.println("Break Interrupt"); break; case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI:

System.out.println("Pick up the receiver."); if( event.getNewValue() ) { System.out.println("Ring Indicator On"); } else { System.out.println("Ring Indicator Off"); } break; case SerialPortEvent.CD: if( event.getNewValue() ) {

System.out.println("Connected"); } else { System.out.println("Disconnected"); System.exit(0); } break; case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: try { // outputStream.write("ata".getBytes()); byte[] readBuffer = new byte[5048]; while (inputStream.available() > 0) { // outputStream.write("ata".getBytes()); // to hook the phone // System.out.println("ram"); int numBytes = inputStream.read(readBuffer); } System.out.print(new String(readBuffer)); outputStream.write("ata".getBytes()); } catch (IOException e) {System.out.println(e);} break; } } }

http://en.wikibooks.org/wiki/Serial_Programming/Serial_Java

You might also like