麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

一個使用Java讀取串口的程序(1)

2019-11-18 13:24:42
字體:
來源:轉載
供稿:網友

  以下是我寫的用java讀取串口的程序,應一些網友的要求在這里貼出來。這個程序里面還有一些問題,也希望有經驗的網友能夠給我提點意見。
  
  這個簡單的程序包括以下文件:
  
   IMU.java (主程序)
   ReadBuffer.java (從緩沖區讀取一個消息)
   ReadSerial.java (讀取串口數據并放入緩沖區)
   SerialBuffer.java (緩沖區)
   WriteSerial.java (不斷的往串口送星號'*')
  
  測試程序:
  
   SendCom.java (將一個數據文件往串口發送)
   SEND.TXT (供測試用的數據文件)
  
  在這個通訊程序中使用了一個簡單的協議,既不同的消息之間用星號'*'作為分隔。這個程序中的問題是ReadSerial進程和WriteSerial進程不能夠同時啟動,出錯信息是不能夠打開串口,因為同樣一個串口不能夠同時被打開兩次(在ReadSerial中聲明了FileReader
  
  測試程序:
  
   SendCom.java (將一個數據文件往串口發送)
   SEND.TXT (供測試用的數據文件)
  
  在這個通訊程序中使用了一個簡單的協議,既不同的消息之間用星號'*'作為分隔。這個程序中的問題是ReadSerial進程和WriteSerial進程不能夠同時啟動,出錯信息是不能夠打開串口,因為同樣一個串口不能夠同時被打開兩次(在ReadSerial中聲明了FileReader和在WriteSerial中聲明了FileWriter)。這樣是不能夠實現全雙工通訊的。不知道有沒有做過的大俠能夠講講處理的辦法。
  
  /*
  *
  * IMU.java 1.0
  * Main PRogram for Serial Communication
  *
  * Created: March 27, 2001
  *
  * Author : Qingye Jiang (John)
  * American GNC Corporation
  * 888 Easy St, Simi Valley CA 93065-1812
  *
  * Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
  * [email protected]
  *
  */
  
  import java.io.*;
  
  class IMU
  {
   public static void main(String[] args)
   {
   //TO DO: Add your JAVA codes here
  
   File ComPort = new File("COM1");
  
   SerialBuffer SB = new SerialBuffer();
   ReadSerial r1 = new ReadSerial(SB, ComPort);
   ReadBuffer r2 = new ReadBuffer(SB);
   WriteSerial r3 = new WriteSerial(ComPort);
  
   r1.start();
   r2.start();
   r3.start();
   }
  }
  
  /*
  *
  * ReadBuffer.java 1.0
  * Program to Read the Serial Buffer
  *
  * Created: March 27, 2001
  *
  * Author : Qingye Jiang (John)
  * American GNC Corporation
  * 888 Easy St, Simi Valley CA 93065-1812
  *
  * Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
  * [email protected]
  
  *
  */
  
  import java.io.*;
  
  public class ReadBuffer extends Thread
  {
   private SerialBuffer ComBuffer;
  
   public ReadBuffer(SerialBuffer SB)
   {
   ComBuffer = SB;
   }
  
   public void run()
   {
   String Msg;
  
   while (true)
   {
   Msg = ComBuffer.GetMsg();
   System.out.println(Msg);
   }
  
   }
  }
  
  /*
  *
  * ReadSerial.java 1.0
  * Program to read characters from the serial port and put it
  * to the buffer
  *
  * Created: March 27, 2001
  *
  * Author : Qingye Jiang (John)
  * American GNC Corporation
  * 888 Easy St, Simi Valley CA 93065-1812
  *
  * Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
  * [email protected]
  *
  */
  
  import java.io.*;
  
  public class ReadSerial extends Thread
  {
   private SerialBuffer ComBuffer;
   private File ComPort;
  
   public ReadSerial(SerialBuffer SB, File Port)
   {
   ComBuffer = SB;
   ComPort = Port;
   }
  
   public void run()
   {
   int c;
   try
   {
   FileReader in = new FileReader(ComPort);
   while (true)
   {
   c = in.read();
   ComBuffer.PutChar(c);
   }
   try
   {
   FileReader in = new FileReader(ComPort);
   while (true)
   {
   c = in.read();
  
   ComBuffer.PutChar(c);
   }
   } catch (IOException e) {}
   }
  }
  
  
  /*
  *
  * SerialBuffer.java 1.0
  * Class that implements the serial buffer
  *
  * Created: March 27, 2001
  *
  * Author : Qingye Jiang (John)
  * American GNC Corporation
  * 888 Easy St, Simi Valley CA 93065-1812
  *
  * Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
  * [email protected]
  
  *
  */
  
  public class SerialBuffer
  {
   private String Content = "";
   private String CurrentMsg, TempContent;
   private boolean available = false;
  
   public synchronized String GetMsg()
   {
   int SepMark;
  
   if ((SepMark = Content.indexOf('*')) == -1)
  {
   available = false;
   while (available == false)
   {
   try
   {
   wait();
   } catch (InterruptedException e) { }
   }
   SepMark = Content.indexOf('*');
   }
  
   CurrentMsg = Content.substring(0, SepMark);
   TempContent = Content.substring(SepMark+1);
   Content = TempContent;
   notifyAll();
   return CurrentMsg;
   }
  
   public synchronized void PutChar(int c)
   {
   Character d = new Character((char) c);
   Content = Content.concat(d.toString());
   if (c == '*')
   {
   available = true;
   }
   notifyAll();
   }
  }
  
  
  /*
  *
  * WriteSerial.java 1.0
  * Program to send a character to the serial port
  *
  * Created: March 27, 2001
  *
  * Author : Qingye Jiang (John)
  * American GNC Corporation
  * 888 Easy St, Simi Valley CA 93065-1812
  *
  * Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
  * [email protected]
  *
  */
  
  import java.io.*;
  
  public class WriteSerial extends Thread
  {
   private SerialBuffer ComBuffer;
   private File ComPort;
  
   public WriteSerial(File Port)
   {
   ComPort = Port;
   }
  
   public void run()
   {
   int c;
   try
   {
   FileWriter out = new FileWriter(ComPort);
   while (true)
   {
   out.write('*');
   }
   } catch (IOException e)
   {
   System.out.println(e.getMessage());
   }
   }
  }
  /*
  *
  * SendCom.java 1.0
  *
  * Project: Java Based Information Exchange Support System
  * Onboard Plug-in System
  * Sending data through serial port
  *
  * Created: March 15, 2001
  *
  * Author : Qingye Jiang (John)
  * American GNC Corporation
  * 888 Easy St, Simi Valley CA 93065-1812
  *
  * Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
  *
  */
  
  import java.io.*;
  
  public class SendCom
  {

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产成人高潮免费观看精品 | 最近免费观看高清韩国日本大全 | 色人阁五月天 | 中文字幕在线观看免费 | 亚洲综人网 | 三级xxxx | 日韩精品一区二区三区中文 | 欧美区在线 | 超碰97国产在线 | 欧美精品在线免费观看 | 久草热久草视频 | 中文字幕在线观看精品 | 欧美日本国产精品 | 欧美a∨一区二区三区久久黄 | 久草在线免费看 | 国产1区2 | 国产1区2区3区中文字幕 | 高潮激情aaaaa免费看 | 精国产品一区二区三区四季综 | 黄污网站在线 | av中文在线观看 | 少妇的肉体的满足毛片 | 亚洲视频综合 | 免费人成在线播放 | 法国性xxx精品hd专区 | 一级做a爱片久久毛片a高清 | 亚洲va在线| 激情夜色 | 一区播放 | 日韩三级伦理在线观看 | 免费中文视频 | 国产一级毛片不卡 | 国产在线观看91精品 | 国产成人免费精品 | 欧美毛片免费观看 | 高清国产福利 | 天天夜夜操操 | 国产免费传媒av片在线 | 成人午夜毛片 | 草草影院地址 | 国产精品999在线观看 |