Java通过得到的Socket流向串口发送数据

我是一个菜鸟,最近学习Java ,写了个向串口发送数据的代码,有点问题,希望各位大神给指点指点

 package server;

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.TooManyListenersException;

import javax.lang.model.element.Parameterizable;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

public class server extends Thread implements SerialPortEventListener  {

    static Enumeration portList;    //已经连接上的端口枚举
    static CommPortIdentifier portId;  //串口通信管理类
    static SerialPort serialPort;        //串口的引用
    static OutputStream outputStream;
    static InputStream ins;
    static int count = 0;

    @SuppressWarnings("null")
    public static void main(String[] args) throws IOException {

        final ServerSocket s = new ServerSocket(12345);
        InetAddress addr = InetAddress.getLocalHost();
        System.out.println("Ip地址为:"+addr.getHostAddress());

        SerialPort port = PortsManager.getSerilPort("COM3", 2400);
        if(null==port) {
            System.out.println("串口为空!");
            return;
        }
        try {
            port.setSerialPortParams(2400,SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,SerialPort.PARITY_SPACE);
        } catch (UnsupportedCommOperationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        OutputStream outs = port.getOutputStream();
        Socket ss = s.accept();
        InputStream ins = ss.getInputStream();
        byte[] b = new byte[8];
        int len = -1;
        while((len = ins.read(b))!= -1){
            count++;
            System.out.println("Socket 连接成功!");
            System.out.println(b.toString());
            if(count>2){  
                outs.write(b);  
            }  

        }

     }

     public void run(){
         Thread th=new Thread();
         th.start();
         try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }

    //@Override
    public void serialEvent(SerialPortEvent arg0) {
         //TODO Auto-generated method stub
        try {
            serialPort.addEventListener(this);//注册一个SerialPortEventListener事件来监听串口事件
            serialPort.notifyOnDataAvailable(true);//设置监听模式为当有数据到达时唤醒监听线程。
        } catch (TooManyListenersException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}



查看全部
sinat_28519535
玻璃心orz
2015/05/27 02:47
  • 串口通信
  • java
  • socket
  • 点赞
  • 收藏
  • 回答
    私信

1个回复