导航:首页 > 编程语言 > java读串口

java读串口

发布时间:2022-09-19 05:50:02

java读取串口数据

byte[]是字节流,你看到的乱码是转成字符乱码了
这也没啥原因,你得知道接收的byte[]是什么编码产生的,然后使用相同的编码才能解析出来。
一般乱码了也就是GBK、UTF8不统一

㈡ java串口,读取和发送数据

publicstaticvoidprocess(){
try{
EnumerationportList=CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements())
{
CommPortIdentifierportId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL)//如果端口类型是串口则判断名称
{
if(portId.getName().equals("COM1")){//如果是COM1端口则退出循环
break;
}else{
portId=null;
}
}
}
SerialPortserialPort=(SerialPort)portId.open("Serial_Communication",1000);//打开串口的超时时间为1000ms
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//设置串口速率为9600,数据位8位,停止位1们,奇偶校验无
InputStreamin=serialPort.getInputStream();//得到输入流
OutputStreamout=serialPort.getOutputStream();//得到输出流

//进行输入输出操作
//操作结束后
in.close();
out.close();
serialPort.close();//关闭串口

}catch(PortInUseExceptione){
e.printStackTrace();
}catch(){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}

}

㈢ Java怎么读取串口数据

public static void process() {
try {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//如果端口类型是串口则判断名称
{
if(portId.getName().equals("COM1")){//如果是COM1端口则退出循环
break;
}else{
portId=null;
}
}
}
SerialPort serialPort = (SerialPort)portId.open("Serial_Communication", 1000);//打开串口的超时时间为1000ms
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//设置串口速率为9600,数据位8位,停止位1们,奇偶校验无
InputStream in = serialPort.getInputStream();//得到输入流
OutputStream out = serialPort.getOutputStream();//得到输出流

//进行输入输出操作
//操作结束后
in.close();
out.close();
serialPort.close();//关闭串口

} catch (PortInUseException e) {
e.printStackTrace();
} catch ( e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

㈣ java串口编程中只能用InputStream来读串口来的数据吗

这两个方法都是阻塞式的方法,readline 方法在流关闭或者缓冲区填满之前,除非遇到回车或换行(\n 或\r)才会返回数据,否则就会一直阻塞在那里等待数据
inputstream的read方法同样是阻塞式方法,当流没有关闭时,他就会一直在那里阻塞等待数据。
试试多线程处理数据流,这样就不需要一直等待流输入了;或者在设备那边每次传输完成都关闭流,但是每次都开启关闭流好像消耗略大

㈤ 用java从串口读取数据然后显示在网页上,能实现吗

用java从串口读取数据然后显示在网页上,能实现。
以下是对串口读写代码,来自网友网络知道网友。其它如何传递到网页自己搜索吧。
public static void process() {
try {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//如果端口类型是串口则判断名称
{
if(portId.getName().equals("COM1")){//如果是COM1端口则退出循环
break;
}else{
portId=null;
}
}
}
SerialPort serialPort = (SerialPort)portId.open("Serial_Communication", 1000);//打开串口的超时时间为1000ms
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//设置串口速率为9600,数据位8位,停止位1们,奇偶校验无
InputStream in = serialPort.getInputStream();//得到输入流
OutputStream out = serialPort.getOutputStream();//得到输出流

//进行输入输出操作
//操作结束后
in.close();
out.close();
serialPort.close();//关闭串口

} catch (PortInUseException e) {
e.printStackTrace();
} catch ( e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}</ol>

㈥ 如何用Java语言向串口读写数据

public static void process() {
try {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//如果端口类型是串口则判断名称
{
if(portId.getName().equals("COM1")){//如果是COM1端口则退出循环
break;
}else{
portId=null;
}
}
}
SerialPort serialPort = (SerialPort)portId.open("Serial_Communication", 1000);//打开串口的超时时间为1000ms
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//设置串口速率为9600,数据位8位,停止位1们,奇偶校验无
InputStream in = serialPort.getInputStream();//得到输入流
OutputStream out = serialPort.getOutputStream();//得到输出流

//进行输入输出操作
//操作结束后
in.close();
out.close();
serialPort.close();//关闭串口

} catch (PortInUseException e) {
e.printStackTrace();
} catch ( e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

㈦ java 多线程读写同一串口

串口本来是单向的,所以,读的时候肯定写不了(相反亦然)、一个程序(线程)写了别的肯定写不了。

所以,一个线程读、另一线程写。通过事件通知上层。 你懂。

㈧ 求java 关于串口数据读取和写入方法(最好解释下)

Comm API基础

我无意于在此详细描述Comm API每个类和接口的用法,但我会介绍Comm API的类结构和几个重要的API用法。

所有的comm API位于javax.comm包下面。从Comm API的javadoc来看,它介绍给我们的只有区区以下13个类或接口:

javax.comm.CommDriver

javax.comm.CommPort

javax.comm.ParallelPort

javax.comm.SerialPort

javax.comm.CommPortIdentifier

javax.comm.CommPortOwnershipListener

javax.comm.ParallelPortEvent

javax.comm.SerialPortEvent

javax.comm.ParallelPortEventListener (extends java.util.EventListener)

javax.comm.SerialPortEventListener (extends java.util.EventListener)

javax.comm.NoSuchPortException

javax.comm.PortInUseException

javax.comm.

下面讲解一下几个主要类或接口。

1.枚举出系统所有的RS232端口

在开始使用RS232端口通讯之前,我们想知道系统有哪些端口是可用的,以下代码列出系统中所有可用的RS232端口:

Enumeration en = CommPortIdentifier.getPortIdentifiers();

CommPortIdentifier portId;

while (en.hasMoreElements())

{

portId = (CommPortIdentifier) en.nextElement();

/*如果端口类型是串口,则打印出其端口信息*/

if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)

{

System.out.println(portId.getName());

}

}

在我的电脑上以上程序输出以下结果:

COM1

COM2

CommPortIdentifier类的getPortIdentifiers方法可以找到系统所有的串口,每个串口对应一个CommPortIdentifier类的实例。

2.打开端口

如果你使用端口,必须先打开它。

try{

CommPort serialPort = portId.open("My App", 60);

/*从端口中读取数据*/

InputStream input = serialPort.getInputStream();

input.read(...);

/*往端口中写数据*/

OutputStream output = serialPort.getOutputStream();

output.write(...)

...

}catch(PortInUseException ex)

{ ... }

通过CommPortIdentifier的open方法可以返回一个CommPort对象。open方法有两个参数,第一个是String,通常设置为你的应用程序的名字。第二个参数是时间,即开启端口超时的毫秒数。当端口被另外的应用程序占用时,将抛出PortInUseException异常。

在这里CommPortIdentifier类和CommPort类有什么区别呢?其实它们两者是一一对应的关系。CommPortIdentifier主要负责端口的初始化和开启,以及管理它们的占有权。而CommPort则是跟实际的输入和输出功能有关的。通过CommPort的getInputStream()可以取得端口的输入流,它是java.io.InputStream接口的一个实例。我们可以用标准的InputStream的操作接口来读取流中的数据,就像通过FileInputSteam读取文件的内容一样。相应的,CommPort的getOutputStream可以获得端口的输出流,这样就可以往串口输出数据了。

3.关闭端口

使用完的端口,必须记得将其关闭,这样可以让其它的程序有机会使用它,不然其它程序使用该端口时可能会抛出端口正在使用中的错误。很奇怪的是,CommPortIdentifier类只提供了开启端口的方法,而要关闭端口,则要调用CommPort类的close()方法。
CommPort的输入流的读取方式与文件的输入流有些不一样,那就是你可能永远不知这个InputStream何时结束,除非对方的OutputStream向你发送了一个特定数据表示发送结束,你收到这个特定字符后,再行关闭你的InputStream。而comm.jar提供了两种灵活的方式让你读取数据。

1.轮询方式(Polling)

举个例子,你同GF相约一起出门去看电影,但你的GF好打扮,这一打扮可能就是半小时甚至一小时以上。这时你就耐不住了,每两分钟就催问一次“好了没?”,如此这样,直到你的GF说OK了才算完。这个就叫轮询(Polling)。

在程序中,轮询通常设计成一个封闭的循环,当满足某个条件时即结束循环。刚才那个例子中,你的GF说“OK了!”,这个就是结束你轮询的条件。在单线程的程序中,当循环一直执行某项任务而又无法预知它何时结束时,此时你的程序看起来可能就像死机一样。在VB程序中,这个问题可以用在循环结构中插入一个doEvent语句来解决。而Java中,最好的方式是使用线程,就像以下代码片断一样。

public TestPort extend Thread

{

...

InputStream input = serialPort.getInputStream();

StringBuffer buf = new StringBuffer();

boolean stopped = false;

...

public void run()

{

try {

while( !stopped )

int ch = input.read();

if ( ch=='q' || ch=='Q' )

{

/*结束读取,关闭端口...*/

stopped = true;

...

}

else

{

buf.append((char)ch);

...

}

}catch (InterruptedException e) { }

}

}

2.监听方式(listening)

Comm API支持标准的Java Bean型的事件模型。也就是说,你可以使用类似AddXXXListener这样的方法为一个串口注册自己的监听器,以监听方式进行数据读取。

如要对端口监听,你必须先取得CommPortIdentifier类的一个实例,

CommPort serialPort = portId.open("My App", 60);

从而取得SerialPort,再调用它的addEventListener方法为它添加监听器,

serialPort.addEventListener(new MyPortListener());

SerialPort的监听器必须继承于SerialPortEventListener接口。当有任何SerialPort的事件发生时,将自动调用监听器中的serialEvent方法。Serial Event有以下几种类型:

BI -通讯中断.

CD -载波检测.

CTS -清除发送.

DATA_AVAILABLE -有数据到达.

DSR -数据设备准备好.

FE -帧错误.

OE -溢位错误.

OUTPUT_BUFFER_EMPTY -输出缓冲区已清空.

PE -奇偶校验错.

RI -振铃指示.

下面是一个监听器的示例:

public void MyPortListener implements SerialPortEventListener

{

public void serialEvent(SerialPortEvent evt)

{

switch (evt.getEventType())

{

case SerialPortEvent.CTS :

System.out.println("CTS event occured.");

break;

case SerialPortEvent.CD :

System.out.println("CD event occured.");

break;

case SerialPortEvent.BI :

System.out.println("BI event occured.");

break;

case SerialPortEvent.DSR :

System.out.println("DSR event occured.");

break;

case SerialPortEvent.FE :

System.out.println("FE event occured.");

break;

case SerialPortEvent.OE :

System.out.println("OE event occured.");

break;

case SerialPortEvent.PE :

System.out.println("PE event occured.");

break;

case SerialPortEvent.RI :

System.out.println("RI event occured.");

break;

case SerialPortEvent.OUTPUT_BUFFER_EMPTY :

System.out.println("OUTPUT_BUFFER_EMPTY event occured.");

break;

case SerialPortEvent.DATA_AVAILABLE :

System.out.println("DATA_AVAILABLE event occured.");

int ch;

StringBuffer buf = new StringBuffer();

InputStream input = serialPort.getInputStream

try {

while ( (ch=input.read()) > 0) {

buf.append((char)ch);

}

System.out.print(buf);

} catch (IOException e) {}

break;

}

}

这个监听器只是简单打印每个发生的事件名称。而对于大多数应用程序来说,通常关心是DATA_AVAILABLE事件,当数据从外部设备传送到端口上来时将触发此事件。此时就可以使用前面提到过的方法,serialPort.getInputStream()来从InputStream中读取数据了。

㈨ 用java从串口读取数据然后显示在网页上,能实现吗

  1. 用java从串口读取数据然后显示在网页上,能实现。

  2. 以下是对串口读写代码,来自网友网络知道网友。其它如何传递到网页自己搜索吧。

  3. public static void process() {

  4. try {

  5. Enumeration portList = CommPortIdentifier.getPortIdentifiers();

  6. while (portList.hasMoreElements())

  7. {

  8. CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();

  9. if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//如果端口类型是串口则判断名称

  10. {

  11. if(portId.getName().equals("COM1")){//如果是COM1端口则退出循环

  12. break;

  13. }else{

  14. portId=null;

  15. }

  16. }

  17. }

  18. SerialPort serialPort = (SerialPort)portId.open("Serial_Communication", 1000);//打开串口的超时时间为1000ms

  19. serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//设置串口速率为9600,数据位8位,停止位1们,奇偶校验无

  20. InputStream in = serialPort.getInputStream();//得到输入流

  21. OutputStream out = serialPort.getOutputStream();//得到输出流

  22. //进行输入输出操作

  23. //操作结束后

  24. in.close();

  25. out.close();

  26. serialPort.close();//关闭串口

  27. } catch (PortInUseException e) {

  28. e.printStackTrace();

  29. } catch ( e) {

  30. e.printStackTrace();

  31. } catch (IOException e) {

  32. e.printStackTrace();

  33. }

  34. }

阅读全文

与java读串口相关的资料

热点内容
机票php源码 浏览:231
linux共享mac 浏览:922
中国没有国外的服务器地址 浏览:757
为什么退款服务器连接错误 浏览:555
android短信存储位置 浏览:970
unix网络编程卷4 浏览:807
找靓机app下单什么时候发货 浏览:413
android一个应用两个进程 浏览:801
linux硬盘复制 浏览:808
php图片服务器搭建 浏览:801
下载压缩文件怎么打开 浏览:194
新建文件夹叫什么名字 浏览:567
windows20的开机命令 浏览:334
微信一般在电脑的那个文件夹 浏览:511
go在win7下编译特别慢 浏览:256
光遇ios耳机安卓为什么没有 浏览:904
小米手机桌面文件夹经常自动散开 浏览:607
小米电话手表用什么app进行设置 浏览:265
虚拟打印机pdf下载 浏览:671
jdk编译运行方法 浏览:459