
JAVA OutputStream写入串口
packagecomread;importcomread.StringToHex;importjava.io.*;importjava.util.*;importjava...
package comread;
import comread.StringToHex;
import java.io.*;
import java.util.*;
import javax.comm.*;
/**
*
* @author Administrator
*/
public class ComRead implements Runnable{
static CommPortIdentifier portId;
static Enumeration portList;//枚举类
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
StringToHex sHex;
public void start(){
portList=CommPortIdentifier.getPortIdentifiers();//利用枚举类型得到所有通信端口
while(portList.hasMoreElements()){
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){//如果为串口的话
///System.out.println(portId.getName());
if(portId.getName().equals("COM5")){//如果是COM3,这里需要你自己设定指定的串口
try{
//打开串口
serialPort=(SerialPort)portId.open("Main",2000);
}catch(PortInUseException e){}
try {
inputStream = serialPort.getInputStream();/*获取端口的输入流对象*/
} catch (IOException e) {}
}// end if
}// end if
}// end while
try{
readThread = new Thread(this);
//线程负责每发送一次数据,休眠2秒钟
readThread.start();
}
catch (Exception e) { }
}
public void run(){
try{
serialPort.setSerialPortParams(19200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}catch (UnsupportedCommOperationException e) { }
byte[] readB=new byte[21];
byte[] readC=new byte[21];
int nBytes=0;
String Buff="";
String Buffer="";
try{
while(inputStream.available()>0){
nBytes = inputStream.read(readB);
}
sHex.printHexString(readB);//将读出的字符数组数据,直接转换成十六进制。
}catch(IOException e){
System.err.println(e.toString());
}
try {
Thread.sleep(2000); //读取数据成功,继续start
} catch (InterruptedException e) { }
start();
}
}
进制转换
package comread;
/**
*
* @author Administrator
*/
public class StringToHex {
public static void printHexString( byte[] b) {
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF)+" ";
if (hex.length() == 2) {
hex = '0' + hex;
}
System.out.print(hex.toUpperCase() );
}
System.out.println("\t\n");
}
}
我想再第一部分代码中添加一个写入功能要求写入的是16进制,谢谢各位哥哥了 展开
import comread.StringToHex;
import java.io.*;
import java.util.*;
import javax.comm.*;
/**
*
* @author Administrator
*/
public class ComRead implements Runnable{
static CommPortIdentifier portId;
static Enumeration portList;//枚举类
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
StringToHex sHex;
public void start(){
portList=CommPortIdentifier.getPortIdentifiers();//利用枚举类型得到所有通信端口
while(portList.hasMoreElements()){
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){//如果为串口的话
///System.out.println(portId.getName());
if(portId.getName().equals("COM5")){//如果是COM3,这里需要你自己设定指定的串口
try{
//打开串口
serialPort=(SerialPort)portId.open("Main",2000);
}catch(PortInUseException e){}
try {
inputStream = serialPort.getInputStream();/*获取端口的输入流对象*/
} catch (IOException e) {}
}// end if
}// end if
}// end while
try{
readThread = new Thread(this);
//线程负责每发送一次数据,休眠2秒钟
readThread.start();
}
catch (Exception e) { }
}
public void run(){
try{
serialPort.setSerialPortParams(19200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}catch (UnsupportedCommOperationException e) { }
byte[] readB=new byte[21];
byte[] readC=new byte[21];
int nBytes=0;
String Buff="";
String Buffer="";
try{
while(inputStream.available()>0){
nBytes = inputStream.read(readB);
}
sHex.printHexString(readB);//将读出的字符数组数据,直接转换成十六进制。
}catch(IOException e){
System.err.println(e.toString());
}
try {
Thread.sleep(2000); //读取数据成功,继续start
} catch (InterruptedException e) { }
start();
}
}
进制转换
package comread;
/**
*
* @author Administrator
*/
public class StringToHex {
public static void printHexString( byte[] b) {
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF)+" ";
if (hex.length() == 2) {
hex = '0' + hex;
}
System.out.print(hex.toUpperCase() );
}
System.out.println("\t\n");
}
}
我想再第一部分代码中添加一个写入功能要求写入的是16进制,谢谢各位哥哥了 展开
2个回答
展开全部
在你的代码基础上,加入你想要的代码。你看看,其实在串口的读取流的地方还能通过getOutputStream取得写入流。
while (en.hasMoreElements()) {
portId = en.nextElement();
try {
tempPort = portId.open("temp", 500);
if (!(tempPort instanceof SerialPort))
continue;
serialPort = (SerialPort) tempPort;
os = serialPort.getOutputStream();
int[] temp = {0xdd,0x33,0x44}; //这里写入你要向串口里写入的16进制数。16进制的数,你自己可以得到。
os.write(temp[0]);
os.write(temp[1]);
os.write(temp[2]);
// 如果有继续写。
os.flush(); // 将写入的缓存里的字符一次性写到串口里。
is = serialPort.getInputStream();
while (en.hasMoreElements()) {
portId = en.nextElement();
try {
tempPort = portId.open("temp", 500);
if (!(tempPort instanceof SerialPort))
continue;
serialPort = (SerialPort) tempPort;
os = serialPort.getOutputStream();
int[] temp = {0xdd,0x33,0x44}; //这里写入你要向串口里写入的16进制数。16进制的数,你自己可以得到。
os.write(temp[0]);
os.write(temp[1]);
os.write(temp[2]);
// 如果有继续写。
os.flush(); // 将写入的缓存里的字符一次性写到串口里。
is = serialPort.getInputStream();
更多追问追答
追问
您好,请问能否留下QQ号我想详细请教一下
追答
留你的Q。我加你。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?

2023-06-12 广告
单片机,即单片微控制器,也称为单片微型计算机,是将中央处理器(CPU)、存储器(ROM,RAM)、输入/输出接口和其他功能部件集成在一块 在一个小块的集成电路上,从而实现对整个电路或系统的数字式控制。单片机不是完成某一个逻辑功能的芯片,而是...
点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询