Android/信捷plc modbus-ASCII串口通信
- modbus协议封装
- 串口通信
- 通信队列
终于有时间总结一下用到的技术了,之前忙得狗血淋头,搞这个plc弄到自闭,由于没有百度到任何案例,遂自己花大量的精力给弄出来并应用到运营项目中,给予各位参考
modbus协议封装
modbus是一种通讯规约,简单的来说,由起始符、帧头、寄存器命令、LRC校验、结束符等组成,此文中,modbus主要用于和信捷plc通讯,采用ascll编码方式(RTU类似,自行修改代码中转码部分即可),另外,因为是规约,所以得需要与你的上位机PLC一起对接,例如本文中规约为:寄存器地址长度为4-“0001”,通讯命令长度为1-“1”,意味着,像寄存器0001发送数据1,来完成对应的操作。
modbus封装类:
public class PlcModBusUtil {
//起始符
private final String START = "3A";
//帧头
private final String FRAME_HEADER = "3032";
//写多个寄存器命令
private final String WRITE_COMMAND = "3130";
//写单个寄存器
private final String WRITE_SIGNLE_COMMAND = "3036";
//读命令
private final String READ_COMMAND = "3033";
//结束符
private final String END = "0D0A";
public static final String TAG = "PlcModBusUtil";
/**
* 封装modbus,写请求协议
*
* @return
*/
public String getWriteString(String address, String[] data) {
StringBuilder stringBuilder = new StringBuilder();
//起始符
stringBuilder.append(START);
//帧头
stringBuilder.append(FRAME_HEADER);
//功能码
if (data.length > 1) {
stringBuilder.append(WRITE_COMMAND);
} else {
stringBuilder.append(WRITE_SIGNLE_COMMAND);
}
//地址,多个地址,只传起始地址,16进制转ascii
stringBuilder.append(hexToAscii(address));
//多个地址与数据,需要添加长度和位数
if (data.length>1){
//长度,例如:写2条数据,长度为2,位数为4,长度补足4位,位数补足2位
StringBuilder lengthBuilder = new StringBuilder();
lengthBuilder.append(data.length);
int length = lengthBuilder.length();
for (int i = 0; i < 4 - length; i++) {
lengthBuilder.insert(0, "0");
}
//转ascii
stringBuilder.append(hexToAscii(lengthBuilder.toString()));
//位数
StringBuilder digitsBuilder = new StringBuilder();
digitsBuilder.append(data.length * 2);
int digitsLength = digitsBuilder.length();
for (int i = 0; i < 2 - digitsLength; i++) {
digitsBuilder.insert(0, "0");
}
//转ascii
stringBuilder.append(hexToAscii(digitsBuilder.toString()));
}
//写入数据处理
for (String datum : data) {
//10进制转16进制
StringBuilder hexString = new StringBuilder();
hexString.append(datum);
int hexLength = hexString.length();
//转化后的16进制进行补位,目标为4位
for (int i = 0; i < 4 - hexLength; i++) {
hexString.insert(0, "0");
}
//转ascii,添加数据
stringBuilder.append(hexToAscii(hexString.toString()));
}
//LRC校验结果生成,去掉起始
stringBuilder.append(getLrc(stringBuilder.substring(START.length())));
//添加结束符
stringBuilder.append(END);
return stringBuilder.toString();
}
public String getReadString(String[] address) {
StringBuilder stringBuilder = new StringBuilder();
//起始符
stringBuilder.append(START);
//帧头
stringBuilder.append(FRAME_HEADER);
//读命令
stringBuilder.append(READ_COMMAND);
//地址,多个地址,只传起始地址,16进制转ascii
stringBuilder.append(hexToAscii(address[0]));
//长度,例如:写读条数据,长度为2,长度补足4位 位数为4,位数补足2位
StringBuilder lengthBuilder = new StringBuilder();
lengthBuilder.append(address.length);
int length = lengthBuilder.length();
for (int i = 0; i < 4 - length; i++) {
lengthBuilder.insert(0, "0");
}
//转ascii
stringBuilder.append(hexToAscii(lengthBuilder.toString()));
//LRC校验结果生成,去掉起始
stringBuilder.append(getLrc(stringBuilder.substring(START.length())));
//添加结束符
stringBuilder.append(END);
return stringBuilder.toString();
}
//返回,是否是读请求的返回数据
public boolean isReadRequest(String readString) {
if (readString.length() < 30) {
System.out.println("收到的数据响应格式不正确");
return false;
}
String startString = START + FRAME_HEADER;
String command = readString.substring(startString.length(), startString.length()+READ_COMMAND.length() );
return command.equals(READ_COMMAND);
}
public String[] formatReadString(String readString) {
String startString = START + FRAME_HEADER + READ_COMMAND;
if (readString.startsWith(startString)) {
if (readString.length() < 30) {
System.out.println("收到的数据响应格式不正确");
return null;
}
String contentString = readString.substring(startString.length() + 4, readString.length() - 8);
//数据为8位一组
if (contentString.length() % 8 != 0) {
System.out.println("收到的数据响应格式不正确");
return null;
}
int length = contentString.length() / 8;
String[] contentArray = new String[length];
for (int i = 0; i < length; i++) {
//8位结果
String data = contentString.substring(i * 8, i * 8 + 8);
//ascii转16进制
String hexString = asciiToHex(data);
//16进制转10进制
contentArray[i] = num16to10(hexString) + "";
}
return contentArray;
}
Flog.e(TAG, "无效数据");
return null;
}
private String getLrc(String hexdata) {
StringBuilder rtu = new StringBuilder();
int dealLength = hexdata.length() / 2;
for (int i = 0; i < dealLength; i++) {
String stringAscill = hexdata.substring(2 * i, 2 * i + 2);
rtu.append(Integer.parseInt(stringAscill) - 30);
}
int rtuLength = rtu.length() / 2;
int sum = 0;
for (int i = 0; i < rtuLength; i++) {
sum += Integer.parseInt(rtu.substring(2 * i, 2 * i + 2), 16);
}
//求和结果,补位成4位
StringBuilder sumBuilder = new StringBuilder();
sumBuilder.append(num10to16(sum).toUpperCase());
int sumBuilderLength = sumBuilder.length();
for (int i = 0; i < 4 - sumBuilderLength; i++) {
sumBuilder.insert(0, "0");
}
//补为4位的 只取低位
String result = sumBuilder.substring(2, sumBuilder.length());
return hexToAscii(num10to16(Integer.parseInt("100", 16) - Integer.parseInt(result, 16)).toUpperCase());
}
private String hexInt(int total) {
int a = total / 256;
int b = total % 256;
if (a > 255) {
return hexInt(a) + format(b);
}
return format(a) + format(b);
}
private String format(int hex) {
String hexa = Integer.toHexString(hex);
int len = hexa.length();
if (len < 2) {
hexa = "0" + hexa;
}
return hexa;
}
/**
* 10进制转16进制
*
* @param num
* @return
*/
private String num10to16(Integer num) {
return Integer.toHexString(num);
}
/**
* 16进制转10进制
*/
private int num16to10(String hexString) {
int result = 0;
try {
result = new BigInteger(hexString, 16).intValue();
} catch (Exception e) {
Flog.e(TAG, "数据解析失败");
}
return result;
}
/**
* 16进制转ascii
*
* @param hex
* @return
*/
public String hexToAscii(String hex) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < hex.length(); i++) {
char a = hex.charAt(i);
int aInt = (int) a;
stringBuilder.append(num10to16(aInt));
}
return stringBuilder.toString();
}
/**
* ascii转16进制
*
* @param str
* @return
*/
private String asciiToHex(@NotNull String str) {
byte[] hex = fromHex(str);
StringBuilder result = new StringBuilder();
for (byte b : hex) {
result.append((char) b);
}
return result.toString();
}
/**
* 字节数组转16进制
*
* @param bytes
* @return
*/
public String toHex(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
byte[] arr = bytes;
int len = bytes.length;
for (int i = 0; i < len; ++i) {
byte b = arr[i];
sb.append(String.format("%02x", b));
}
return sb.toString().toUpperCase();
}
/**
* string转16进制字节数组
*
* @param string
* @return
*/
public byte[] fromHex(String string) {
byte[] result = new byte[string.length() / 2];
for (int i = 0; i < result.length; ++i) {
try {
result[i] = (byte) Integer.parseInt(string.substring(i * 2, i * 2 + 2), 16);
} catch (Exception e) {
Flog.e(TAG, "数据解析失败");
}
}
return result;
}
/**
* hex字符串转byte数组
*
* @param inHex 待转换的Hex字符串
* @return 转换后的byte数组结果
*/
public byte[] hexToByteArray(String inHex) {
int hexlen = inHex.length();
byte[] result;
if (hexlen % 2 == 1) {
//奇数
hexlen++;
result = new byte[(hexlen / 2)];
inHex = "0" + inHex;
} else {
//偶数
result = new byte[(hexlen / 2)];
}
int j = 0;
for (int i = 0; i < hexlen; i += 2) {
result[j] = hexToByte(inHex.substring(i, i + 2));
j++;
}
return result;
}
/**
* Hex字符串转byte
*
* @param inHex 待转换的Hex字符串
* @return 转换后的byte
*/
public byte hexToByte(String inHex) {
return (byte) Integer.parseInt(inHex, 16);
}
/**
* 字节数组转16进制
*
* @param bytes 需要转换的byte数组
* @return 转换后的Hex字符串
*/
public String bytesToHex(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() < 2) {
sb.append(0);
}
sb.append(hex);
}
return sb.toString();
}
}
关键代码都有注释,不再进行解释了
串口通信
串口通讯主要使用github上的开源项目AndroidSerialPort :“com.github.kongqw:AndroidSerialPort:1.0.1”
import进项目中后,串口通讯封装类:
class PlcSeriaPortService {
var mSerialPortManager: SerialPortManager? = null
//地址数组,用于读取返回数据
var addressArray: Array<String>? = null
companion object {
val TAG = this.javaClass.name
}
fun openSeriaPort(): Boolean {
/**
* 打开串口
* @param device 串口设备文件
* @param baudRate 波特率
* @param parity 奇偶校验,0 None(默认); 1 Odd; 2 Even
* @param dataBits 数据位,5 ~ 8 (默认8)
* @param stopBit 停止位,1 或 2 (默认 1)
* @param flags 标记 0(默认)
*/
try {
mSerialPortManager = SerialPortManager()
mSerialPortManager!!.openSerialPort(
File(AppSetting.seriaPort),
AppSetting.baudrate
)
return true
} catch (e: Exception) {
return false
}
}
fun sendWriteRequest(address: String, content: Array<String>): Boolean {
val sendContent = PlcModBusUtil().getWriteString(
address, content
)
try {
mSerialPortManager?.sendBytes(PlcModBusUtil().fromHex(sendContent))
Flog.d(TAG, "请求串口写入指令成功:" + sendContent)
return true
} catch (e: Exception) {
e.printStackTrace()
return false
}
}
fun sendWriteRequest(address: String, content: String): Boolean {
val strArray = Array(1) { "" }
strArray.set(0, content)
return sendWriteRequest(address, strArray)
}
fun sendReadRequest(addressArray: Array<String>): Boolean {
this.addressArray = addressArray
val sendContent = PlcModBusUtil().getReadString(addressArray)
try {
mSerialPortManager?.sendBytes(PlcModBusUtil().fromHex(sendContent))
// Flog.d(TAG, "请求串口读取指令成功:" + sendContent)
return true
} catch (e: Exception) {
e.printStackTrace()
return false
}
}
fun sendReadRequest(address: String): Boolean {
val strArray = Array(1) { "" }
strArray.set(0, address)
return sendReadRequest(strArray)
}
fun setOnDataInteractionListener(
onResponseListener: (correctData: Boolean, Map<String, String>?) -> Unit
) {
mSerialPortManager!!.setOnSerialPortDataListener(object : OnSerialPortDataListener {
override fun onDataReceived(bytes: ByteArray) {
val responseString = PlcModBusUtil().bytesToHex(bytes).toUpperCase()
if (PlcModBusUtil().isReadRequest(responseString)) {
//读数据请求返回
val contentMap = mutableMapOf<String, String>()
val contentArray = PlcModBusUtil().formatReadString(responseString)
if (addressArray != null && contentArray != null && addressArray?.size == contentArray.size) {
for ((index, data) in addressArray!!.withIndex()) {
contentMap.put(data, contentArray[index])
}
onResponseListener(true, contentMap)
}
} else {
onResponseListener(false, null)
}
}
override fun onDataSent(bytes: ByteArray) {}
})
}
fun closeSerialPort() {
try {
mSerialPortManager?.closeSerialPort()
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
}
}
1.打开串口时,需要注意串口地址与波特率(与PLC上位机对齐),否则会导致数据传输出问题
2.写数据时,先根据modbus封装类获取到对应的String,然后转为16进制字节数组,再进行串口写操作
3.读操作是一直再进行的,并且受到的数据也是16进制字节数组,调用封装类对字节数组拆解获取到具体传输定义内容,然后进行匹配操作
4.某些硬件在频繁打开串口和关闭的时候,容易出现卡死的问题(所以一般不要去关闭串口~)
通信队列
与上位机的通信特点是,像串口写数据发送指令后,需要等到有了回应再发送 下一个指令,否则可能会出现堵塞或指令得不到响应的情况(与我对接的PLC老哥如此说道),并且他需要每一次我发送完一个操作指令后,将寄存器状态重置,具体看如何与plc开发人员对接,方式不固定;
因此这边使用一个定时器,不断的从集合中取出数据向PLC发送指令,并在得到响应后,再发送下一条,写数据时,只需要从向集合中添加信息即可;
考虑到先进后出的特点,本打算改为栈的方式做,不过因为一些原因换掉了公司,因此也就没那个必要去修改了;
1.指令封装类:
class PlcSeriaPortMessage {
companion object {
const val WRITE = 0
const val READ = 1
}
//消息类型,分读 和写
var type = WRITE
//消息内容,只接受两种格式的定义,String 和String[],用于写数据,读时不需要使用
var content: Any = ""
//寄存器地址,只接受两种格式的定义,String 和String[]
//写多个时,地址为起始地址,地址之间相差为1,address为String类型
//读多个时,address类型为String[]类型,内容为目标地址,地址相差为1,并从小到大排列
var address: Any = ""
}
2.打开串口,初始化指令集合,放入一个读寄存器状态的指令
fun openSeriaPort(
onResponseListener: (correctData: Boolean, Map<String, String>?) -> Unit
) {
//打开Plc通信串口
val openSuccess = plcSeriaPortServices.openSeriaPort()
if (!openSuccess) {
showToast("打开Plc串口失败,请检查连接!")
} else {
//通信串口打开成功,就启动向指定寄存器读取信息的定时器
plcSeriaPortServices.setOnDataInteractionListener(onResponseListener)
//读消息设置,同时向多个地址发送请求时,要求地址相差为1 ,如1000,1001,
//否则应该添加多个消息,不能放到一块处理
plcSeriaPortMessageQueue.add(PlcSeriaPortMessage().apply {
type = PlcSeriaPortMessage.READ
address = arrayOf(
PlcSeriaPortReadEnum.READ_NORMAL_ADDRESS.msg,
PlcSeriaPortReadEnum.READ_ERROR_ADDRESS.msg
)
})
startSeriaPortTimer()
}
}
3.开启定时器,不断的从集合找那个取出指令,并根据状态来进行判断是否进行下一次消息发送,定时器循环频率看PLC设置的刷新率,需要自己调试
fun startSeriaPortTimer() {
plcSeriaPortTimer = Timer()
var sendTime = 0L
plcSeriaPortTimer?.schedule(timerTask {
if (System.currentTimeMillis() - sendTime > 5000 && !couldSendMessage) {
//5秒后 未收到PLC反馈,则再次发送
couldSendMessage = true
}
if (couldSendMessage) {
sendTime = System.currentTimeMillis()
if (plcSeriaPortMessageQueue.size > 0) {
//获取队列中的第一个消息
val message = plcSeriaPortMessageQueue.get(0)
if (message.type == PlcSeriaPortMessage.WRITE) {
//优先写
if (message.content is String) {
if (!plcSeriaPortServices.sendWriteRequest(
message.address as String,
message.content as String
)
) {
showToast("指令发送失败,请检查连接!")
}
} else if (message.content is Array<*>) {
if (!plcSeriaPortServices.sendWriteRequest(
message.address as String,
message.content as Array<String>
)
) {
showToast("指令发送失败,请检查连接!")
}
}
//写请求发送完,则移除队列
plcSeriaPortMessageQueue.removeAt(0)
} else {
//开始读
var readMessage: PlcSeriaPortMessage? = null
//读普通寄存器
readMessage = plcSeriaPortMessageQueue.get(0)
if (readMessage.address is String) {
plcSeriaPortServices.sendReadRequest(readMessage.address as String)
} else if (readMessage.address is Array<*>) {
plcSeriaPortServices.sendReadRequest(readMessage.address as Array<String>)
}
}
//发送完消息,则关闭可发送开关,等待Plc返回
couldSendMessage = false
}
}
}, 1000, 10)
}
4.发送指令
//串口方式Plc发起指令
fun sendSeriaPortMessageToPlc(address: String, string: String) {
//向队列头添加
plcSeriaPortMessageQueue.add(0, PlcSeriaPortMessage().apply {
type = PlcSeriaPortMessage.WRITE
content = string
this.address = address
})
}
通信队列这些代码仅用于提供思路
PS:串口通信有时候可能需要自己接线,一般地线不用动,电源线和数据线某些时候很操蛋的需要交换一下位置,万一这边一直和plc通讯一直不成功,不要一直考虑代码的问题,弄一下线试试,血的教训。