Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    non termina il programma

    ciao a tutti scusate se mi ripeto (vedi oggetto) ma il programma cui troverete il codice sotto non mi termina per quale oscuro seppur banale motivo?
    il codice tratta la comunicazione con un modem collegato alla porta seriale utilizzando le java comm il codice č lungo ma consolatevi con il fatto che potete usarlo dato che funziona( che consolazione?!)

    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.IOException;
    import java.io.Serializable;
    import java.util.TooManyListenersException;

    import javax.comm.*;


    /**
    * Simple program to read characters from the command-line, send
    * them to the specified serial port, and echo any characters
    returned.
    * Modeled after the SimpleRead and SimpleWrite sample programs
    included
    * with the Java Communications API distribution.
    */
    public class SimpleWrite1 implements Runnable,
    SerialPortEventListener {

    static String DEFAULT_PORT = "COM4";

    InputStream in;
    OutputStream out;
    private String message = "AT\r";

    private Object readerThread;
    public void writeToCommPort(String message) {
    this.message = message;
    Thread readerThread;
    }


    /**
    * Main method.
    */
    public static void main(String[] args) {
    String portname = DEFAULT_PORT;
    if (args.length > 1) {
    System.err.println("Usage: java SimpleReadWrite [port_name]");
    System.exit(1);
    }
    else if (args.length == 1) {
    portname = args[0];
    }

    try {
    CommPortIdentifier portId =

    CommPortIdentifier.getPortIdentifier(portname);
    if (portId.getPortType() ==
    CommPortIdentifier.PORT_SERIAL) {
    SimpleReadWrite1 srw = new SimpleReadWrite1(portId);
    }
    else {
    System.err.println(portname + " is not a serial port");
    System.exit(1);
    }
    }
    catch (NoSuchPortException e) { //Port nicht vorhanden
    System.err.println("No such port: " + portname);
    System.exit(1);
    }
    catch (PortInUseException e) { //Port ist gesperrt/inGebrauch
    System.err.println("Port in use: " + portname);
    System.exit(1);
    }
    }

    /**
    * Constructor.
    */
    public SimpleWrite1(CommPortIdentifier portId) throws
    PortInUseException {
    try {
    SerialPort serialPort = (SerialPort)
    portId.open("SimpleReadWrite",
    2000);
    //Port mit
    in = serialPort.getInputStream();
    out = serialPort.getOutputStream();
    serialPort.addEventListener(this);
    serialPort.notifyOnDataAvailable(true);
    serialPort.setSerialPortParams(9600, //Schnittstelleneinstellungen initialisieren
    SerialPort.DATABITS_8,
    SerialPort.STOPBITS_1,
    SerialPort.PARITY_NONE);
    Thread readerThread = new Thread(this);
    readerThread.start();

    } //Exception-Handling
    catch (IOException e) {
    System.err.println("Problem opening streams");
    System.exit(1);
    }
    catch (TooManyListenersException e) {
    System.err.println("Too Many Listeners");
    System.exit(1);
    }
    catch (UnsupportedCommOperationException e) {
    System.err.println("Problem setting port parameters");
    System.exit(1);
    }
    }

    /**
    * This method loops forever, reading data from the
    * command line and writing it directly to the serial port.
    * No buffering is performed.
    */
    public void run() {

    try {



    out.write(message.getBytes());



    }
    catch (IOException e) {
    // Terminate thread
    }
    }




    /**
    * Process serial port events. This method is required
    * when implementing SerialPortEventListener
    *
    * @param event SerialPortEvent object to process
    */
    public void serialEvent(SerialPortEvent event) {
    switch (event.getEventType()) {
    case SerialPortEvent.BI:
    case SerialPortEvent.OE:
    case SerialPortEvent.FE:
    case SerialPortEvent.PE:
    case SerialPortEvent.CD:
    case SerialPortEvent.CTS:
    case SerialPortEvent.DSR:
    case SerialPortEvent.RI:
    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
    break;
    case SerialPortEvent.DATA_AVAILABLE:
    // If there is data available on serial port,
    // read it in in chunks <=20 bytes
    //
    byte[] readBuffer = new byte[1];
    try {String l;
    while (in.available() >0 && (in.read())!=0) {

    int numBytes = in.read(readBuffer);

    in.close();

    }

    System.out.write(readBuffer);


    }
    catch (IOException e) {
    // Terminate handling this event on read error
    }
    break;
    default:
    System.out.print("Unknown SerialPortEvent type = " +
    event.getEventType());
    break;
    }
    }
    }
    ciao e grazie
    riccardo

  2. #2
    Utente di HTML.it L'avatar di MMarzia
    Registrato dal
    Mar 2001
    Messaggi
    1,781

    Re: non termina il programma

    Originariamente inviato da ric_mancio
    ciao a tutti scusate se mi ripeto (vedi oggetto)
    il crossposting č vietato
    inoltre sarebbe opportuno specificare il linguaggio anche nel titolo, senza essere troppo vaghi

    č tutto scritto nel regolamento
    io sono festosamente cicciottello :: e. cartman

    t'amo senza sapere come, nč quando nč da dove,
    t'amo direttamente senza problemi nč orgoglio:
    cosė ti amo perchč non so amare altrimenti

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.