package comm.rtx;
import java.util.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
//import org.eclipse.swt.*;
import gnu.io.*;
//import gnu.io.SerialPortEventListener;
public class Form implements SerialPortEventListener{
public JFrame f= new JFrame("串口");
public Container con = f.getContentPane();
public Toolkit toolkit= Toolkit.getDefaultToolkit();
public Dimension sc= toolkit.getScreenSize();
public JLabel label1= new JLabel("串口号:");
public JLabel label2= new JLabel("波特率:");
public JLabel label3= new JLabel("数据位:");
public JLabel label4= new JLabel("校验位:");
public JLabel label5= new JLabel("停止位:");
public JLabel label6= new JLabel("接收区:");
public JLabel label7= new JLabel("发送数据:");
public JLabel status= new JLabel("la");
public JTextArea textArea1= new JTextArea();
public JTextArea textArea2= new JTextArea(); //fasongshuju
// String[] str = new String[] { "COM1", "COM2","COM3","COM4","COM5"};
String[] parIty = new String[] { "NONE", "OLD", "EVEN", "MARK", "SPACE" };
Integer[] baudrateCode = new Integer[] { 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200 };
Integer[] DATA = new Integer[] { 6,7,8 };
Integer[] STOP = new Integer[] { 1,2 };
public JComboBox<String> jComboBox1= new JComboBox<String>();//串口号
public JComboBox<Integer> jComboBox2= new JComboBox<Integer>(baudrateCode);//波特率
public JComboBox<Integer> jComboBox3= new JComboBox<Integer>(DATA);//数据位
public JComboBox<String> jComboBox4= new JComboBox<String>(parIty);//校验位
public JComboBox<Integer> jComboBox5= new JComboBox<Integer>(STOP);//停止位
JButton button1= new JButton("Open");//打开串口按钮
JButton button2= new JButton("Close");//关闭串口按钮
JButton button3= new JButton("Send");
JButton button4= new JButton("Exit");
JPanel p1 = new JPanel();
private Enumeration portList;
private CommPortIdentifier portId;
private OutputStream outputStream;
private InputStream inputStream;
private SerialPort serialPort;
private Timer TimerDisplay;
// private byte[] readRxBuffer = new byte[2048];
// private int readRxCount = 0;
public Form()
{
Initialize();
Load();
Run();
// CommPortInit();
Event();
}
public void Initialize()
{
//f.setVisible(true);
con.setLayout(null);
f.setSize(sc.width/4,sc.height*10/25);
f.setLocation(sc.width/3,sc.height/4);
con.setLayout(null);
f.setSize(sc.width/4,sc.height*10/25);
f.setLocation(sc.width/3,sc.height/4);
Border border= BorderFactory.createEtchedBorder(Color.gray, Color.black);
Border borde= BorderFactory.createTitledBorder(border, "串口设置", TitledBorder.LEFT, TitledBorder.TOP,
new Font("楷体", Font.BOLD, 14));
p1.setBorder(borde);
p1.setLayout (new GridLayout (0, 2, 5, 10));
//p1.setForeground(Color.red);
p1.setBounds(5, 5, 150, 200);
f.setResizable(false);
///label1.setBounds(5, 5, 50, 50);
label1.setForeground(Color.LIGHT_GRAY);
label2.setForeground(Color.blue);
label3.setForeground(Color.blue);
button1.setForeground(Color.green);
button1.setForeground(Color.red);
con.add(p1);
p1.add(label1);
p1.add(jComboBox1);
p1.add(label2);
p1.add(jComboBox2);
p1.add(label3);
p1.add(jComboBox3);
p1.add(label4);
p1.add(jComboBox4);
p1.add(label5);
p1.add(jComboBox5);
p1.add(button1);
p1.add(button2);
// con.add(label1);
status.setBounds(2, 180, 200, 100);
label6.setBounds(160, 2, 80, 50);
label7.setBounds(0, 250, 80, 50);
textArea1.setBounds(160, 50, 300, 150);
textArea2.setBounds(60, 250, 300, 100);
button3.setBounds(100, 350, 70, 30);
button4.setBounds(180, 350, 70, 30);
con.add(label6);
con.add(label7);
con.add(textArea1);
con.add(textArea2);
con.add(button3);
con.add(button4);
// textArea1.add(scroll);
// con.add(scroll);
TimerDisplay = new Timer();
con.add(status);
jComboBox1.setEnabled(true);
jComboBox2.setEnabled(true);
jComboBox3.setEnabled(true);
jComboBox4.setEnabled(true);
jComboBox5.setEnabled(true);
button1.setEnabled(true);
button2.setEnabled(false);
// buttonSend.setEnabled(false);
}
public void Load()
{
CommPortInit();
// jComboBox1.setSelectedIndex(1);
}
public void Run()
{
f.setVisible(true);
while (!f.isDisplayable())
{
con.addNotify();
}
this.Close();
}
public void Close() {//关闭及卸载事件处理过程
if (serialPort != null)
serialPort.close();// 关闭串口
TimerDisplay.cancel();
// 显式地调用dispose() 方法来释放程序运行中所获得的资源
}
public void Event()
{
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
portList=CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements())
{
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL)
{
if(portId.getName().equals(jComboBox1.getSelectedItem().toString()))
{
CommportOpen();
}
}
}
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (serialPort != null) {
status.setText("系统提示: " + "串口"
+ serialPort.getName() + "被关闭!!!");
serialPort.close();// 关闭串口
jComboBox1.setEnabled(true);
jComboBox2.setEnabled(true);
jComboBox3.setEnabled(true);
jComboBox4.setEnabled(true);
jComboBox5.setEnabled(true);
button1.setEnabled(true);
button2.setEnabled(false);
}
}
});
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(JOptionPane.showConfirmDialog(null, "是否退出")==JOptionPane.OK_OPTION)
{
System.exit(1);
}
}
});
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
if(JOptionPane.showConfirmDialog(null, "是否退出")==JOptionPane.OK_OPTION)
{
System.exit(1);
}
else
{
return;
}
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(serialPort!=null)
{
try {
if(textArea2.getText().length()>0)
{
outputStream.write(textArea2.getText().getBytes());
- 1
- 2
前往页