import javax.comm.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
/**
* Created by WuSong
* 2017-05-04
*/
public class Dlt645Test implements SerialPortEventListener{
private ArrayList<String> portList;
private CommPortIdentifier portId;
private SerialPort serialPort;
private OutputStream outputStream;
private InputStream inputStream;
private int packetlength;
static final char[] HEX_CHAR_TABLE = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
public void scanPorts(){
portList = new ArrayList<String>();
Enumeration<?> en = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId;
while(en.hasMoreElements()){
portId = (CommPortIdentifier)en.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
String name = portId.getName();
if(!portList.contains(name)){
portList.add(name);
}
}
}
if(null == portList || portList.isEmpty()){
System.out.println("未找到可用的串行端口号,程序无法启动!");
}
}
public void openSerialPort(String portname){
try{
portId = CommPortIdentifier.getPortIdentifier(portname);
}catch (NoSuchPortException e){
System.out.println("抱歉,没有找到" + portname + "串行端口号!");
return;
}
}
public void setSerialPortParam(int rate,int data,int stop,int parity){
try{
serialPort = (SerialPort)portId.open("test",2000);
}catch (PortInUseException e){
System.out.println(serialPort.getName() +"端口已被占用");
return;
}
try {
serialPort.setSerialPortParams(rate,data,stop,parity);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
try{
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
}catch (IOException e){
e.printStackTrace();
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
serialPort.notifyOnDataAvailable(true);
}
public void sendDataToSerialPort(byte[] b){
try {
outputStream.write(b);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void serialEvent(SerialPortEvent e){
switch (e.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:
// String data = toHexString(getPack());
byte[] msgPack=this.getPack();
System.out.println("**********************************************************");
List list = new ArrayList();
for (int i = 0; i < msgPack.length; i++) {
//System.out.println(msgPack[i]<0?msgPack[i]+256:msgPack[i]);
list.add(Integer.toHexString(msgPack[i]<0?msgPack[i]+256:msgPack[i]));
/*for (int j = i; j <= i; j++) {
if ((list.get(j).toString()).length()==1){
((String) list.get(j)).replace((String)list.get(j),String.format("%02d",Integer.parseInt((String)list.get(j))));
}
}*/
}
Object[] msg=list.toArray(new String[list.size()]);
list=new ArrayList();
for (int i = 0; i < msg.length; i++) {
if (msg[i].toString().length()==1){
list.add(String.format("%02d",Integer.parseInt((String) msg[i])));
}else {
list.add(msg[i]);
}
}
System.out.println("返回数据:" + list.toString().replace(",","").replaceAll("\\[|\\]",""));
AgreementDemo ad=new AgreementDemo();
try {
System.out.println("*************************解析结果*************************");
ad.analysis(list.toString().replace(",","").replaceAll("\\[|\\]",""));
} catch (Exception e1) {
e1.printStackTrace();
System.out.println("解析出错!");
}
}
}
public byte[] getPack(){
byte[] pack1 = new byte[10];
for(int i=0;i<10;i++){
int newData = 0;
try{
if((newData = inputStream.read()) != -1){
// System.out.println("数据:" + newData);
pack1[i] = (byte) newData;
}
}catch (IOException e){
e.printStackTrace();
}
}
int packetLength=Integer.parseInt(String.valueOf(pack1[pack1.length-1]));
byte[] pack2 = new byte[packetLength+2];
//System.out.println(pack2.length);
for(int i=0;i<pack2.length;i++){
int newData = 0;
try{
if((newData = inputStream.read()) != -1){
pack2[i] = (byte) newData;
}
}catch (IOException e){
e.printStackTrace();
}
}
byte[] msgPack = new byte[pack1.length+pack2.length];
System.arraycopy(pack1,0,msgPack,0,pack1.length);
System.arraycopy(pack2,0,msgPack,pack1.length,pack2.length);
System.out.println("数据长度:" + msgPack.length);
return msgPack;
}
public String toHexString(byte[] data){
if(data == null || data.length == 0)
return null;
byte[] hex = new byte[data.length * 2];
int index = 0;
for(byte b : data){
int v = b & 0xFF;
hex[index++] = (byte) HEX_CHAR_TABLE[v >>> 4];
hex[index++] = (byte) HEX_CHAR_TABLE[v & 0xF];
}
return new String(hex);
}
public String toHexString(Byte[] data){
byte[] resultBytes = new byte[data.length];
for(int i =0 ;i<data.length;i++){
resultBytes[i] = data[i];
}
return toHexString(resultBytes);
}
public byte[] hexStringToBytes(String data){
if(data == null || "".equals(data))
return null;
data = data.toUpperCase();
int length = data.length()/2;
char[] dataChars = data.toCharArray();
byte[] byteData = new byte[length];
for (int i = 0;i<length;i++){
int pos = i * 2;
byteData[i] = (byte)(charToByte(dataChars[pos]) << 4 | charToByte(dataChars[pos + 1]));
}
return byteData;
}
public byte charToByte(char c){
return (byte)"0123456789ABCDEF".indexOf(c);
}
/* public int getPacketlength() {
return packetlength;
}
public void setPacketlength(int packetlength) {
this.packetlength = packetlength;
}*/
public static void main(String[] args){
Dlt645Test dlt645 = new Dlt645Test();
没有合适的资源?快使用搜索试试~ 我知道了~
dlt645-2007电能表协议解析源码+串口编程源码
共56个文件
sample:10个
xml:8个
master:4个
3星 · 超过75%的资源 需积分: 5 313 下载量 78 浏览量
2017-05-15
17:47:21
上传
评论 20
收藏 50KB RAR 举报
温馨提示
dlt645-2007电能表协议解析源码+串口编程源码,利用串口编程直接与电能表通信,Java源码
资源推荐
资源详情
资源评论
收起资源包目录
Agreement_wu.rar (56个子文件)
Agreement_wu
out
production
Agreement
config.properties 1KB
Test.class 2KB
AgreementDemo.class 6KB
Dlt645Test.class 8KB
src
config.properties 1KB
Dlt645Test.java 8KB
Test.java 5KB
AgreementDemo.java 7KB
web
WEB-INF
web.xml 304B
index.jsp 314B
.idea
uiDesigner.xml 9KB
libraries
comm.xml 240B
workspace.xml 62KB
vcs.xml 167B
misc.xml 2KB
modules.xml 258B
artifacts
Agreement_war_exploded.xml 511B
Agreement.iml 868B
.git
HEAD 23B
index 437B
FETCH_HEAD 121B
COMMIT_EDITMSG 28B
objects
aa
c886b7c7685556341d68175113e09ecd1d7d9b 45B
77
59d8043d9891cd2098a5c405aa2988122902c1 2KB
a1
f9dd36f17ba563be76497544ed6c1397ddf170 532B
84
4f8e8266500bc37d0ce7d00946cf9e96e61680 126B
pack
db
a4a321ab0a3a5eab85a30ce01ebb5de901f6b8 45B
33
b2e42705becf5cc09cd5315e8ab75ef07557db 164B
14
0afd426a32cd787a1eacbe008d5c06a6f5c1f7 3KB
cf
cb26f59b215d8d89a84310048949337af919fb 1KB
de
c2bfd0fbf4f37a3e4046d1509212d10acd8736 2KB
58
9f5069bb12bc9223d9c23767350d33b6d7ff5f 196B
ff
d8aa2e61f88f9e2dc04a797e237b868ad5e36f 164B
2c
64901d2b747cb5c7c3f71e90c95e1ea648d82d 972B
95
adf7912e4f9a10e9aba9843e57dee89ae27fca 510B
74
75067238de5d4395049fd9a4c8a70deb3deea8 82B
7b
aacf35ece4de30faec1627be54a3e023714ef4 54B
40
84a2d6552e07d165987fb237d69932b03fb61e 128B
info
description 73B
config 312B
info
exclude 240B
hooks
pre-applypatch.sample 424B
pre-commit.sample 2KB
applypatch-msg.sample 478B
pre-rebase.sample 5KB
commit-msg.sample 896B
prepare-commit-msg.sample 1KB
update.sample 4KB
pre-receive.sample 544B
post-update.sample 189B
pre-push.sample 1KB
logs
HEAD 323B
refs
heads
master 323B
remotes
origin
master 433B
refs
tags
heads
master 41B
remotes
origin
master 41B
共 56 条
- 1
奔跑的阳光
- 粉丝: 41
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页