精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Java>>JAVA编程>>杂项>>一个使用Java读取串口的程序(2)[转自:朋友处]

主题:一个使用Java读取串口的程序(2)[转自:朋友处]
发信人: sunrisepro(SunRise)
整理人: zjxyz(2002-09-10 10:11:16), 站内信件
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());
       }
   }
}






----
什么叫工作?工作就是斗争。哪些地方有困难、有问题,需要我们去解决。我们是为着解决困难、解决问题去斗争的。越是困难的地方越是要去,这样才是好同志——毛泽东

[关闭][返回]