读书人

经过串口让普通的电路开关控制你的jav

发布时间: 2012-08-27 21:21:57 作者: rapoo

通过串口让普通的电路开关控制你的java程序



根据脚踏开关的两种类型采用不同的方式

1.带上位机的
该方式比较简单,当开关的闭合触发事件时上位机会通过串口向pc发送数据,在程序中采用串口的事件监听模型即可进行事件的捕捉,主要代码如下:

public class Demo {    static SwitchControl control;    private InputStream in;    private OutputStream os;    private SerialPort serialPort;    boolean inited = false;    boolean stoped = false;    long beg = System.currentTimeMillis();    public boolean init() {        if (inited) {            return true;        }        try {            CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM1");            serialPort = (SerialPort) portId.open("SerialBean", 4000);            serialPort.setSerialPortParams(9600,                    SerialPort.DATABITS_8,                    SerialPort.STOPBITS_1,                    SerialPort.PARITY_NONE);            serialPort.setDTR(true);            in = serialPort.getInputStream();            os = serialPort.getOutputStream();            inited = true;                   } catch (Exception e) {            e.printStackTrace();            return false;        }        return true;    }    public void writePort(int s) {        try {            os.write(s);            os.flush();        } catch (Exception e) {            e.printStackTrace();        }    }    public void start() {        Thread send = new Thread(new SendData());        Thread recive = new Thread(new ReceiveData());        send.start();        recive.start();    }    public void close() {        stoped = true;        try {            Thread.sleep(20);            if (in != null) {                in.close();            }            if (os != null) {                os.close();            }            if (serialPort != null)                serialPort.close();        } catch (Exception e) {            e.printStackTrace();        }    }    class ReceiveData implements Runnable {        public void run() {            while (true) {                if (stoped) {                    break;                }                byte[] b = new byte[10];                try {                    if (in.read(b) == 0) {                        continue;                    }                    long end = System.currentTimeMillis();                    //处去噪声影响,通过连续两次接受到数据的时间差来判断是一次长时间的踩踏还是连续2次或者多次踩踏                    if (end - beg > 20) {                        Thread tt = new Thread(new Task());                        tt.start();                    }                    Thread.sleep(10);                    beg = System.currentTimeMillis();                } catch (Exception e) {                    break;                }            }        }    }    class SendData implements Runnable {        public void run() {            while (true) {                if (stoped) {                    break;                }                writePort(0);                try {                    Thread.sleep(5);                } catch (Exception e) {                    e.printStackTrace();                    break;                }            }        }    }    class Task implements Runnable {        public void run() {            // 相关任务            System.out.println("捕捉到事件.............");        }    }    public static void main(String[] args) {        Demo demo = new Demo();        if (!demo.init()) {            System.err.println("初始化串口失败!");            return;        }        demo.start();    }}

?

?

1 楼 mlhorizon 2008-05-06 还是带点编码解码的好!!
防止干扰,同时能实现的功能也多一点!! 2 楼 yangyiqian 2008-05-06 好东东,顶! 3 楼 Unmi 2008-05-08 我们要创新,但是要有价值的创新

就说这东西的实际意义在哪里,现在PC上在慢慢淘汰 COM,某些仪器设备保留下来。 4 楼 walsece 2008-05-09 慢慢淘汰并不代表无用武之地,只不过你不经常接触相应的行业而已,其实现在很多方面都还在用com,这是我在做软件中遇到的实实在在的需求,虽然目前大部分都在使用usb,但是从编程的角度而言两者之间没有太大的区别。

读书人网 >移动开发

热点推荐