通讯协议:
1.UUID(0xAAF1)用于接收数据,UUID(0xAAF2)用于发送数据,
2.发送:数据格式5字节(0xAA,单位,高字节,低字节,校验和),16进制表示:
2.1其中0xAA表示传输的头字节
2.2压力区间,0-3 :一般取压力值为1的区间。
2.3数据用两个字节表示比如0x1234,高字节0x12,低字节0x34 (0x12<<8 + 0x34)
2.4校验和前面4个数的和,取低字节(尾字节)表示!!!
2.5例子:0xAA,0x01,0x03,0x1F,0xCD
AA 01 03 1F 9D
解释:头0xAA,压力区间1,数据0x31F,校验和0xCD
2.6 数值计算 03 1F 十进制为799
换算公式:{(4096-799)/799}*680=2806
所以,此值为2806K的阻抗(有效范围100--3000)。
累计二十个值,两两之间误差在30(需要继续精确)以内
主要是在从串口读取数据时根据需求对数据进行解析
SerialTool.java中的从串口读取数据方法
public static String readFromPortSpecial(SerialPort serialPort)
throws ReadDataFromSerialPortFailure,
SerialPortInputStreamCloseFailure {
String lastData = null;
InputStream in = null;
byte[] bytes = null;
try {
in = serialPort.getInputStream();
// 获取buffer里的数据长度
int bufflenth = in.available();
//System.out.println("bufflenth:"+bufflenth);
while (bufflenth != 0) {
// 初始化byte数组为buffer中数据的长度
bytes = new byte[bufflenth];
in.read(bytes);
bufflenth = in.available();
if (bytes!=null && bytes.length>0) {
for (int i = 0; i < bytes.length-1; i++) {
if (bytes.length-1-i>=5) {
Integer byteInt0 = Integer.parseInt(Integer.toString(0XFF & bytes[i+0], 10));
Integer byteInt1 = Integer.parseInt(Integer.toString(0XFF & bytes[i+1], 10));
if (byteInt0!=170) {
continue;
}
else {
if(byteInt1==170){
continue;
}
}
Integer byteInt2 = Integer.parseInt(Integer.toString(bytes[i+2]<<8, 10));
Integer byteInt20 = Integer.parseInt(Integer.toString(bytes[i+2] & 0XFF, 10));
Integer byteInt3 = Integer.parseInt(Integer.toString(bytes[i+3] & 0XFF, 10));
Integer byteInt4 = Integer.parseInt(Integer.toString(0XFF & bytes[i+4], 10));
if (byteInt0+byteInt1+byteInt20+byteInt3==byteInt4) {
System.out.println("++++++有效值:"+((int)(((4096f-(byteInt2+byteInt3))/(byteInt2+byteInt3))*680))+"+++压力值:"+byteInt1);
Integer integer = (int)(((4096f-(byteInt2+byteInt3))/(byteInt2+byteInt3))*680);
if (integer>=100 && integer<=3000) {
arrayList.add(integer);
if (arrayList.size()==20) {
for (int k = 0; k <= arrayList.size()-2; k++) {
if (Math.abs(arrayList.get(k+1)-arrayList.get(k))<=30) {
lastData = arrayList.get(k).toString();
System.out.println("测量成功:值为"+lastData);
arrayList.clear();
break;
}else {
arrayList.clear();
}
}
}
}else {
arrayList.clear();
}
}
i+=4;
}
}
}
}
} catch (IOException e) {
throw new ReadDataFromSerialPortFailure();
} finally {
try {
if (in != null) {
in.close();
in = null;
}
} catch (IOException e) {
throw new SerialPortInputStreamCloseFailure();
}
}
return lastData;
}
debugDemo.java直接指定串口 波特率 打开串口 向串口发送数据
/*
* MainFrame.java
*
* Created on 2016.8.19
*/
package com.huidaifu.serialport.ui;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.huidaifu.serialport.exception.NoSuchPort;
import com.huidaifu.serialport.exception.NotASerialPort;
import com.huidaifu.serialport.exception.PortInUse;
import com.huidaifu.serialport.exception.ReadDataFromSerialPortFailure;
import com.huidaifu.serialport.exception.SendDataToSerialPortFailure;
import com.huidaifu.serialport.exception.SerialPortInputStreamCloseFailure;
import com.huidaifu.serialport.exception.SerialPortOutputStreamCloseFailure;
import com.huidaifu.serialport.exception.SerialPortParameterFailure;
import com.huidaifu.serialport.exception.TooManyListeners;
import com.huidaifu.serialport.manage.SerialPortManager;
import com.huidaifu.serialport.utils.ByteUtils;
import com.huidaifu.serialport.utils.ShowUtils;
import com.huidaifu.util.JsonData;
/**
* 主界面
*
* @author yangle
*/
public class debugDemo extends JFrame {
public static SerialPort serialport;
public static void main(String[] args) throws ReadDataFromSerialPortFailure, SerialPortInputStreamCloseFailure {
Map<String, Object> outMap = new HashMap<String, Object>();
byte[] data = null;
String stringData = null;
//打开串口
String commName = "COM6";
int baudrate = 115200;
SerialPortManager.stringData=null;
try {
serialport = SerialPortManager.openPort(commName, baudrate);
if (serialport!=null) {
while( stringData==null ){
SerialPortManager.stringData=null;
stringData = SerialPortManager.readFromPortSpecial(serialport);
System.out.print("");
}
}
} catch (SerialPortParameterFailure e) {
e.printStackTrace();
} catch (NotASerialPort e) {
e.printStackTrace();
} catch (NoSuchPort e) {
e.printStackTrace();
} catch (PortInUse e) {
e.printStackTrace();
}
int data0 = 0xAA;
int data1 = 0x01;
int data2 = 0x01;
int data3 = 0xAC;
byte[] order = new byte[4];
order[0] = (byte) data0;
order[1] = (byte) data1;
order[2] = (byte) data2;
order[3] = (byte) data3;
try {
SerialPortManager.sendToPort(serialport,order);
} catch (SendDataToSerialPortFailure e) {
e.printStackTrace();
} catch (SerialPortOutputStreamCloseFailure e) {
e.printStackTrace();
}
//关闭串口
SerialPortManager.closePort(serialport);
}
}