乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java 串口通信

       昵稱2807 2007-04-29
       java 串口通信


      /******************************************
      * 程序文件名稱:SendComm.java
      *  功能:從串行口COM1中發(fā)送數(shù)據(jù)
      ******************************************/
      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
      import java.util.*;
      import javax.comm.*;

       class S_Frame extends Frame implements Runnable,ActionListener
      {
        /*檢測系統(tǒng)中可用的通訊端口類 */
        static CommPortIdentifier      portId;
        /*Enumeration 為枚舉型類,在util中  */
        static Enumeration             portList;
        OutputStream                   outputStream;
        /*RS-232的串行口  */
        SerialPort                     serialPort;   
        Thread                         readThread;
        Panel                          p=new Panel();
        TextField in_message=new  TextField("打開COM1,波特率9600,數(shù)據(jù)位8,停止位1.");
        TextArea  out_message=new TextArea();
        Button    btnOpen=new Button("打開串口,   發(fā)送數(shù)據(jù)");
        Button    btnClose=new Button("關(guān)閉串口, 停止發(fā)送數(shù)據(jù)");
        byte      data[]=new byte[10240];
          /*設(shè)置判斷要是否關(guān)閉串口的標志*/
        boolean   mark;

       /*安排窗體*/
       S_Frame()
       { super("串口發(fā)送數(shù)據(jù)");
         setSize(200,200);
         setVisible(true);
         add(out_message,"Center");
         add(p,"North");
         p.add(btnOpen);
         p.add(btnClose);
         add(in_message,"South");
         btnOpen.addActionListener(this);
         btnClose.addActionListener(this);
       } //R_Frame() end

       /*點擊按扭打開串口.*/
       public void actionPerformed(ActionEvent event) {
       if (event.getSource()==btnClose){
            serialPort.close();//關(guān)閉串口
            mark=true;  //用于中止線程的run()方法
              in_message.setText("串口COM1已經(jīng)關(guān)閉,停止發(fā)送數(shù)據(jù).");
         }
       else {  mark=false;
           /*從文本區(qū)按字節(jié)讀取數(shù)據(jù)*/
           data=out_message.getText().getBytes();
              /*打開串口*/
           start();
              in_message.setText("串口COM1已經(jīng)打開,正在每2秒鐘發(fā)送一次數(shù)據(jù).....");
            }
       } //actionPerformed() end

        /*打開串口,并調(diào)用線程發(fā)送數(shù)據(jù)*/
       public void start(){
        /*獲取系統(tǒng)中所有的通訊端口  */
        portList=CommPortIdentifier.getPortIdentifiers();
        /* 用循環(huán)結(jié)構(gòu)找出串口 */
        while (portList.hasMoreElements()){ 
         /*強制轉(zhuǎn)換為通訊端口類型*/
          portId=(CommPortIdentifier)portList.nextElement();
          if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
            if (portId.getName().equals("COM1")) {
               /*打開串口 */
              try {
      serialPort = (SerialPort) portId.open("ReadComm", 2000);
                }
      catch (PortInUseException e) {  }
                 /*設(shè)置串口輸出流*/
              try {
      outputStream = serialPort.getOutputStream();
                 }
      catch (IOException e) {}
            } //if end
           } //if end
         } //while end
         /*調(diào)用線程發(fā)送數(shù)據(jù)*/
        try{
           readThread = new Thread(this);
          //線程負責每發(fā)送一次數(shù)據(jù),休眠2秒鐘
       readThread.start();
      }
      catch (Exception e) {  }
       }  //start() end
       
        /*發(fā)送數(shù)據(jù),休眠2秒鐘后重發(fā)*/
        public void run() {
          /*設(shè)置串口通訊參數(shù)*/
          try {
               serialPort.setSerialPortParams(9600,
                      SerialPort.DATABITS_8,
                      SerialPort.STOPBITS_1,
                      SerialPort.PARITY_NONE);
              }
       catch (UnsupportedCommOperationException e) {  }
              /*發(fā)送數(shù)據(jù)流(將數(shù)組data[]中的數(shù)據(jù)發(fā)送出去)*/
        try {
      outputStream.write(data);
         }
      catch (IOException e) {  }
              /*發(fā)送數(shù)據(jù)后休眠2秒鐘,然后再重發(fā)*/
        try { Thread.sleep(2000);
             if (mark)
             {return;   //結(jié)束run方法,導致線程死亡
             }
             start();
            }
             catch (InterruptedException e) {  }
         }  //run() end
      }  //類S_Frame end

      public class SendComm
      {public static void main(String args[])
       { S_Frame S_win=new S_Frame();
         S_win.addWindowListener(new WindowAdapter()
           {public void windowClosing(WindowEvent e)
            {System.exit(0);   }
           });
         S_win.pack();
       }
      }

       

       

      /******************************************
      * 程序文件名稱:ReadComm.java
      *  功能:從串行口COM1中接收數(shù)據(jù)
      ******************************************/
      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
      import java.util.*;
      import javax.comm.*;

       class R_Frame extends Frame implements Runnable,ActionListener,SerialPortEventListener
      {
        /*  檢測系統(tǒng)中可用的通訊端口類 */
         static CommPortIdentifier     portId;
           /*  Enumeration 為枚舉型類,在java.util中  */
         static Enumeration            portList;
         InputStream                   inputStream;
           /*  聲明RS-232串行端口的成員變量  */
         SerialPort     serialPort;   
         Thread         readThread;
         String         str="";
         TextField      out_message=new TextField("上面文本框顯示接收到的數(shù)據(jù)");
         TextArea       in_message=new TextArea();
         Button         btnOpen=new Button("打開串口");

       /*建立窗體*/
       R_Frame()
       {
       super("串口接收數(shù)據(jù)");
         setSize(200,200);
         setVisible(true);
         btnOpen.addActionListener(this);
         add(out_message,"South");
         add(in_message,"Center");
         add(btnOpen,"North");
       } //R_Frame() end

       /*點擊按扭所觸發(fā)的事件:打開串口,并監(jiān)聽串口. */
       public void actionPerformed(ActionEvent event)
       {
        /*獲取系統(tǒng)中所有的通訊端口  */
        portList=CommPortIdentifier.getPortIdentifiers();
        /* 用循環(huán)結(jié)構(gòu)找出串口 */
        while (portList.hasMoreElements()){ 
         /*強制轉(zhuǎn)換為通訊端口類型*/
          portId=(CommPortIdentifier)portList.nextElement();
          if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
            if (portId.getName().equals("COM1")) {
              try {
      serialPort = (SerialPort) portId.open("ReadComm", 2000);
             out_message.setText("已打開端口COM1 ,正在接收數(shù)據(jù)..... ");
            }
        catch (PortInUseException e) { }

           /*設(shè)置串口監(jiān)聽器*/
           try {
      serialPort.addEventListener(this);
      }
              catch (TooManyListenersException e) { }
              /* 偵聽到串口有數(shù)據(jù),觸發(fā)串口事件*/
           serialPort.notifyOnDataAvailable(true);
            } //if end
           } //if end
         } //while end
         readThread = new Thread(this);
         readThread.start();//線程負責每接收一次數(shù)據(jù)休眠20秒鐘
       } //actionPerformed() end
       
        /*接收數(shù)據(jù)后休眠20秒鐘*/
         public void run() {
              try {
      Thread.sleep(20000);
      }
              catch (InterruptedException e) {  }
         }  //run() end

            /*串口監(jiān)聽器觸發(fā)的事件,設(shè)置串口通訊參數(shù),讀取數(shù)據(jù)并寫到文本區(qū)中*/
         public void serialEvent(SerialPortEvent event) {
         /*設(shè)置串口通訊參數(shù):波特率、數(shù)據(jù)位、停止位、奇偶校驗*/
            try {
                 serialPort.setSerialPortParams(9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
      }
       catch (UnsupportedCommOperationException e) {   }
        byte[] readBuffer = new byte[20];
           try {
      inputStream = serialPort.getInputStream();
      }
              catch (IOException e) {}
      try {
         /* 從線路上讀取數(shù)據(jù)流 */
               while (inputStream.available() > 0) {
                     int numBytes = inputStream.read(readBuffer);
                   }    //while end
         str=new String(readBuffer);
               /*接收到的數(shù)據(jù)存放到文本區(qū)中*/
         in_message.append(str+"\n");
        }
       catch (IOException e) {    }
         } //serialEvent() end
      }  //類R_Frame end

      public class ReadComm
      {
      public static void main(String args[])
        {
          /*  實例化接收串口數(shù)據(jù)的窗體類  */
       R_Frame R_win=new R_Frame();
      /*  定義窗體適配器的關(guān)閉按鈕功能 */
           R_win.addWindowListener(new WindowAdapter()
                   {public void windowClosing(WindowEvent e)
                      {System.exit(0); }
            });
         R_win.pack();
       }
      }


       



      Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1588546

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多