服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

一个使用java读取串口的程序(1)


  以下是我写的用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)
  * 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)
  {
   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)
  * qjiang@tsinghua.edu
  *
  */
  
  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
  {

扫描关注微信公众号