Page 422 - MDP2020-3
P. 422

416
                      |    인천전자마이스터고등학교  ·············································································································

            -  Serial.java  :  미사용  시리얼  포트  탐색  및  연결
            import  java.io.InputStream;

            import  java.io.OutputStream;
            import  gnu.io.CommPort;
            import  gnu.io.CommPortIdentifier;
            import  gnu.io.SerialPort;
            public  class  Serial  {

                    public  Serial()  {
                            super();
                    }
                    void  connect(String  portName)  throws  Exception  {
                            CommPortIdentifier                      portIdentifier                          =

            CommPortIdentifier.getPortIdentifier(portName);
                            if  (portIdentifier.isCurrentlyOwned())  {
                                    System.out.println("Error:  Port  is  currently  in  use");
                            }  else  {
                                    CommPort  commPort  =  portIdentifier.open(this.getClass().getName(),  2000);

                                    if  (commPort  instanceof  SerialPort)  {
                                            SerialPort  serialPort  =  (SerialPort)  commPort;
                                            serialPort.setSerialPortParams(9600,   SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
                                                            SerialPort.PARITY_NONE);

                                            InputStream  in  =  serialPort.getInputStream();
                                            OutputStream  out  =  serialPort.getOutputStream();
                                            (new  Thread(new  SerialReader(in))).start();
                                            (new  Thread(new  SerialWriter(out))).start();
                                    }  else  {

                                            System.out.println("Error:  Only  serial  ports  are  handled  by  this
            example.");
                                    }
                            }
                    }

            }
   417   418   419   420   421   422   423   424   425   426   427