modbus协议使用【android串口通信】
本文的目的是android端与上位机之间使用modbus协议进行串口通信。通过串口与其他设备进行通信,传递数据。可以理解为电脑和键盘、鼠标通信。
关于modbus协议我已经上传
modbus协议下载
串口连接
android端连接串口方法,其实就是设置串口的参数,打开底层的串口文件,android开发的朋友也可以让驱动人员帮忙写,当然谷歌也有官方框架,这里我使用的是第三方框架,参考的是这位大神的文章
参考大神链接
调试精灵
首先在gradle配置:
implementation 'tp.xmaihh:serialport:2.1'
全部源码
import java.io.ByteArrayOutputStream;
public class ByteArrayWriter extends ByteArrayOutputStream {
public ByteArrayWriter() {
super();
}
public void writeInt8(byte b)
{
this.write(b);
}
public void writeInt8(int b)
{
this.write((byte)b);
}
public void writeInt16(int n) {
byte[] bytes = ByteUtil.fromInt16(n);
this.write(bytes, 0, bytes.length);
}
public void writeInt16Reversal(int n){
byte[] bytes=ByteUtil.fromInt16Reversal(n);
this.write(bytes,0,bytes.length);
}
public void writeInt32(int n) {
byte[] bytes = ByteUtil.fromInt32(n);
this.write(bytes, 0, bytes.length);
}
public void writeBytes(byte[] bs,int len){
this.write(bs,0,len);
}
}
public class ByteUtil {
public static String toHexString(byte[] input, String separator) {
if (input == null) return null;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.length; i++) {
if (separator != null && sb.length() > 0) {
sb.append(separator);
}
String str = Integer.toHexString(input[i] & 0xff);
if (str.length() == 1) str = "0" + str;
sb.append(str);
}
return sb.toString();
}
public static String toHexString(byte[] input) {
return toHexString(input, " ");
}
public static byte[] fromInt32(int input) {
byte[] result = new byte[4];
result[3] = (byte) (input >> 24 & 0xFF);
result[2] = (byte) (input >> 16 & 0xFF);
result[1] = (byte) (input >> 8 & 0xFF);
result[0] = (byte) (input & 0xFF);
return result;
}
public static byte[] fromInt32R(int input) {
byte[] result = new byte[4];
result[0] = (byte) ((input >> 24) & 0xFF);
result[1] = (byte) ((input >> 16) & 0xFF);
result[2] = (byte) ((input >> 8) & 0xFF);
result[3] = (byte) (input & 0xFF);
return result;
}
public static byte[] fromInt16(int input) {
byte[] result = new byte[2];
result[0] = (byte) (input >> 8 & 0xFF);
result[1] = (byte) (input & 0xFF);
return result;
}
public static int fromBytes(byte a, byte b) {
return ((a & 0xFF) << 8) + (b & 0xFF);
}
public static byte[] fromInt16Reversal(int input) {
byte[] result = new byte[2];
result[1] = (byte) (input >> 8 & 0xFF);
result[0] = (byte) (input & 0xFF);
return result;
}
public static int byteArrayToInt16(byte[] bytes) {
int value = 0;
for (int i = 0; i < 2; i++) {
int shift = (1 - i) * 8;
value += (bytes[i] & 0xFF) << shift;
}
return value;
}
/**
* byte[]转int
*
* @param bytes 需要转换成int的数组
* @return int值
*/
public static int byteArrayToInt(byte[] bytes) {
int value = 0;
for (int i = 0; i < 4; i++) {
int shift = (3 - i) * 8;
value += (bytes[i] & 0xFF) << shift;
}
return value;
}
}
public class CRC16 {
private static final byte[] crc16_tab_h = {
(byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
(byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80,
(byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0,
(byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00,
(byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
(byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81,
(byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0,
(byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01,
(byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
(byte)<