如题,现在我已经实现串口的收发数据与坐标的显示,问题是如何每次得到一个数据就显示在坐标上然后画成折线,我知道要用到线程,我考虑在读数据那个类中创建一个线程用来把读到的数据显示在坐标上并连成折线,但如何实施却一头雾水,还望指教!
class R_Frame extends Frame implements Runnable,ActionListener,SerialPortEventListener{ 
/* 检测系统中可用的通讯端口类 */
static CommPortIdentifier portId; 
/* Enumeration 为枚举型类,在java.util中 */
static Enumeration portList; 
InputStream inputStream; 
/* 声明RS-232串行端口的成员变量 */
SerialPort serialPort; 
Thread readThread; 
String str=""; 
TextField out_message=new TextField("下面显示接收到的数据"); 
TextArea in_message=new TextArea(); 
Button btnOpen=new Button("打开串口");  /*建立窗体*/
R_Frame()
{
super("串口接收数据"); 
setSize(200,200); 
setVisible(true); 
btnOpen.addActionListener(this); 
add(out_message,"North"); 
add(in_message,"Center"); 
add(btnOpen,"South"); 
} //R_Frame() end /*点击按扭所触发的事件:打开串口,并监听串口. */
public void actionPerformed(ActionEvent event){
/*获取系统中所有的通讯端口 */
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("ReadComm", 2000); 
out_message.setText("已打开端口COM2 ,正在接收数据..... "); 
}
catch (PortInUseException e) { }

/*设置串口监听器*/
try {
serialPort.addEventListener(this); 

catch (TooManyListenersException e) { }
/* 侦听到串口有数据,触发串口事件*/
serialPort.notifyOnDataAvailable(true); 
} //if end
} //if end
} //while end
readThread = new Thread(this); 
readThread.start(); //线程负责每接收一次数据休眠20秒钟
} //actionPerformed() end /*接收数据后休眠20秒钟*/
public void run() {
try {
Thread.sleep(20000); 

catch (InterruptedException e) { }
} //run() end /*串口监听器触发的事件,设置串口通讯参数,读取数据并写到文本区中*/
public void serialEvent(SerialPortEvent event) {
/*设置串口通讯参数:波特率、数据位、停止位、奇偶校验*/
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE); 
}
catch (UnsupportedCommOperationException e) {}
byte[] readBuffer = new byte[20]; 
String readStr="";
int numBytes=0;

try {
inputStream = serialPort.getInputStream(); 
}
catch (IOException e) {}
try {
/* 从线路上读取数据流 */
while (inputStream.available() > 0) { 
numBytes = inputStream.read(readBuffer); 
} //while end
str=new String(readBuffer); 
for(int iii=0;iii<numBytes;iii++){   
              readStr=readStr + Byte.toString(readBuffer[iii]);                  
        } 

/*接收到的数据存放到文本区中*/
in_message.append(str);
in_message.append("\n");
Graphics g2=getGraphics();
Mov m=new Mov(g2);
m.start();
   }
catch (IOException e) { }
} //serialEvent() end
      /*这个是画椭圆我测试用的*/
class Mov extends Thread{
     Ellipse2D.Double ec;
     int xb=100,yb=50;
     Graphics2D g2d;
     
     public Mov(Graphics g)
     {
         g2d=(Graphics2D)g;
     }
     
     public void run(){
      try {
      while(xb<=300){
             ec=new Ellipse2D.Double(xb,yb,100,100);
             g2d.draw(ec);
             for(int i=0;i<1000;i++){}
             xb++;yb++;
             Thread.sleep(100);           
      }
      }catch (InterruptedException e) {
         }
    }
}
} //类R_Frame endpublic class ReadComm{
public static void main(String args[]){
/* 实例化接收串口数据的窗体类 */
R_Frame R_win=new R_Frame(); 
Histogram demo=new Histogram(); /*这个是我做的坐标类*/
demo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

/* 定义窗体适配器的关闭按钮功能 */
R_win.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0); }
}); 
R_win.pack(); 
}
}

解决方案 »

  1.   

    步骤都说了
    LZ照着来吧其实像这种模式,vb里也有
    可以参看一下那里的代码而且这个vb做会不会更简单点啊
      

  2.   

    没必要用线程吧
    可以按时间去得到坐标点 
    不然太多也没什么意义吧如果图多则可以考虑多线程 
    又在发明轮子
    为啥不用 jfreechar 
    还有很多 swf 开源图表 漂亮的一塌糊涂接口很简单的