国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

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

2019-11-18 11:04:35
字體:
供稿:網(wǎng)友

 




這個(gè)簡(jiǎn)單的程序包括以下文件:

       IMU.java (主程序)
       ReadBuffer.java (從緩沖區(qū)讀取一個(gè)消息)
       ReadSerial.java (讀取串口數(shù)據(jù)并放入緩沖區(qū))
       SerialBuffer.java (緩沖區(qū))
       WriteSerial.java (不斷的往串口送星號(hào)'*')

測(cè)試程序:

       SendCom.java (將一個(gè)數(shù)據(jù)文件往串口發(fā)送)
       SEND.TXT (供測(cè)試用的數(shù)據(jù)文件)

在這個(gè)通訊程序中使用了一個(gè)簡(jiǎn)單的協(xié)議,既不同的消息之間用星號(hào)'*'作為分隔。這個(gè)程序中的問題是ReadSerial進(jìn)程和WriteSerial進(jìn)程不能夠同時(shí)啟動(dòng),出錯(cuò)信息是不能夠打開串口,因?yàn)橥瑯右粋€(gè)串口不能夠同時(shí)被打開兩次(在ReadSerial中聲明了FileReader

測(cè)試程序:

       SendCom.java (將一個(gè)數(shù)據(jù)文件往串口發(fā)送)
       SEND.TXT (供測(cè)試用的數(shù)據(jù)文件)

在這個(gè)通訊程序中使用了一個(gè)簡(jiǎn)單的協(xié)議,既不同的消息之間用星號(hào)'*'作為分隔。這個(gè)程序中的問題是ReadSerial進(jìn)程和WriteSerial進(jìn)程不能夠同時(shí)啟動(dòng),出錯(cuò)信息是不能夠打開串口,因?yàn)橥瑯右粋€(gè)串口不能夠同時(shí)被打開兩次(在ReadSerial中聲明了FileReader和在WriteSerial中聲明了FileWriter)。這樣是不能夠?qū)崿F(xiàn)全雙工通訊的。不知道有沒有做過的大俠能夠講講處理的辦法。

/*
*
* 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)
* qjiang@tsinghua.edu
*
*/

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)
* qjiang@tsinghua.edu

*
*/

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)
*       qjiang@tsinghua.edu
*
*/

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)
* qjiang@tsinghua.edu

*
*/

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)



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新营市| 三明市| 东港市| 闻喜县| 阿尔山市| 赤峰市| 松滋市| 磴口县| 噶尔县| 肇东市| 祥云县| 交口县| 夏津县| 高淳县| 唐海县| 海南省| 萍乡市| 历史| 海丰县| 肥东县| 苍梧县| 河北区| 泗洪县| 正宁县| 河津市| 凤台县| 诸暨市| 建昌县| 吴江市| 密山市| 锡林浩特市| 奉节县| 孟津县| 望奎县| 峨眉山市| 天峨县| 襄城县| 大冶市| 天峨县| 襄城县| 绥化市|