/ /in是一个inputstream类型的。我在类的其他地方定义的。现在为了取得in中的数据,我做了如下的方法。
方法中我把十进制转化成了16进制。
现在的问题是:我仅仅输出了个字符出来,也就是一个字节的长度的。
我的具体的结果如下:、
68
1
2
3
4
5
6
68
但是本身需要的更长的数据,是怎么就丢失了呢?public void reader(){
java.io.DataInputStream r = new java.io.DataInputStream(new BufferedInputStream(in));
int t;
try { for(int i=0;i<100;i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
t = r.read();
Integer t1 = new Integer(t);
System.out.println(t1.toHexString(t));
}
} catch (IOException e) {
e.printStackTrace();
}
}
注:我是从串口中读取的数据变成的io流。
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
private InputStream in; //输入流
in = serialPort.getInputStream();我可以链接上com1,也能向com1中发送命令,但是读取的数据只有8位,后面不能读出。
串口的代码如下:import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.TooManyListenersException;import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;public class SerialBean
implements SerialPortEventListener {
// static{
// System.load("D:/apache-tomcat-6.0.14/bin/win32com.dll");
// System.out.println("加载成功");
// }
private String PortName = ""; //串口名称
private CommPortIdentifier portId; //串口管理器
private SerialPort serialPort; //串口对象 private OutputStream out; //输出流
private InputStream in; //输入流 /**
*<br>功能:本函数构造一个指向特定串口的SerialBean,该串口由参数PortID所指定。PortID = 1 表示COM1,PortID = 2 表示COM2,由此类推。
*/ public SerialBean(int portid) {
PortName = "COM" + portid;
//初始化串口
try {
portId = CommPortIdentifier.getPortIdentifier(PortName);
if(portId==null){
System.out.println("portId==null");
}
//System.out.println(portId.getName());
try {
serialPort = (SerialPort) portId.open("com", 1000);
// System.out.println(serialPort);
}
catch (PortInUseException e) {
System.out.println("串口已被使用");
System.out.println(serialPort);
serialPort.close();
} //用InputStream in去读取串口数据, 用OutputStream out向串口写数据.
try {
in = serialPort.getInputStream();
out = serialPort.getOutputStream(); }
catch (IOException e) {
serialPort.close();
} try {
serialPort.addEventListener(this);
/*注册一个SerialPortEventListener事件来监听串口事件*/
serialPort.notifyOnDataAvailable(true); /*数据可用*/
}
catch (TooManyListenersException e) {
serialPort.close();
} //初始化端口状态: 9600, 8, 1, none.
try {
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {
}
}
catch (Exception e) {
if(portId==null){
System.out.println("portId==null");
}
System.out.println(portId.getName());
System.out.println("初始化串口失败");
e.printStackTrace();
if (serialPort != null) {
serialPort.close();
}
} } public void serialEvent(SerialPortEvent evt) {
switch (evt.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE: {
try {
InputStream input = this.getIn();
int length = input.available();
byte[] image = new byte[length];
input.read(image);
input.close();
ClosePort();
}
catch (Exception e) {
e.printStackTrace();
ClosePort();
}
}
break;
}
} /**
* <br> 功能:本函数向串口发送一个字符串。
* <br> 参数:Msg是需要发送的字符串。
*/
public void WritePort(int[] Msg) {
try {
byte[] aa = new byte[Msg.length];
for(int i = 0;i<Msg.length;i++){
aa[i] = (byte)Msg[i];
}
out.write(aa);
}
catch (IOException e) {
e.printStackTrace();
}
} /**
* <br> 功能:本函数从串口读取一个字符。
* <br> 参数:
*/
public Character ReadOneFromPort() {
Character c = null;
try { c = Character.valueOf( (char) in.read());
return c.charValue();
}
catch (IOException e) {
return null;
}
}
public int[] readAll(){
int[] a=new int[8];
try {
int i=0;
while (in.read()!= -1) {
a[i] = in.read();
System.out.println(a[i]);
i++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a;
}
public byte[] reader(){
//int[] a = new int[]{};
//java.io.BufferedInputStream r = new java.io.BufferedInputStream(in);
java.io.DataInputStream r = new java.io.DataInputStream(new BufferedInputStream(in,1024));
StringBuffer buf = new StringBuffer();
byte[] d = new byte[30];
int t;
try { for(int i=0;i<100;i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t = r.read();
Integer t1 = new Integer(t);
System.out.println(t1.toHexString(t));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* <br> 功能:本函数从串口读取指定长度的字符。
* <br> 参数:Msg是需要发送的字符串。
*/
public String ReadMoreFromPort(int length) {
StringBuffer strbuff = new StringBuffer();
try {
for (int i = 0; i < length; i++) {
strbuff.append(ReadOneFromPort().charValue());
}
return strbuff.toString();
}
catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* <br>功能:停止串口检测进程并关闭串口.
*/ public void ClosePort() {
if (serialPort != null) {
serialPort.close();
}
} public void setPortId(CommPortIdentifier portId) {
this.portId = portId;
} public void setSerialPort(SerialPort serialPort) {
this.serialPort = serialPort;
} public void setOut(OutputStream out) {
this.out = out;
} public void setIn(InputStream in) {
this.in = in;
} public void setPortName(String PortName) {
this.PortName = PortName;
} public CommPortIdentifier getPortId() {
return portId;
} public SerialPort getSerialPort() {
return serialPort;
} public OutputStream getOut() {
return out;
} public InputStream getIn() {
return in;
} public String getPortName() {
return PortName;
}
}
方法中我把十进制转化成了16进制。
现在的问题是:我仅仅输出了个字符出来,也就是一个字节的长度的。
我的具体的结果如下:、
68
1
2
3
4
5
6
68
但是本身需要的更长的数据,是怎么就丢失了呢?public void reader(){
java.io.DataInputStream r = new java.io.DataInputStream(new BufferedInputStream(in));
int t;
try { for(int i=0;i<100;i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
t = r.read();
Integer t1 = new Integer(t);
System.out.println(t1.toHexString(t));
}
} catch (IOException e) {
e.printStackTrace();
}
}
注:我是从串口中读取的数据变成的io流。
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
private InputStream in; //输入流
in = serialPort.getInputStream();我可以链接上com1,也能向com1中发送命令,但是读取的数据只有8位,后面不能读出。
串口的代码如下:import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.TooManyListenersException;import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;public class SerialBean
implements SerialPortEventListener {
// static{
// System.load("D:/apache-tomcat-6.0.14/bin/win32com.dll");
// System.out.println("加载成功");
// }
private String PortName = ""; //串口名称
private CommPortIdentifier portId; //串口管理器
private SerialPort serialPort; //串口对象 private OutputStream out; //输出流
private InputStream in; //输入流 /**
*<br>功能:本函数构造一个指向特定串口的SerialBean,该串口由参数PortID所指定。PortID = 1 表示COM1,PortID = 2 表示COM2,由此类推。
*/ public SerialBean(int portid) {
PortName = "COM" + portid;
//初始化串口
try {
portId = CommPortIdentifier.getPortIdentifier(PortName);
if(portId==null){
System.out.println("portId==null");
}
//System.out.println(portId.getName());
try {
serialPort = (SerialPort) portId.open("com", 1000);
// System.out.println(serialPort);
}
catch (PortInUseException e) {
System.out.println("串口已被使用");
System.out.println(serialPort);
serialPort.close();
} //用InputStream in去读取串口数据, 用OutputStream out向串口写数据.
try {
in = serialPort.getInputStream();
out = serialPort.getOutputStream(); }
catch (IOException e) {
serialPort.close();
} try {
serialPort.addEventListener(this);
/*注册一个SerialPortEventListener事件来监听串口事件*/
serialPort.notifyOnDataAvailable(true); /*数据可用*/
}
catch (TooManyListenersException e) {
serialPort.close();
} //初始化端口状态: 9600, 8, 1, none.
try {
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {
}
}
catch (Exception e) {
if(portId==null){
System.out.println("portId==null");
}
System.out.println(portId.getName());
System.out.println("初始化串口失败");
e.printStackTrace();
if (serialPort != null) {
serialPort.close();
}
} } public void serialEvent(SerialPortEvent evt) {
switch (evt.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE: {
try {
InputStream input = this.getIn();
int length = input.available();
byte[] image = new byte[length];
input.read(image);
input.close();
ClosePort();
}
catch (Exception e) {
e.printStackTrace();
ClosePort();
}
}
break;
}
} /**
* <br> 功能:本函数向串口发送一个字符串。
* <br> 参数:Msg是需要发送的字符串。
*/
public void WritePort(int[] Msg) {
try {
byte[] aa = new byte[Msg.length];
for(int i = 0;i<Msg.length;i++){
aa[i] = (byte)Msg[i];
}
out.write(aa);
}
catch (IOException e) {
e.printStackTrace();
}
} /**
* <br> 功能:本函数从串口读取一个字符。
* <br> 参数:
*/
public Character ReadOneFromPort() {
Character c = null;
try { c = Character.valueOf( (char) in.read());
return c.charValue();
}
catch (IOException e) {
return null;
}
}
public int[] readAll(){
int[] a=new int[8];
try {
int i=0;
while (in.read()!= -1) {
a[i] = in.read();
System.out.println(a[i]);
i++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a;
}
public byte[] reader(){
//int[] a = new int[]{};
//java.io.BufferedInputStream r = new java.io.BufferedInputStream(in);
java.io.DataInputStream r = new java.io.DataInputStream(new BufferedInputStream(in,1024));
StringBuffer buf = new StringBuffer();
byte[] d = new byte[30];
int t;
try { for(int i=0;i<100;i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t = r.read();
Integer t1 = new Integer(t);
System.out.println(t1.toHexString(t));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* <br> 功能:本函数从串口读取指定长度的字符。
* <br> 参数:Msg是需要发送的字符串。
*/
public String ReadMoreFromPort(int length) {
StringBuffer strbuff = new StringBuffer();
try {
for (int i = 0; i < length; i++) {
strbuff.append(ReadOneFromPort().charValue());
}
return strbuff.toString();
}
catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* <br>功能:停止串口检测进程并关闭串口.
*/ public void ClosePort() {
if (serialPort != null) {
serialPort.close();
}
} public void setPortId(CommPortIdentifier portId) {
this.portId = portId;
} public void setSerialPort(SerialPort serialPort) {
this.serialPort = serialPort;
} public void setOut(OutputStream out) {
this.out = out;
} public void setIn(InputStream in) {
this.in = in;
} public void setPortName(String PortName) {
this.PortName = PortName;
} public CommPortIdentifier getPortId() {
return portId;
} public SerialPort getSerialPort() {
return serialPort;
} public OutputStream getOut() {
return out;
} public InputStream getIn() {
return in;
} public String getPortName() {
return PortName;
}
}
方法中我把十进制转化成了16进制。
现在的问题是:我仅仅输出了个字符出来,也就是一个字节的长度的。
我的具体的结果如下:、
68
1
2
3
4
5
6
68
但是本身需要的更长的数据,是怎么就丢失了呢?public void reader(){
java.io.DataInputStream r = new java.io.DataInputStream(new BufferedInputStream(in));
int t;
try { for(int i=0;i<100;i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
t = r.read();
Integer t1 = new Integer(t);
System.out.println(t1.toHexString(t));
}
} catch (IOException e) {
e.printStackTrace();
}
}
注:我是从串口中读取的数据变成的io流。
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
private InputStream in; //输入流
in = serialPort.getInputStream();我可以链接上com1,也能向com1中发送命令,但是读取的数据只有8位,后面不能读出。
串口的代码如下:
[code=Java]
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.TooManyListenersException;import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;public class SerialBean
implements SerialPortEventListener {
// static{
// System.load("D:/apache-tomcat-6.0.14/bin/win32com.dll");
// System.out.println("加载成功");
// }
private String PortName = ""; //串口名称
private CommPortIdentifier portId; //串口管理器
private SerialPort serialPort; //串口对象 private OutputStream out; //输出流
private InputStream in; //输入流 /**
*<br>功能:本函数构造一个指向特定串口的SerialBean,该串口由参数PortID所指定。PortID = 1 表示COM1,PortID = 2 表示COM2,由此类推。
*/ public SerialBean(int portid) {
PortName = "COM" + portid;
//初始化串口
try {
portId = CommPortIdentifier.getPortIdentifier(PortName);
if(portId==null){
System.out.println("portId==null");
}
//System.out.println(portId.getName());
try {
serialPort = (SerialPort) portId.open("com", 1000);
// System.out.println(serialPort);
}
catch (PortInUseException e) {
System.out.println("串口已被使用");
System.out.println(serialPort);
serialPort.close();
} //用InputStream in去读取串口数据, 用OutputStream out向串口写数据.
try {
in = serialPort.getInputStream();
out = serialPort.getOutputStream(); }
catch (IOException e) {
serialPort.close();
} try {
serialPort.addEventListener(this);
/*注册一个SerialPortEventListener事件来监听串口事件*/
serialPort.notifyOnDataAvailable(true); /*数据可用*/
}
catch (TooManyListenersException e) {
serialPort.close();
} //初始化端口状态: 9600, 8, 1, none.
try {
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {
}
}
catch (Exception e) {
if(portId==null){
System.out.println("portId==null");
}
System.out.println(portId.getName());
System.out.println("初始化串口失败");
e.printStackTrace();
if (serialPort != null) {
serialPort.close();
}
} } public void serialEvent(SerialPortEvent evt) {
switch (evt.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE: {
try {
InputStream input = this.getIn();
int length = input.available();
byte[] image = new byte[length];
input.read(image);
input.close();
ClosePort();
}
catch (Exception e) {
e.printStackTrace();
ClosePort();
}
}
break;
}
} /**
* <br> 功能:本函数向串口发送一个字符串。
* <br> 参数:Msg是需要发送的字符串。
*/
public void WritePort(int[] Msg) {
try {
byte[] aa = new byte[Msg.length];
for(int i = 0;i<Msg.length;i++){
aa[i] = (byte)Msg[i];
}
out.write(aa);
}
catch (IOException e) {
e.printStackTrace();
}
} /**
* <br> 功能:本函数从串口读取一个字符。
* <br> 参数:
*/
public Character ReadOneFromPort() {
Character c = null;
try { c = Character.valueOf( (char) in.read());
return c.charValue();
}
catch (IOException e) {
return null;
}
}
public int[] readAll(){
int[] a=new int[8];
try {
int i=0;
while (in.read()!= -1) {
a[i] = in.read();
System.out.println(a[i]);
i++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a;
}
public byte[] reader(){
//int[] a = new int[]{};
//java.io.BufferedInputStream r = new java.io.BufferedInputStream(in);
java.io.DataInputStream r = new java.io.DataInputStream(new BufferedInputStream(in,1024));
StringBuffer buf = new StringBuffer();
byte[] d = new byte[30];
int t;
try { for(int i=0;i<100;i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t = r.read();
Integer t1 = new Integer(t);
System.out.println(t1.toHexString(t));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* <br> 功能:本函数从串口读取指定长度的字符。
* <br> 参数:Msg是需要发送的字符串。
*/
public String ReadMoreFromPort(int length) {
StringBuffer strbuff = new StringBuffer();
try {
for (int i = 0; i < length; i++) {
strbuff.append(ReadOneFromPort().charValue());
}
return strbuff.toString();
}
catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* <br>功能:停止串口检测进程并关闭串口.
*/ public void ClosePort() {
if (serialPort != null) {
serialPort.close();
}
} public void setPortId(CommPortIdentifier portId) {
this.portId = portId;
} public void setSerialPort(SerialPort serialPort) {
this.serialPort = serialPort;
} public void setOut(OutputStream out) {
this.out = out;
} public void setIn(InputStream in) {
this.in = in;
} public void setPortName(String PortName) {
this.PortName = PortName;
} public CommPortIdentifier getPortId() {
return portId;
} public SerialPort getSerialPort() {
return serialPort;
} public OutputStream getOut() {
return out;
} public InputStream getIn() {
return in;
} public String getPortName() {
return PortName;
}
}
package cn.superweb.tools;
public class KongTiaoService {
SerialBean sb ;
// public static final int[] ADDPOWER = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x98,0x83,0x16};//
public static final int[] READ = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x02,0x00,0xE7,0x16}; //读命令
public static final int[] OPEN = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x11,0xfc,0x16}; //开机
public static final int[] CLOSE = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x00,0xeb,0x16}; //关机
public static final int[] EIGHTEEN = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x11,0xfc,0x16}; //制冷高风18度
public static final int[] NINETEEN = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x12,0xfd,0x16}; //制冷高风19
public static final int[] TWENTY_ = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x13,0xfe,0x16}; //制冷高风20
public static final int[] TWENTY_ONE = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x14,0xff,0x16}; //制冷高风21
public static final int[] TWENTY_TWO = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x15,0x00,0x16}; //制冷高风22
public static final int[] TWENTY_THREE = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x08,0xf3,0x16}; //制冷自动23
public static final int[] TWENTY_FOUR = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x11,0xfc,0x16}; //24
public static final int[] TWENTY_FIVE = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x11,0xfc,0x16}; //25
public static final int[] TWENTY_SIX = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x43,0x2e,0x16}; //通风高风26
public static final int[] TWENTY_SENVEN = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x98,0x83,0x16}; //制热低风27
public static final int[] TWENTY_EIGHT = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x11,0xfc,0x16}; //28
public static final int[] TWENTY_NINE = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x6f,0x5a,0x16}; //制热自动29
public static final int[] THIRTY_ = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x11,0xfc,0x16}; //30
public static final int[] THIRTY_ONE = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x11,0xfc,0x16}; //31
public static final int[] THIRTY_TWO = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x11,0xfc,0x16}; //32
public static final int[] THIRTY_THREE = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x11,0xfc,0x16}; //33
public void open(){
int[] con_flag = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x98,0x83,0x16};
connComm(con_flag);
}
public void connComm(int[] con_flag){
sb = new SerialBean(1);
sb.WritePort(con_flag);
}
public void close(){
int[] con_flag = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x98,0x83,0x16};
connComm(con_flag);
}
public void sendTo(int[] commands){
this.connComm(commands);
}
public void up(){
int[] con_flag = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x98,0x83,0x16};
connComm(con_flag);
}
public void down(){
int[] con_flag = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x98,0x83,0x16};
connComm(con_flag);
}
public void set(){
int[] con_flag = {0x68,0x01,0x02,0x03,0x04,0x05,0x06,0x68,0x05,0x01,0x98,0x83,0x16};
connComm(con_flag);
}
// public void read(){
// int[] a = sb.readAll();
// System.out.println(a.length);
// for(int i=0;i<a.length+1;i++)
// System.out.println(a[i]+"d");
// }
// public String read(){
// return null;
// }
public void read(){
sb.reader();
}
public static void main(String[] args) {
KongTiaoService kt = new KongTiaoService();
System.out.println("读前");
kt.sendTo(KongTiaoService.READ);
System.out.println("loading...");
kt.read();
System.out.println("读数完毕");
}
}
下面简单的介绍一下我要实现的一个功能:
我需要用到电脑的一个com口,然后去链接外部的单片机类型的硬件设备。
然后从com口读取数据和发送数据,也就是输入和输入
数据流的形式用的是java的I/O流
基本的InputStream和OutputStream,结合数据的缓冲。java.io.DataInputStream r = new java.io.DataInputStream(new BufferedInputStream(in,1024)); 最为重要的一点是,需要下载comm.jar和javax.comm.properties,并且把它们放在正确的位置,这个很重要很重要。
把comm.jar和javax.comm.properties,放在你的java的jdk目录下的jre目录下的lib下面。就OK了。这样就可以在tomcat的环境中运行了
当然用java的main方法中也是可以使用的。
我的表达能力有限,如果不明白可以加去QQ群26572256,我是群主,来问这个问题,我会耐心回答,因为这个问题困扰了我几天。
祝你幸运。
我更正下。lz看下是不是这样。
javax.comm在windows下是3个文件。comm.jar、javax.comm.properties及win32com.dll
comm.jar是开发包、javax.comm.properties是comm配置文件,开发包是需要放到项目的lib,并导入构建路径的。当然lz说放在jre的lib下也是可以的。但是注意。如果仅放到jdk的jre下,在使用独立jre进行项目调试的时候会仍然会找不到。我一直的做法是comm.jar及javax.comm.properties包放在项目中。而其他两个文件放到jre中。注意是jre。不是jdk的jre。win32com.dll是comm在windows下的驱动。需要放到jre的bin文件夹下。项目在部署的时候一般给用户配置的都是独立的jre所以将comm.jar及properties文件放到项目中能够减少安装文件编写的复杂度。这样在进行串口的安装的时候只需要将wind32com.dll文件复制到之前安装的jre文件目录下的bin下就可以。
应接收一串数据后判断数据是否正确,正确再进一步处理!int mycount;//数据计数
int busy=0;//数据处理标志,0为数据处理中,1为空闲
public byte[] reader(){
java.io.DataInputStream r = new java.io.DataInputStream(new BufferedInputStream(in,1024));
int t;
try {
t = r.read();
Integer t1 = new Integer(t);
///////////////////////////////////////////////////
/*
在这里可以设置你的数据格式,如下面的数据格式为:引导位\数据体\停止位
引导位为"S" 停止位为"E" 数据体长度mycount为10-1=9
*/
if(t1.toHexString(t)=="S" && mycount==0 && busy==1){mycount=1;}
else {if(mycount== 10 &&t1.toHexString(t)=="E"){
mycount=0;
busy=0;
XXXXXX();//处理数据或关闭串等的函数
}
esle {
mycount++;
//保你的数据
}
// System.out.println(t1.toHexString(t));
}
//////////////////////////////////////////////////
} catch (IOException e) {
e.printStackTrace();
}
return null;
} public void serialEvent(SerialPortEvent evt) {
switch (evt.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE: {
try {
/* InputStream input = this.getIn();
int length = input.available();
byte[] image = new byte[length];
input.read(image);
input.close();*///这里使你的数据变得很慢
this.reader();
// ClosePort();//这里使你接收到一个数据就关串口
}
catch (Exception e) {
e.printStackTrace();
ClosePort();
}
}
break;
}
}