日期:2014-05-20  浏览次数:20773 次

求做过串口通信的帮忙看一下

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Master1View extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField equiNum = new JTextField();
private JButton sendButton;
private JButton receiveButton;
private Master1 master = new Master1();
public Master1View(){
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
sendButton = new JButton("Send");
sendButton.setToolTipText("向Slave 发送告警查询报文");
this.setLayout(null);
sendButton.setBounds(100,300,100,100);
this.add(sendButton);
receiveButton = new JButton("Receive");
receiveButton.setToolTipText("接收Slave 发送过来的报文");
receiveButton.setBounds(250,300,100,100);
this.add(receiveButton);
JLabel equiLabel = new JLabel("请输入设备号(十六进制):");
equiLabel.setBounds(50,500,200,100);
this.add(equiLabel);
equiNum.setBounds(200,530,100,40);
master.setNumString(equiNum.getText().trim());
this.add(equiNum);
sendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
master.send();
}
});
receiveButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
master.receive();
}
});
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
new MasterView();
}
});
}
}

上面是第一个文件

import java.io.*;
import java.util.*;
import javax.comm.*;
public class SerialCommunication implements SerialPortEventListener{
private static CommPortIdentifier portId;
private static Enumeration portList;
private static SerialPort serialPort;
private static OutputStream outputStream;
private static InputStream  inputStream;
private byte[]sendMessage ; 
private byte[] replyMessage;
private byte[] readBuffer[];
private int count = 0; //用来为一次读取到的数据计数
public void setReadBuffer(byte[] readBuffer){
this.readBuffer = this.readBuffer;
}
public SerialPort getSerialPort(){
return this.serialPort;
}
public void setSendMessage(byte []sendMessage){
this.sendMessage = sendMessage;
}
public byte[] getReplyMessage(){
return replyMessage;
}
/**
 * initial 函数主要用来打开端口及初始化端口的各个参数,如波特率,初始数据为等等。
 */
public void initial(String commPort,int port,int baudRate,int Databits,int Stopbits,int parity){
portList = CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements()){
portId = (CommPortIdentifier)portList.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL && portId.getName().equals(commPort)){
try{
serialPort = (SerialPort)portId.open("SerialCommunication", port);
}catch(PortInUseException e){
e.printStackTrace();
}
try{
outputStream = serialPort.getOutputStream();
}catch(IOException e){
e.printStackTrace();
}
try{
serialPort.setSerialPortParams(baudRate, Databits, Stopbits, parity);
}catch(UnsupportedCommOperationException e){
e.printStackTrace();
}

}
}
}
/**
 * receive 函数主要用来监听端口,以及接收slave发送过来的报文
 * 
 */
public void receive(){
try{
try{
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
}catch(TooManyListenersException e){
e.printStackTrace();
}
}finally{
serialPort.close();
}
}
/**
 * Send 函数用来发送报文给Master
 */
public void send(){
try{
outputStream.write(sendMessage);
outputStream.flush(); 
System.out.println("Send Over!");

}catch(IOException e){
e.printStackTrace();
}finally{
try{
outputStream.close();
serialPort.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
@Override
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()){
case SerialPortEvent.BI: break;/* break interrupt 通讯中断 */
case SerialPortEvent.OE: break;/* Overrun error 溢位错误 */
case SerialPortEvent.FE: break;/* Framing error 传帧错误*/
case SerialPortEvent.PE: break;/* Parity error 校验错误 */
case SerialPortEvent.DATA_AVAILABLE: {/*Data avaliable at the serial port 端口有可用数据,读到缓冲数组,输出到终端*/
int numBytes = 0;
byte []readBuffer = new byte[1024];