谁可以发给我个在windows下能用的JAVA串口通信啊~~

jspenguin 2008-03-23 10:06:29
谁可以发给我个在windows下能用的JAVA串口通信啊~~要可以用的
...全文
61 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
刘彬彬 2008-03-24
  • 打赏
  • 举报
回复
下面是Java 串口通信的核心代码,希望对你有用!
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
OutputStream outputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType()==CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals( "COM1 ")) {
SimpleRead reader = new SimpleRead();
}
}
}
}
public SimpleRead() {
try {//打开串口COM1
serialPort = (SerialPort) portId.open( "SimpleReadApp ", 2000);
} catch (PortInUseException e) {}
try {//获得输入输出流
inputStream = serialPort.getInputStream();
 outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {//为串口添加监听器
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {//配置串口
serialPort.setSerialPortParams(9600,//波特率9600bps
SerialPort.DATABITS_7,//7位数据位
SerialPort.STOPBITS_1,//1位停止位
SerialPort.PARITY_EVEN);//偶校验
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);//新建一线程
readThread.start();//启动线程
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE://如果有数据到达
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
//读取一个字节到readBuffer
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}
xlongbuilder 2008-03-24
  • 打赏
  • 举报
回复
帮顶
老紫竹 2008-03-24
  • 打赏
  • 举报
回复
comm.jar 可以从我的网站,或者我在CSDN的资源里找到

import java.util.Enumeration;

import javax.comm.CommPortIdentifier;

public class TestCom {
public static void main(String argv[]) {
new TestCom();
}

public TestCom() {
CommPortIdentifier portId;

Enumeration ports = CommPortIdentifier.getPortIdentifiers();

if (ports == null) {
System.out.println("No comm ports found!");
return;
}

// print out all ports
System.out.println("printing all ports...");
while (ports.hasMoreElements()) {
System.out.println(" " + ((CommPortIdentifier) ports.nextElement()).getName());

}
}

}
CruiseYoung提供的带有详细书签的电子书籍目录 http://blog.csdn.net/fksec/article/details/7888251 该资料是《Visual C++ 2005入门经典》的源代码及课后练习答案 对应的书籍资料见: Visual C++ 2005入门经典 基本信息 原书名: Ivor Horton's Beginning Visual C++ 2005 原出版社: Wiley 作者: (美)Ivor Horton 译者: 李颂华 康会光 出版社:清华大学出版社 ISBN:9787302142713 上架时间:2007-2-12 出版日期:2007 年1月 开本:16开 页码:1046 版次:1-1 编辑推荐   本书由编程语言先驱者Ivor Horton倾力而著,是国内第一本全面、深入介绍Visual C++ 2005的经典之作! 内容简介   本书系编程语言先驱者Ivor Horton的经典之作,是学习C++编程最畅销的图书品种之一,不仅涵盖了Visual C++ .NET编程知识,还全面介绍了标准C++语言和.NET C++/CLI。本书延续了Ivor Horton讲解编程语言的独特方法,从中读者可以学习Visual C++ 2005的基础知识,并全面掌握在MFC和Windows Forms中访问数据源的技术。此外,本书各章后面的习题将有助于读者温故而知新,并尽快成为C++高效程序员。...    作译者   Ivor Horton是世界著名的计算机图书作家,主要从事与编程相关的顾问及撰写工作,曾帮助无数程序员步入编程的殿堂。他曾在IBM工作多年,能使用多种语言进行编程(在多种机器上使用汇编语言和高级语言),设计和实现了实时闭环工业控制系统。Horton拥有丰富的教学经验(教学内容包括C、C++、Fortran、PL/1、APL等),同时还是机械、加工和电子CAD系统、机械CAM系统和DNC/CNC系统方面的专家。Ivor Horton还著有Beginning Visual C++ 6、Beginning C Programming和Beginning Java 2等多部入门级好书。 目录 封面 -18 前言 -14 目录 -9 第1章 使用Visual C++ 2005编程 1 1.1 .NET Framework 1 1.2 CLR 2 1.3 编写C++应用程序 3 1.4 学习Windows编程 4 1.4.1 学习C++ 4 1.4.2 C++标准 5 1.4.3 控制台应用程序 5 1.4.4 Windows编程概念 6 1.5 集成开发环境简介 7 1.6 使用IDE 9 1.6.1 工具栏选项 9 1.6.2 可停靠的工具栏 10 1.6.3 文档 11 1.6.4 项目和解决方案 11 1.6.5 设置Visual C++ 2005的选项 23 1.6.6 创建和执行Windows应用程序 24 1.6.7 创建Windows Forms应用程序 26 1.7 小结 29 第2章 数据、变量和计算 31 2.1 C++程序结构 31 2.1.1 程序注释 36 2.1.2 #include指令——头文件 37 2.1.3 命名空间和using声明 37 2.1.4 main()函数 38 2.1.5 程序语句 38 2.1.6 空白 40 2.1.7 语句块 41 2.1.8 自动生成的控制台程序 41 2.2 定义变量 42 2.2.1 命名变量 43 2.2.2 C++中的关键字 43 2.2.3 声明变量 44 2.2.4 变量的初值 44 2.3 基本数据类型 45 2.3.1 整型变量 45 2.3.2 字符数据类型 46 2.3.3 整型修饰符 47 2.3.4 布尔类型 48 2.3.5 浮点类型 48 2.3.6 ISO/ANSI C++中的基本类型 49 2.3.7 字面值 50 2.3.8 定义数据类型的同义词 50 2.3.9 具有特定值集的变量 51 2.3.10 指定枚举常量的类型 52 2.4 基本的输入/输出操作 53 2.4.1 从键盘输入 53 2.4.2 到命令行的输出 53 2.4.3 格式化输出 54 2.4.4 转义序列 55 2.5 C++中的计算 57 2.5.1 赋值语句 57 2.5.2 算术运算 58 2.5.3 计算余数 63 2.5.4 修改变量 63 2.5.5 增量和减量运算符 64 2.5.6 计算

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧