java Ymodem协议上位机程序
本文代码传入文件地址返回如代码块:
为了更好的拓展,我把foo.c这个文件名拓展成了6个字节用来表示版本号如1.2.66.c
后四个字节用来存储文件长度
也就是说第0帧数据为 SOH 00 FF 1.2.66 1236542 NUL[118] CRC CRC
当然我最后返回的都是10进制的byte数组 可以直接netty发送
代码:
package com.ruoyi.netty.netty;
import com.ruoyi.netty.netty.domain.ByteUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.FileCopyUtils;
import java.io.*;
/**
* @ClassName: Ymodem
* @Description: Ymodem协议工具类
* @author ns
* @date 2021-10-18 14:25
* @Copyright 873103468@qq.com
*/
public class Ymodem {
/**
* byte[]文件转Ymodem协议文件
* @param file
* @return
*/
public static byte[][] BytesYmodem(byte[] file,double len,String name){
byte[] bytestr = name.getBytes();
int a=1;
int b=254;
int ys=(int)Math.ceil(len/128.00);
if(bytestr.length!=6){
return null;
}
byte[][] bytess=new byte[ys+1][133];
bytess[0]=ymodem_0(bytestr,len);
for (int bl=1;bl<ys;bl++){
bytess[bl]=ymodemTobyte(file,bl,a,b,(int)len);
a++;
b--;
}
return bytess;
}
/***
* 把文件地址转换成byte二维数组 二维数组例子
* (例子未转换实际已经转换)
* SOH 00 FF 00~00(共128个) CRCH CRCL
* SOH 01 FE 00~00(共128个) CRCH CRCL
* SOH 02 FD 00~00(共128个) CRCH CRCL
* SOH 03 FC 00~00(共128个) CRCH CRCL
* EOT
* EOT
* SOH 00 FF FUL[128] CRCH CRCL
* @param path 文件地址
* @return 最终数据
*/
public static byte[][] getStringByteYmodem(String path){
if (StringUtils.isNotEmpty(path)) {
File file = new File(path);
byte[] byte1 = fileToBinArray(file);
int len=byte1.length;
return BytesYmodem(byte1,len,getFileNameNoEx(file.getName()));
}else {
throw new RuntimeException("文件找不到地址");
}
}
/**
* 文件转为二进制数组
* @param file
* @return
*/
public static byte[] fileToBinArray(File file){
try {
InputStream fis = new FileInputStream(file);
byte[] bytes = FileCopyUtils.copyToByteArray(fis);
return bytes;
}catch (Exception ex){
throw new RuntimeException("文件转为二进制数组出错",ex);
}
}
/**
* 第一帧
* @param bytestr
* @param len
* @return
*/
public static byte[] ymodem_0(byte[] bytestr,double len){
//第一页
byte[] b0= new byte[3];
b0[0]=0x01;
b0[1]=(byte) 00;
b0[2]=(byte) 255;
byte[] b1= new byte[128];
ByteUtil.getByte2inByte1(b1,bytestr,0);
byte[] b3= new byte[4];
ByteUtil.putInt(b3,(int)len,0);
ByteUtil.getByte2inByte1(b1,b3,6);
ByteUtil.inMaxByte(b1,10);
byte[] b5=ByteUtil.getByte1andByte2(b1,CRC.getCRC5(b1));
return ByteUtil.getByte1andByte2(b0,b5);
}
/**
* 第n帧数据
* @param file 文件
* @param bl 第几行
* @param a 01
* @param b FF
* @param len 长度
* @return
*/
public static byte[] ymodemTobyte(byte[] file,int bl,int a,int b,int len){
byte[] b0= new byte[3];
b0[0]=0x01;
b0[1]=(byte) a;
b0[2]=(byte) b;
byte[] sj= new byte[128];
//判断剩余长度是否大于128时
int lening=(bl-1)*128;
int xhint =lening;
if(len-lening<128){
for (int f=0;f<len-lening;f++){
sj[f]=file[xhint];
xhint++;
}
ByteUtil.inMaxByte(sj,len-lening);
}else {
for (int f=0;f<128;f++){
sj[f]=file[xhint];
xhint++;
}
}
byte[] sj1=ByteUtil.getByte1andByte2(sj,CRC.getCRC5(sj));
return ByteUtil.getByte1andByte2(b0,sj1);
}
public static void main(String[] args) {
System.out.println(getFileNameNoEx("1.2.66.c"));
}
/**
* Java文件操作 获取文件扩展名
*/
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
/**
* Java文件操作 获取不带扩展名的文件名
*/
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
}
ByteUtil:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class ByteUtil {
/**
* 字节转十六进制
* @param b 需要进行转换的byte字节
* @return 转换后的Hex字符串
*/
public static String byteToHex(byte b){
String hex = Integer.toHexString(b & 0xFF);
if(hex.length() < 2){
hex = "0" + hex;
}
return hex;
}
/**
* 字节数组转16进制
* @param bytes 需要转换的byte数组
* @return 转换后的Hex字符串
*/
public static 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);
sb.append(" ");
}
return sb.toString();
}
/**
* Hex字符串转byte
* @param inHex 待转换的Hex字符串
* @return 转换后的byte
*/
public static byte hexToByte(String inHex){
return (byte)Integer.parseInt(inHex,16);
}
/**
* hex字符串转byte数组
* @param inHex 待转换的Hex字符串
* @return 转换后的byte数组结果
*/
public static 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;
}
/**
* 字节转十六进制
* @param
* @return 转换后的Hex字符串
*/
public static String listByteToHex(List<Byte> list){
if (list == null || list.size() < 0)
return null;
byte[] bytes = new byte[list.size()];
int i = 0;
Iterator<Byte> iterator = list.iterator();
while (iterator.hasNext()) {
bytes[i] = iterator.next();
i++;
}
String s = bytesToHex(bytes);
return s;
}
/**
* 16进制转2进制
* @param s
* @return
*/
public static String parseHexStr2Byte(String s) {
//字符串形式十进制--作为桥梁!
int sint=Integer.valueOf(s, 16);
//十进制在转换成二进制的字符串形式输出!
String bin=Integer.toBinaryString(sint);
if(bin.length()==3){
bin+="0"+bin;
}else if(bin.length()==2){
bin+="00"+bin;
}else if(bin.length()==1){
bin+="000"+bin;
}
return bin;
}
/**
* 转换short为byte
*
* @param b
* @param s
* 需要转换的short
* @param index
*/
public static void putShort(byte b[], short s, int index) {
b[index + 1] = (byte) (s >> 8);
b[index + 0] = (byte) (s >> 0);
}
/**
* 通过byte数组取到short
*
* @param b
* @param index
* 第几位开始取
* @return
*/
public static short getShort(byte[] b, int index) {
return (short) (((b[index + 1] << 8) | b[index + 0] & 0xff));
}
/**
* 转换int为byte数组
*
* @param bb
* @param x
* @param index
*/
public static void putInt(byte[] bb, int x, int index) {
bb[index + 3] = (byte) (x >> 24);
bb[index + 2] = (byte) (x >> 16);
bb[index + 1] = (byte) (x >> 8);
bb[index + 0] = (byte) (x >> 0);
}
/**
* 通过byte数组取到int
*
* @param bb
* @param index
* 第几位开始
* @return
*/
public static int getInt(byte[] bb, int index) {
return (int) ((((bb[index + 3] & 0xff) << 24)
| ((bb[index + 2] & 0xff) << 16)
| ((bb[index + 1] & 0xff) << 8) | ((bb[index + 0] & 0xff) << 0)));
}
/**
* 转换long型为byte数组
*
* @param bb
* @param x
* @param index
*/
public static void putLong(byte[] bb, long x, int index) {
bb[index + 7] = (byte) (x >> 56);
bb[index + 6] = (byte) (x >> 48);
bb[index + 5] = (byte) (x >> 40);
bb[index + 4] = (byte) (x >> 32);
bb[index + 3] = (byte) (x >> 24);
bb[index + 2] = (byte) (x >> 16);
bb[index + 1] = (byte) (x >> 8);
bb[index + 0] = (byte) (x >> 0);
}
/**
* 通过byte数组取到long
*
* @param bb
* @param index
* @return
*/
public static long getLong(byte[] bb, int index) {
return ((((long) bb[index + 7] & 0xff) << 56)
| (((long) bb[index + 6] & 0xff) << 48)
| (((long) bb[index + 5] & 0xff) << 40)
| (((long) bb[index + 4] & 0xff) << 32)
| (((long) bb[index + 3] & 0xff) << 24)
| (((long) bb[index + 2] & 0xff) << 16)
| (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index + 0] & 0xff) << 0));
}
/**
* 字符到字节转换
*
* @param ch
* @return
*/
public static void putChar(byte[] bb, char ch, int index) {
int temp = (int) ch;
// byte[] b = new byte[2];
for (int i = 0; i < 2; i ++ ) {
bb[index + i] = new Integer(temp & 0xff).byteValue(); // 将最高位保存在最低位
temp = temp >> 8; // 向右移8位
}
}
/**
* float转换byte
*
* @param bb
* @param x
* @param index
*/
public static void putFloat(byte[] bb, float x, int index) {
// byte[] b = new byte[4];
int l = Float.floatToIntBits(x);
for (int i = 0; i < 4; i++) {
bb[index + i] = new Integer(l).byteValue();
l = l >> 8;
}
}
/**
* 通过byte数组取得float
*
* @param bb
* @param index
* @return
*/
public static float getFloat(byte[] b, int index) {
int l;
l = b[index + 0];
l &= 0xff;
l |= ((long) b[index + 1] << 8);
l &= 0xffff;
l |= ((long) b[index + 2] << 16);
l &= 0xffffff;
l |= ((long) b[index + 3] << 24);
return Float.intBitsToFloat(l);
}
/**
* double转换byte
*
* @param bb
* @param x
* @param index
*/
public static void putDouble(byte[] bb, double x, int index) {
// byte[] b = new byte[8];
long l = Double.doubleToLongBits(x);
for (int i = 0; i < 4; i++) {
bb[index + i] = new Long(l).byteValue();
l = l >> 8;
}
}
/**
* @param: [content]
* @return: int
* @description: 十六进制转十进制
*/
public static int covert(String content){
int number=0;
String [] HighLetter = {"A","B","C","D","E","F"};
Map<String,Integer> map = new HashMap<>();
for(int i = 0;i <= 9;i++){
map.put(i+"",i);
}
for(int j= 10;j<HighLetter.length+10;j++){
map.put(HighLetter[j-10],j);
}
String[]str = new String[content.length()];
for(int i = 0; i < str.length; i++){
str[i] = content.substring(i,i+1);
}
for(int i = 0; i < str.length; i++){
number += map.get(str[i])*Math.pow(16,str.length-1-i);
}
return number;
}
public static void main(String[] args) {
byte[] ss=new byte[2];
ss[0]=00;
ss[1]=01;
System.out.println(covert("01"));
}
public static byte[] getFileToByte(File file) {
byte[] by = new byte[(int) file.length()];
try {
InputStream is = new FileInputStream(file);
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
byte[] bb = new byte[2048];
int ch;
ch = is.read(bb);
while (ch != -1) {
bytestream.write(bb, 0, ch);
ch = is.read(bb);
}
by = bytestream.toByteArray();
} catch (Exception ex) {
throw new RuntimeException("transform file into bin Array 出错",ex);
}
return by;
}
/**
* getBytesplit
* @param bytes 文件
* @param q 起始位
* @param len 长度
* @return byte[]
*/
public static byte[] getBytesplit(byte[] bytes,int q,int len) {
int length=bytes.length;
int cd=length-q;
int lengths=len;
byte[] ret_bytes;
if(cd>=len){
ret_bytes=new byte[1026];
lengths=len;
}else {
ret_bytes=new byte[cd+2];
lengths=cd;
}
int a=q;
for(int i=0;i<lengths;i++){
ret_bytes[i]=bytes[a];
a++;
}
return ret_bytes;
}
/**
* byte[]合并
* @param byte1
* @param byte2
* @return
*/
public static byte[] getByte1andByte2(byte[] byte1,byte[] byte2) {
byte[] ss=new byte[byte1.length+byte2.length];
int i=0;
for(int s1=0;s1<byte1.length;s1++){
ss[i]=byte1[s1];
i++;
}
for(int s2=0;s2<byte2.length;s2++){
ss[i]=byte2[s2];
i++;
}
return ss;
}
/**
* byte[]插入
* @param byte1
* @param byte2
* @param i
* @return
*/
public static void getByte2inByte1(byte[] byte1,byte[] byte2,int i) {
for(int s1=0;s1<byte2.length;s1++){
byte1[i]=byte2[s1];
i++;
}
}
/**
* byte[]空数据插入0xFF
* @param byte1
* @param i
* @return
*/
public static void inMaxByte(byte[] byte1,int i) {
for(int s1=i;s1<byte1.length;s1++){
byte1[s1]=(byte) 255;
}
}
/**
* ASCII转 String
* @param value
* @return
*/
public static String asciiToString(String value)
{
StringBuffer sbu = new StringBuffer();
String[] chars = value.split(",");
for (int i = 0; i < chars.length; i++) {
sbu.append((char) Integer.parseInt(chars[i]));
}
return sbu.toString();
}
// 从byte数组的index处的连续4个字节获得一个int
public static int getInts(byte[] arr, int index) {
return (0xff000000 & (arr[index+0] << 24)) |
(0x00ff0000 & (arr[index+1] << 16)) |
(0x0000ff00 & (arr[index+2] << 8)) |
(0x000000ff & arr[index+3]);
}
}
crc
/**
* 查表法计算CRC16校验
*
* @param data 需要计算的字节数组
*/
public static byte[] getCRC5(byte[] data) {
byte[] crc16_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) 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) 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) 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) 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) 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) 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) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40
};
byte[] crc16_l = {
(byte) 0x00, (byte) 0xC0, (byte) 0xC1, (byte) 0x01, (byte) 0xC3, (byte) 0x03, (byte) 0x02, (byte) 0xC2, (byte) 0xC6, (byte) 0x06, (byte) 0x07, (byte) 0xC7, (byte) 0x05, (byte) 0xC5, (byte) 0xC4, (byte) 0x04,
(byte) 0xCC, (byte) 0x0C, (byte) 0x0D, (byte) 0xCD, (byte) 0x0F, (byte) 0xCF, (byte) 0xCE, (byte) 0x0E, (byte) 0x0A, (byte) 0xCA, (byte) 0xCB, (byte) 0x0B, (byte) 0xC9, (byte) 0x09, (byte) 0x08, (byte) 0xC8,
(byte) 0xD8, (byte) 0x18, (byte) 0x19, (byte) 0xD9, (byte) 0x1B, (byte) 0xDB, (byte) 0xDA, (byte) 0x1A, (byte) 0x1E, (byte) 0xDE, (byte) 0xDF, (byte) 0x1F, (byte) 0xDD, (byte) 0x1D, (byte) 0x1C, (byte) 0xDC,
(byte) 0x14, (byte) 0xD4, (byte) 0xD5, (byte) 0x15, (byte) 0xD7, (byte) 0x17, (byte) 0x16, (byte) 0xD6, (byte) 0xD2, (byte) 0x12, (byte) 0x13, (byte) 0xD3, (byte) 0x11, (byte) 0xD1, (byte) 0xD0, (byte) 0x10,
(byte) 0xF0, (byte) 0x30, (byte) 0x31, (byte) 0xF1, (byte) 0x33, (byte) 0xF3, (byte) 0xF2, (byte) 0x32, (byte) 0x36, (byte) 0xF6, (byte) 0xF7, (byte) 0x37, (byte) 0xF5, (byte) 0x35, (byte) 0x34, (byte) 0xF4,
(byte) 0x3C, (byte) 0xFC, (byte) 0xFD, (byte) 0x3D, (byte) 0xFF, (byte) 0x3F, (byte) 0x3E, (byte) 0xFE, (byte) 0xFA, (byte) 0x3A, (byte) 0x3B, (byte) 0xFB, (byte) 0x39, (byte) 0xF9, (byte) 0xF8, (byte) 0x38,
(byte) 0x28, (byte) 0xE8, (byte) 0xE9, (byte) 0x29, (byte) 0xEB, (byte) 0x2B, (byte) 0x2A, (byte) 0xEA, (byte) 0xEE, (byte) 0x2E, (byte) 0x2F, (byte) 0xEF, (byte) 0x2D, (byte) 0xED, (byte) 0xEC, (byte) 0x2C,
(byte) 0xE4, (byte) 0x24, (byte) 0x25, (byte) 0xE5, (byte) 0x27, (byte) 0xE7, (byte) 0xE6, (byte) 0x26, (byte) 0x22, (byte) 0xE2, (byte) 0xE3, (byte) 0x23, (byte) 0xE1, (byte) 0x21, (byte) 0x20, (byte) 0xE0,
(byte) 0xA0, (byte) 0x60, (byte) 0x61, (byte) 0xA1, (byte) 0x63, (byte) 0xA3, (byte) 0xA2, (byte) 0x62, (byte) 0x66, (byte) 0xA6, (byte) 0xA7, (byte) 0x67, (byte) 0xA5, (byte) 0x65, (byte) 0x64, (byte) 0xA4,
(byte) 0x6C, (byte) 0xAC, (byte) 0xAD, (byte) 0x6D, (byte) 0xAF, (byte) 0x6F, (byte) 0x6E, (byte) 0xAE, (byte) 0xAA, (byte) 0x6A, (byte) 0x6B, (byte) 0xAB, (byte) 0x69, (byte) 0xA9, (byte) 0xA8, (byte) 0x68,
(byte) 0x78, (byte) 0xB8, (byte) 0xB9, (byte) 0x79, (byte) 0xBB, (byte) 0x7B, (byte) 0x7A, (byte) 0xBA, (byte) 0xBE, (byte) 0x7E, (byte) 0x7F, (byte) 0xBF, (byte) 0x7D, (byte) 0xBD, (byte) 0xBC, (byte) 0x7C,
(byte) 0xB4, (byte) 0x74, (byte) 0x75, (byte) 0xB5, (byte) 0x77, (byte) 0xB7, (byte) 0xB6, (byte) 0x76, (byte) 0x72, (byte) 0xB2, (byte) 0xB3, (byte) 0x73, (byte) 0xB1, (byte) 0x71, (byte) 0x70, (byte) 0xB0,
(byte) 0x50, (byte) 0x90, (byte) 0x91, (byte) 0x51, (byte) 0x93, (byte) 0x53, (byte) 0x52, (byte) 0x92, (byte) 0x96, (byte) 0x56, (byte) 0x57, (byte) 0x97, (byte) 0x55, (byte) 0x95, (byte) 0x94, (byte) 0x54,
(byte) 0x9C, (byte) 0x5C, (byte) 0x5D, (byte) 0x9D, (byte) 0x5F, (byte) 0x9F, (byte) 0x9E, (byte) 0x5E, (byte) 0x5A, (byte) 0x9A, (byte) 0x9B, (byte) 0x5B, (byte) 0x99, (byte) 0x59, (byte) 0x58, (byte) 0x98,
(byte) 0x88, (byte) 0x48, (byte) 0x49, (byte) 0x89, (byte) 0x4B, (byte) 0x8B, (byte) 0x8A, (byte) 0x4A, (byte) 0x4E, (byte) 0x8E, (byte) 0x8F, (byte) 0x4F, (byte) 0x8D, (byte) 0x4D, (byte) 0x4C, (byte) 0x8C,
(byte) 0x44, (byte) 0x84, (byte) 0x85, (byte) 0x45, (byte) 0x87, (byte) 0x47, (byte) 0x46, (byte) 0x86, (byte) 0x82, (byte) 0x42, (byte) 0x43, (byte) 0x83, (byte) 0x41, (byte) 0x81, (byte) 0x80, (byte) 0x40
};
int crc = 0x0000ffff;
int ucCRCHi = 0x00ff;
int ucCRCLo = 0x00ff;
int iIndex;
for (int i = 0; i < data.length; ++i) {
iIndex = (ucCRCLo ^ data[i]) & 0x00ff;
ucCRCLo = ucCRCHi ^ crc16_h[iIndex];
ucCRCHi = crc16_l[iIndex];
}
crc = ((ucCRCHi & 0x00ff) << 8) | (ucCRCLo & 0x00ff) & 0xffff;
//高低位互换,输出符合相关工具对Modbus CRC16的运算
crc = ( (crc & 0xFF00) >> 8) | ( (crc & 0x00FF ) << 8);
String ss=String.format("%04X", crc);
byte[] sj=new byte[2];
sj[1]=(byte)covert(ss.substring(0, 2));
sj[0]=(byte)covert(ss.substring(2, 4));
return sj;
}
原创不易,觉得有用点赞打赏一下吧
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器