java获取已经打开的串口的输出流_使用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)。这样是不能够实现全双工通讯的。
------------------------------------------
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读取串口的程序(转)相关推荐
- java获取当前时间代码(适合于是springboot+vue+java)
java获取当前时间代码(适合于是springboot+vue+java) /*** 获取当前的的动态时间格式*/Calendar calendar = Calendar.getInstance(); ...
- java 标准输入流 关闭 打开_java--标准输入输出流
//读取键盘录入的数据写到a.txt //方式一 private static void method() throws IOException { //创建输入流对象 InputStream is ...
- java多线程为啥一直用的一个线程_一个Java多线程的问题,颠覆了我多年的认知!...
作者 | ithuangqing 来源 | 编码之外(ID:ithuangqing) 碰见个奇怪的多线程问题 小白们也不用怕,今天的文章你们都能看得懂,最近的学习中,碰到这样的一个问题: Java创建 ...
- java赋值运算的类型转换出新的问题_学习Java基本数据类型与转换++,--等问题总结...
java中的数据类型java有4种类型分别是整数类型.浮点类型.字符型.布尔型.java的八中分类分别是byte.short.int.long.float.double.char.boolean简称4 ...
- Java main方法_解释Java中的main方法,及其作用_一个java文件中可包含多个main方法
public static void main(String[] args) {} 或者 public static void main(String args[]) {} main方法是我们学习Ja ...
- java程序设计 第2版 唐大仕_《Java程序设计(第2版)》唐大仕 源代码
[实例简介] <Java程序设计(第2版)>唐大仕 清华大学出版社 北方交通大学出版社 [实例截图] [核心代码] bbe30560-8619-4a35-a024-50004cc5c8e2 ...
- java获取窗口_如何使用Java获取当前打开的窗口/进程的列表?
最后,使用Java 9+可以使用ProcessHandle:public static void main(String[] args) { ProcessHandle.allProcesses() ...
- java中怎么下载图片不显示图片_关于Java/Kotlin下载图片,图片打开不能显示问题探究...
图片下载其实是个很简单的功能,通过IO流从在线地址获取流,之后将流输出到文件即可完成下载功能,但是,最近我发现某个网站中的图片下载成功,但是打开却是无法打开,这让我迷惑,百度上根本就没有人说清楚 今天 ...
- java 获取路由器mac_求好人帮助,如何用java语言获取像无线路由器上的MAC地址,我会重赏...
如图所示的无线路由max地址,,挺说可以利用java的工具类java.net.NetworkInterface获取次无线网卡的max,我看了某个人的微博,他这样写:"通过java.net.N ...
最新文章
- [CF494D]Birthday
- 经典数据结构和算法 双端队列 java
- linux command find
- python读取rar文件_在 python 中,如何读取由 7z 压缩的文本文件_python_酷徒编程知识库...
- vi and vim 用法
- eclipse加速之禁用 JS、jsp 等文件的语法验证
- java中的Iterator和Iterable 区别
- 解决“Automation 服务器不能创建对象”的问题!
- eureka动态扩容_SpringCloud- 第二篇 Eureka快速上手
- 远程办公、游戏迅猛增长,旅行、汽车业遭重创!疫期移动互联网行业报告解读...
- 一起来看小米发布会!
- IOUtils使用介绍
- C语言学习-翁凯(目录总章)
- 条码软件如何制作SCC-14条形码
- web前端vue项目完整步骤。pc端
- Hi,欢迎大家来到阿毛小猪的博客,分享自己学习中的经验,默默的前行,聆听心的声音...
- 设计模式的艺术 行为型模式之观察者模式
- 美苏太空竞赛历年卫星火箭发射以及历史事件介绍
- oracle中有没有distance,oracle_关于extended distance cluster rac的介绍
- 清默网络——RIP单播更新
热门文章
- 同等学力计算机综合难吗,计算机在职研究生考试难度大吗?这种方式通过率高...
- mysql表空间不足_MySQL Innodb表空间不足的处理方法 风好大
- The netfilter.org project
- 基于GitHub创建自己的个人网站
- python写算法快吗_用python写排序算法
- python输入三个整数_python笔记3:依次输入3个数排序打
- python写tcp通信程序_一个简单的基于TCP通信的服务器端与客户端程序
- java2的7次方怎么表示_一元二次方程常见题型之方程根的解法
- 数据字典模板_巧用单元格保护功能+字典技术,制作高大上的人员信息录入表...
- java程序在内存中的存储分配