62,616
社区成员
发帖
与我相关
我的任务
分享
package main;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
public class Main66 {
static Enumeration portList;
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static BufferedReader br;
static boolean flag = false;
static boolean isPlay = false;
/**
* 红外感应到有人,投影机播放内容,讲到哪个地方,沙盘中对应地址的路灯亮。
*/
public static void main(String[] args) {
controlInfrared();
}
/**
* 红外人体感应
* @throws Exception
*/
public static void controlInfrared() {
try {
//aa00db0100000055 测试灯的命令
//aa00dd0000000055 测试红外命令
byte[] bt = new byte[]{(byte)0xaa,(byte)0x00,(byte)0xdd,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x55};
portId = CommPortIdentifier.getPortIdentifier("COM1");
//打开通讯
serialPort = (SerialPort)portId.open("SimpleWriteApp", 2000);
serialPort.setRTS(true);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
//打开输出流,写入命令
outputStream = serialPort.getOutputStream();
outputStream.write(bt);
outputStream.flush();
//获得输出流
inputStream = serialPort.getInputStream();
//下面的代码该如何写,才能获得“FC AA 00 DD 03 00 00 55 55”这样的返回值?
outputStream.close();
serialPort.close();
}catch (Exception e) {
e.printStackTrace();
} finally{
//关闭流和端口
try {
if(outputStream != null){
outputStream.close();
}
if(serialPort != null){
serialPort.close();
}
}catch(Exception e){
System.out.println("关闭端口时异常出现:"+e.getMessage());
}
}
}
}
package com.main2;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
public class Test extends Thread implements SerialPortEventListener {
static Enumeration portList;
static CommPortIdentifier portId;
static InputStream inputStream;
static OutputStream outputStream;
static SerialPort serialPort;
static int count = 0;
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("COM3")) {
Test test = new Test();
}
}
}
}
public Test() {
try {
//打开通讯
serialPort = (SerialPort) portId.open("ReadCommApp", 2000);
serialPort.setRTS(false);//此步骤可以省略,猜测默认情况即是false
serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
byte[] bt = new byte[]{(byte)0xaa,(byte)0x00,(byte)0xdd,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x55};
outputStream = serialPort.getOutputStream();
outputStream.write(bt);
outputStream.flush();
outputStream.close();
Thread.sleep(200); //50~~1400
serialPort.setRTS(true);
inputStream = serialPort.getInputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public void serialEvent(SerialPortEvent event) {
try{
if (event.getEventType() == event.DATA_AVAILABLE){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch;
while((ch=inputStream.read()) != -1){
count++;
if(count>2){
bos.write(ch);
}
}
byte[] bt = bos.toByteArray();
for(byte b : bt){
System.out.print("b="+b+"\t");//b=-86 b=0 b=-35 b=3 b=0 b=0 b=85 b=85
}
bos.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/config_man/archive/2010/06/30/5705264.aspx