⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialbean.java

📁 大家共享愉快, 共享愉快, 共享愉快, 共享愉快,共享愉快
💻 JAVA
字号:
package org.compiere.cti;

import java.io.*;
import java.util.*;
import gnu.io.*;
import org.compiere.util.CLogger;
import java.util.zip.*;

/**
 *
 * This bean provides some basic functions to implement full dulplex
 * information exchange through the srial port.
 *
 */
public class SerialBean {
    static String PortName;
    CommPortIdentifier portId;
    SerialPort serialPort;
    static OutputStream out;
    static InputStream in;
    ReadSerial RT;
    /**	Logger			*/
    protected CLogger log = CLogger.getCLogger(getClass());

    /**
     *
     * Constructor
     *
     * @param PortID the ID of the serial to be used. 1 for COM1,
     * 2 for COM2, etc.
     *
     */
    public SerialBean(int PortID) {
	PortName = "COM" + PortID;
        Initialize();
        WritePort("AT #cid=1\r\n");
        WritePort("AT +vcid=1\r\n");
    }

    /**
     *
     * This function initialize the serial port for communication. It startss a
     * thread which consistently monitors the serial port. Any signal capturred
     * from the serial port is stored into a buffer area.
     *
     */
    public int Initialize() {
        int InitSuccess = 1;
        int InitFail = -1;

        try {
            String outDir = null;
            String libPath = System.getProperty("java.library.path");
            int sys32 = libPath.indexOf(System.getProperty(
                    "file.separator") +
                                        "system32" +
                                        System.getProperty("path.separator"));
            if (sys32 > 0) {
                outDir = libPath.substring(0, sys32);
            } else {
                sys32 = libPath.indexOf(System.getProperty(
                        "file.separator") +
                                        "system" +
                                        System.getProperty("path.separator"));
                if (sys32 > 0)
                    outDir = libPath.substring(0, sys32);
            }
            if (outDir == null) {
                return InitFail;
            }
            outDir = outDir.substring(outDir.lastIndexOf(System.getProperty(
                    "path.separator")) + 1);
            File sysFile = new File(outDir, "rxtxSerial.dll");
            String userPath = System.getProperty("user.dir");

            if (!sysFile.exists()) {
                File f = new File(userPath +
                                  System.getProperty("file.separator") +
                                  "lib", "Serial.jar");
                if (f.exists()) {
                    userPath = userPath + System.getProperty("file.separator") +
                               "lib";
                    Extract(userPath, outDir, f);
		} else {
                    f = new File(userPath, "Serial.jar");
                    if (f.exists()) {
                        Extract(userPath, outDir, f);
                    } else {
                        System.loadLibrary("rxtxSerial");
                        System.loadLibrary("rxtxParallel");
                    }
                }
            }

            Enumeration en = CommPortIdentifier.getPortIdentifiers();

            portId = (CommPortIdentifier) en.nextElement();
            if (portId.getPortType() ==
                CommPortIdentifier.PORT_SERIAL) {
                this.PortName = portId.getName();
                //System.out.println(portId.getName());
            }

            portId = CommPortIdentifier.getPortIdentifier(PortName);

            try {
                serialPort = (SerialPort)
                             portId.open("CompiereONE 2005 CTI", 2000);
            } catch (PortInUseException e) {
                System.out.println(e.getMessage());
                return InitFail;
            }
            //Use InputStream in to read from the serial port, and OutputStream
            //out to write to the serial port.
            try {
                in = serialPort.getInputStream();
                out = serialPort.getOutputStream();
            } catch (IOException e) {
                return InitFail;
            }

            //Initialize the communication parameters to 9600, 8, 1, none.
            try {
                serialPort.setSerialPortParams(9600,
                                               SerialPort.DATABITS_8,
                                               SerialPort.STOPBITS_1,
                                               SerialPort.PARITY_NONE);
            } catch (UnsupportedCommOperationException e) {
                return InitFail;
            }
        } catch (NoSuchPortException e) {
            System.out.println(e.getMessage());
            return InitFail;
	}catch (Exception ex) {
	    return InitFail;
	}


        RT = new ReadSerial(in);
        RT.start();
        return InitSuccess;
    }

    /**
     * Deploy Jar Files
     *
     * @throws Exception
     */
    private void Extract(String baseDir, String outDir, File f) throws
            Exception {
        ZipFile zfile = new ZipFile(f);
        Enumeration zList = zfile.entries();
        ZipEntry ze = null;
        byte[] buf = new byte[1024];
        while (zList.hasMoreElements()) {
            ze = (ZipEntry) zList.nextElement();
            if (ze.isDirectory()) {
                System.out.println("Dir: " + ze.getName() + " skipped..");
                continue;
            }
            if (ze.getName().equals("rxtxSerial.dll") ||
                ze.getName().equals("rxtxParallel.dll")) {
                System.out.println("Extracting: " + ze.getName() + "\t" +
                                   ze.getSize() + "\t" + ze.getCompressedSize());

                OutputStream os = new BufferedOutputStream(new FileOutputStream(
                        getRealFileName(outDir, ze.getName())));
                InputStream is = new BufferedInputStream(zfile.getInputStream(
                        ze));
                int readLen = 0;
                while ((readLen = is.read(buf, 0, 1024)) != -1) {
                    os.write(buf, 0, readLen);
                }
                is.close();
                os.close();
                System.out.println("Extracted: " + ze.getName());
            }
        }
        zfile.close();
    }

    /**
     */
    private File getRealFileName(String baseDir, String absFileName) {
        String[] dirs = absFileName.split("/");
        File ret = new File(baseDir);
        if (dirs.length > 1) {
            for (int i = 0; i < dirs.length - 1; i++) {
                ret = new File(ret, dirs[i]);
            }
        }
        if (!ret.exists()) {
            ret.mkdirs();
        }
        ret = new File(ret, dirs[dirs.length - 1]);
        return ret;
    }

    /**
     *
     * This function sends a message through the serial port.
     *
     * @param Msg The string to be sent.
     *
     */
    public void WritePort(String Msg) {
        int c;
        try {
            if (out != null) {
                out.write(Msg.getBytes());
                out.flush();
            }
        } catch (IOException e) {}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -