虚拟串口工具的使用

1.(comm包的下载以及配置就不说了,这里主要说工具的使用)

下载VSPD(创建虚拟串口并监视通信流量的工具,用它可以创建一对虚拟串口,例如com2-com3

,那么可以用com2发数据,com2将数据发给com3,我们可以从com3接收数据)

2.下载com模拟工具(LP-COMM,或Commix,或BoastSerialTool)

这些工具不是必须的,他可以连接某个com口,你可以把数据填写到输入框,点击发送按钮,就把数据发送了。

这样比较方便,一般情况下,我们用程序发送数据,但为了调试,每次都启动程序很麻烦,这样就比较方便。

但这些工具都不能接受数据!你想看看发的数据到底发送成功没有,需要写一个从comm口读取数据的程序,

先启动它,当你用上述工具发送时,可以看到控制台输出的数据(当然前提时你的程序里有输出语句)

3.从VSPD也可以看出数据发送了,只是它只显示流量,比如从com2发送了5kb的数据,com3接受了5kb的数据

,根据这个也可以判断数据的走向,只是具体发的什么你不清楚。所以还是建议写一个读取数据的程序,

并将读取的内容在控制台打印出来,比较直观。下面附上两个程序,可供使用:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.mypro;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;

public class CommWrite {
 
 static Enumeration portList;
    static CommPortIdentifier portId;
    static String messageString = "Hello, world!\n";
    static SerialPort serialPort;
    static OutputStream outputStream;

    public static void main(String[] args) {
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (portId.getName().equals("COM2")) {
                    try {
                        serialPort = (SerialPort)
                            portId.open("SimpleWriteApp", 2000);
                    } catch (PortInUseException e) {}
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {}
                    try {
                        serialPort.setSerialPortParams(9600,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {}
                    try {
                     for(int i=0;i<1000;i++){
                      messageString+="the time "+i+"--\n"+messageString+"\n";
                      outputStream.write(messageString.getBytes());
                     }
                       
                    } catch (IOException e) {}
                }
            }
        }
    }

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.mypro;


import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

public class CommRead implements  SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration portList;

    static InputStream inputStream;
    static SerialPort serialPort;
    Thread readThread;
   
    public CommRead(){
     init();
    }

   
   
    private void init() {

        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (portId.getName().equals("COM3")) {
                 
                  try {
                   serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
                     } catch (PortInUseException e) {}
                    
                     //加串口事件
               try {
                          serialPort.addEventListener(this);
               } catch (TooManyListenersException e) {}
               //获取输入流
                    try {
                     inputStream = serialPort.getInputStream();
                    } catch (IOException e) {}
                     serialPort.notifyOnDataAvailable(true);
                     //设置参数
                     try {
                         serialPort.setSerialPortParams(9600,
                             SerialPort.DATABITS_8,
                             SerialPort.STOPBITS_1,
                             SerialPort.PARITY_NONE);
                     } catch (UnsupportedCommOperationException e) {}
                }
            }
        }
       
 }

 

 //串口事件
    public void serialEvent(SerialPortEvent event) {
        switch(event.getEventType()) {
        case SerialPortEvent.BI:{System.out.println("SerialPortEvent.BI");}
        case SerialPortEvent.OE:{System.out.println("SerialPortEvent.OE");}
        case SerialPortEvent.FE:{System.out.println("SerialPortEvent.FE");}
        case SerialPortEvent.PE:{System.out.println("SerialPortEvent.PE");}
        case SerialPortEvent.CD:{System.out.println("SerialPortEvent.CD");}
        case SerialPortEvent.CTS:{System.out.println("SerialPortEvent.CTS");}
        case SerialPortEvent.DSR:{System.out.println("SerialPortEvent.DSR");}
        case SerialPortEvent.RI:{System.out.println("SerialPortEvent.RI");}
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;
           
        case SerialPortEvent.DATA_AVAILABLE:
            byte[] readBuffer = new byte[200];

            try {
                while (inputStream.available() > 0) {
                    int numBytes = inputStream.read(readBuffer);
                //    System.out.println("-----"+numBytes);
                    System.out.print(new String(readBuffer));
                }
             //   System.out.print(new String(readBuffer));
            } catch (IOException e) {}
            break;
        }
       
    }
   
   
   
    public static void main(String[] args) {
     CommRead cc=new CommRead();
    }
}

 

 

 

你可能感兴趣的:(虚拟串口工具的使用)