Shop OBEX P1 Docs P2 Docs Learn Events
writing a program to read the usb inputs — Parallax Forums

writing a program to read the usb inputs

science_geekscience_geek Posts: 247
edited 2009-09-27 00:08 in General Discussion
i had this idea the other day for my homemade cnc mill, i have been using a prop board and an itl 710 display, what im kinda looking for is how to output something from the prop to the usb port of the computer with a usb to serial converter. i want to read the data in using a java gui. the problem...i dont know how to read the usb ports using java. any idea where i should start.

Comments

  • dev/nulldev/null Posts: 381
    edited 2009-09-27 00:08
    If you run Unix, uncomment the /dev/term/a line.

    import java.io.*;
    import java.util.*;
    import javax.comm.*;
    
    public class SimpleRead implements Runnable, SerialPortEventListener {
        static CommPortIdentifier portId;
        static Enumeration portList;
    
        InputStream inputStream;
        SerialPort serialPort;
        Thread readThread;
    
        public static void main(String[noparse][[/noparse]] args) {
            portList = CommPortIdentifier.getPortIdentifiers();
    
            while (portList.hasMoreElements()) {
                portId = (CommPortIdentifier) portList.nextElement();
                if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                     if (portId.getName().equals("COM1")) {
                //                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);
            try {
                serialPort.setSerialPortParams(9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
            } catch (UnsupportedCommOperationException e) {System.out.println(e);}
            readThread = new Thread(this);
            readThread.start();
        }
    
        public void run() {
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {System.out.println(e);}
        }
    
        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:
                byte[noparse][[/noparse]] readBuffer = new byte[noparse][[/noparse]20];
    
                try {
                    while (inputStream.available() > 0) {
                        int numBytes = inputStream.read(readBuffer);
                    }
                    System.out.print(new String(readBuffer));
                } catch (IOException e) {System.out.println(e);}
                break;
            }
        }
    }
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
Sign In or Register to comment.