Skoči na vsebino

IPB








Slika

Java In Serial Port

Objavil boss-tech , 22 julij 2009 · 1 928 views

Kako komunicirati z napravami preko serijskega porta ter USBja?

Najprej sem mal googlal, vendar nič pametnega... najdem java comm od sun-a, pridem lepo do downloada knjižnice - stran ne obstaja. Potem pa le najdem en forum, kjer predlagajo knjižnico RXTX, ki jo najdeš na  http://users.frii.com/jarvi/rxtx oz download na http://rxtx.qbang.org/wiki
Ko stvar preneseš, jo razpakiraš in dobiš knjižnice za win, linux, macos ter solaris + jar datoteka.

Jaz sem delal v win, tako da sem uporabil win dllje ter jar file. Prekopirat pa jih moraš v:

Installation of javax.comm on Linux

copy libLinuxSerialParallel.so to /usr/lib/
copy javax.comm.properties to [JDK-directory]/jre/lib/
copy comm.jar to [JDK-directory]/lib/

Installation of rxtxSerial (from www.rxtx.org) on Linux

copy librxtxSerial.so to /usr/lib
copy RXTXcomm.jar to [JDK-directory]/jre/lib/ext/

Installation of rxtxSerial (from www.rxtx.org) on Windows

copy rxtxSerial.dll to [JDK-directory]\jre\bin\rxtxSerial.dll
copy RXTXcomm.jar to [JDK-directory]\jre\lib\ext\RXTXcomm.jar

Ko imaš te knjižnice pa lahko začneš z delom. Jar file je potrebno še dodati v library v projektu v javi (jaz uporabljam eclipse). Potem pa lahko začnemo s kodo:

Main class

private void connect(String portName) throws Exception {
      
        try {
            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(115200, SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                    System.out.println("Device connected...");  
                                                      
                    InputStream in = serialPort.getInputStream();
                    SerialReader reader = new SerialReader(in);
                    reader.run();
                                      
                    OutputStream out3 = serialPort.getOutputStream();
                    SerialWriter writer = new SerialWriter(out3, serialPort);              
                
                    writer.setCommand("xxx");                
                    writer.run();
                  
                  
                    //close comport
                    try{
                        commPort.close();
              
                           }
                           catch (Exception e) {
                               System.out.println("Close port error "+ e);
                           }
                } else
                    System.out
                            .println("Error: Only serial ports are handled by this example.");
                }
          
        } catch (Exception conn) {
            System.out.println("Error connecting:"+conn);
          
        }

    }


Branje iz naprave:

import java.io.IOException;
import java.io.InputStream;

    public class SerialReader implements Runnable
    {
        InputStream in;
      
        public SerialReader ( InputStream in )
        {
            this.in = in;
        }
      
        public void run ()
        {
            byte[] buffer = new byte[1024];
            int len = -1;
            try
            {
                while ( ( len = this.in.read(buffer)) > -1 )
                {
                    System.out.print(new String(buffer,0,len));
                }
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }          
        }
    }


Pisanje v napravo oz pošiljanje komand:

  public void run ()
        {
            System.out.println("Insert command: ");
                        
              try {                
                  serialPort.notifyOnOutputEmpty(true);
               } catch (Exception e) {
                  System.out.println("Error setting event notification");
                  System.out.println(e.toString());                
               }
                              
               System.out.println("Writing \""+messageString+"\" to "+serialPort.getName());
               try {
                  //write to serial port
                  out.write(messageString.getBytes());
                  out.close();
              
               } catch (IOException e) {}  
        }


Blog najdete tudi na dotnet blog




Thanks for sharing this nice information. I appreciated your all post. All are full informative mcdba and useful for blogger. If all bloggers offered the same content as you, the internet would be a much better place. Keep it up.
  • Poročilo

Tags

  • Črna Lista

Tags

  • Črna Lista