这个简单的程序包括以下文件:

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)。这样是不能够实现全双工通讯的。

------------------------------------------

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();

}

}

---------------------------------------------

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);

}

}

}

-----------------------------------------------

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)

{}

}

}

------------------------------------------------

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();

}

}

-------------------------------------------------

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());

}

}

}

-----------------------------------------------

import

java.io.*;

public class SendCom

{

public static void main(String[]

args)

{

File OutFile = new File("SEND.TXT");

File ComPort = new

File("COM2");

int c;

try

{

FileReader in = new

FileReader(OutFile);

FileWriter out = new FileWriter(ComPort);

while ((c =

in.read()) != -1)

out.write(c);

in.close();

out.close();

} catch

(IOException e)

{}

}

}

----------------------------------------------

SEND.TXT*

This

is a sample of the data file for program testing. *

It should be in the

same directory as the SendCom.class file.*

When you run this sample

program, connect your COM1 and COM2 with a

serial cable so that you can test

this program on one machine. If

you have two machines, you can connect the

two machine via a serial

cable and test it. Modified the definition of

ComPort in the program

if necessary. *

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9650775/viewspace-923907/,如需转载,请注明出处,否则将追究法律责任。

java获取已经打开的串口的输出流_使用Java读取串口的程序(转)相关推荐

  1. java获取当前时间代码(适合于是springboot+vue+java)

    java获取当前时间代码(适合于是springboot+vue+java) /*** 获取当前的的动态时间格式*/Calendar calendar = Calendar.getInstance(); ...

  2. java 标准输入流 关闭 打开_java--标准输入输出流

    //读取键盘录入的数据写到a.txt //方式一 private static void method() throws IOException { //创建输入流对象 InputStream is ...

  3. java多线程为啥一直用的一个线程_一个Java多线程的问题,颠覆了我多年的认知!...

    作者 | ithuangqing 来源 | 编码之外(ID:ithuangqing) 碰见个奇怪的多线程问题 小白们也不用怕,今天的文章你们都能看得懂,最近的学习中,碰到这样的一个问题: Java创建 ...

  4. java赋值运算的类型转换出新的问题_学习Java基本数据类型与转换++,--等问题总结...

    java中的数据类型java有4种类型分别是整数类型.浮点类型.字符型.布尔型.java的八中分类分别是byte.short.int.long.float.double.char.boolean简称4 ...

  5. Java main方法_解释Java中的main方法,及其作用_一个java文件中可包含多个main方法

    public static void main(String[] args) {} 或者 public static void main(String args[]) {} main方法是我们学习Ja ...

  6. java程序设计 第2版 唐大仕_《Java程序设计(第2版)》唐大仕 源代码

    [实例简介] <Java程序设计(第2版)>唐大仕 清华大学出版社 北方交通大学出版社 [实例截图] [核心代码] bbe30560-8619-4a35-a024-50004cc5c8e2 ...

  7. java获取窗口_如何使用Java获取当前打开的窗口/进程的列表?

    最后,使用Java 9+可以使用ProcessHandle:public static void main(String[] args) { ProcessHandle.allProcesses() ...

  8. java中怎么下载图片不显示图片_关于Java/Kotlin下载图片,图片打开不能显示问题探究...

    图片下载其实是个很简单的功能,通过IO流从在线地址获取流,之后将流输出到文件即可完成下载功能,但是,最近我发现某个网站中的图片下载成功,但是打开却是无法打开,这让我迷惑,百度上根本就没有人说清楚 今天 ...

  9. java 获取路由器mac_求好人帮助,如何用java语言获取像无线路由器上的MAC地址,我会重赏...

    如图所示的无线路由max地址,,挺说可以利用java的工具类java.net.NetworkInterface获取次无线网卡的max,我看了某个人的微博,他这样写:"通过java.net.N ...

最新文章

  1. [CF494D]Birthday
  2. 经典数据结构和算法 双端队列 java
  3. linux command find
  4. python读取rar文件_在 python 中,如何读取由 7z 压缩的文本文件_python_酷徒编程知识库...
  5. vi and vim 用法
  6. eclipse加速之禁用 JS、jsp 等文件的语法验证
  7. java中的Iterator和Iterable 区别
  8. 解决“Automation 服务器不能创建对象”的问题!
  9. eureka动态扩容_SpringCloud- 第二篇 Eureka快速上手
  10. 远程办公、游戏迅猛增长,旅行、汽车业遭重创!疫期移动互联网行业报告解读...
  11. 一起来看小米发布会!
  12. IOUtils使用介绍
  13. C语言学习-翁凯(目录总章)
  14. 条码软件如何制作SCC-14条形码
  15. web前端vue项目完整步骤。pc端
  16. Hi,欢迎大家来到阿毛小猪的博客,分享自己学习中的经验,默默的前行,聆听心的声音...
  17. 设计模式的艺术 行为型模式之观察者模式
  18. 美苏太空竞赛历年卫星火箭发射以及历史事件介绍
  19. oracle中有没有distance,oracle_关于extended distance cluster  rac的介绍
  20. 清默网络——RIP单播更新

热门文章

  1. 同等学力计算机综合难吗,计算机在职研究生考试难度大吗?这种方式通过率高...
  2. mysql表空间不足_MySQL Innodb表空间不足的处理方法 风好大
  3. The netfilter.org project
  4. 基于GitHub创建自己的个人网站
  5. python写算法快吗_用python写排序算法
  6. python输入三个整数_python笔记3:依次输入3个数排序打
  7. python写tcp通信程序_一个简单的基于TCP通信的服务器端与客户端程序
  8. java2的7次方怎么表示_一元二次方程常见题型之方程根的解法
  9. 数据字典模板_巧用单元格保护功能+字典技术,制作高大上的人员信息录入表...
  10. java程序在内存中的存储分配