第一次對接這玩意,也是RLG!!! 對接硬件不都用C來搞嗎?沒法奈何我只會java
不廢話,先直接上部分對接需求文檔>>>

先解讀一下以上的東西,看備注可知,以0x 開頭,基本確定是全是16進制的東西
先理一下,
類型為Byte, 一個字節等於2位16進制數
類型為Uint ...反正跟java中的"int"八九不離十, int占4個字節等於8位16進制數
類型為Ushort ...反正跟java中的"short八九不離十, short占2個字節等於4位16進制數
還有個要注意的點,數字類型高低位排序。低位在“前”按照甲方爸爸給的提示,這個“前”是從右往左排 ,意思是越小的,越排在后面
以長度lenth舉個栗子吧:
//當長度 lenth=18時 int lenth=18; //轉16進制 String lenthHex = Integer.toHexString(lenth); //這時的lenthHex應該等於12,但是文檔要求長度為4x2位16進制,於是 //往前補0,這是的lenthHex等於00000012 //再低位在前排序,注意,此處全部都是這對於字節,於是2位一體倒序 //輸出lenthHex=12000000
下面給出 設備登錄的需求文檔來做一次實際操作

//設備登陸測試 final static String info = "01" +//開始標記 "41000000" +//長度 LEN 原值為65 (低位排序) "00000000" +// "00000000" +// "01" +//版本 "4B03" +//命令 原值為843 (低位排序) "1B00ED8B91D23143AFE845EAA4F7EFCF" +//唯一標識 (隨便填的,文檔只要求保證唯一性) //content 原值為 廠家識別碼:15EFD2B2FD6B476697B729F33D08630A+設備的唯一標識碼:5078c650bac04a0fa190bb7480573f26 +xor運算可參考 http://www.ip33.com/bcc.html "31354546443242324644364234373636393742373239463333443038363330413530373863363530626163303461306661313930626237343830353733663236" +"05" +//xor運算 "00" +//狀態 "01";//結束標記
來看一下測試工具的返回結果

返回Flag區域=00表示處理成功
下面給出java代碼中如何實現發送和接收數據代碼,基於springboot,為了性能全局只創建一次連接
數據發送者
/**
* 在項目啟動完成后啟動 優先級1,發送數據直接調用sendMsg方法即可
*/
@Component
@Order(value = 1)
public class BaseServer implements ApplicationRunner {
private final static Logger logger = LoggerFactory.getLogger(AttendanceServer.class); private static final ThreadLocal<Socket> threadConnect = new ThreadLocal<Socket>(); //對方域名 private static final String HOST = ""; //對方監聽端口 private final static int PORT = 8080; //服務輸出流 public static OutputStream outStr = null; //服務輸入流 public static InputStream inStr = null; //Socket連接 public static Socket client;
public void run(ApplicationArguments args) throws Exception { try { //初始化連接 this.connect(); }catch (Exception e){ logger.error("初始化連接服務出現異常!"+e.getMessage()); e.printStackTrace(); } } /** * 連接初始化方法 * @throws Exception */ public static void connect() throws Exception { client = threadConnect.get(); //如果client為空,則建立一次新的連接 if (client == null) { client = new Socket(HOST, PORT); threadConnect.set(client); logger.info("服務初始化成功!"); } outStr = client.getOutputStream(); inStr = client.getInputStream(); } /** * 關閉連接方法 * @throws Exception */ public static void disconnect() { try { outStr.close(); inStr.close(); client.close(); outStr=null; inStr=null; client=null; } catch (IOException e) { e.printStackTrace(); } } /** * 發送數據方法(添加同步鎖,防止輸出流被寫入垃圾數據) * @param info */ public synchronized static void sendMsg(String info) { byte[] m = HexUtil.hexStringToBytes(info); try { logger.info("准備發送報文:"+info); Thread.sleep(1500); outStr.write(m); outStr.flush(); } catch (Exception e) { e.printStackTrace(); //如果此處捕獲到異常,直接關閉當前連接,然后重新初始化 logger.info("發送報文出現異常,即將重新建立連接!"); disconnect(); try { //初始化連接 connect(); }catch (Exception ex){ logger.error("初始化連接服務出現異常!"+ex.getMessage()); ex.printStackTrace(); } } } }
接收者服務
/** * 數據接收服務 ,繼承自連接服務 * 在項目啟動完成后啟動 優先級2 */ @Component @Order(value = 2) public class Receiver extends BaseServer implements ApplicationRunner { private final Logger logger = LoggerFactory.getLogger(Receiver.class); @Override public void run(ApplicationArguments args) throws Exception { logger.info("數據接收服務初始化成功!"); try { while (true) { byte[] b = new byte[1024]; if (inStr == null) { //可能是連接還在初始化中,需要等待 continue; } Thread.sleep(500); if (inStr.available() > 0) { int r = inStr.read(b); if (r > -1) { String result = HexUtil.BinaryToHexString(b); } } } } catch (IOException e) { logger.error("數據接收服務出現異常" + e.getMessage() + "原因是:"); e.printStackTrace(); } } }
進制轉換工具類
public class HexUtil { public static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } // toUpperCase將字符串中的所有字符轉換為大寫 hexString = hexString.toUpperCase(); int length = hexString.length() / 2; // toCharArray將此字符串轉換為一個新的字符數組。 char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } return d; } //charToByte返回在指定字符的第一個發生的字符串中的索引,即返回匹配字符 private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } public static String BinaryToHexString(byte[] bytes) { String hexStr = "0123456789ABCDEF"; String result = ""; String hex = ""; for (byte b : bytes) { hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4)); hex += String.valueOf(hexStr.charAt(b & 0x0F)); result += hex ; } return result; } //兩位一字符,倒序排序 public static String reverseString(String str) { List<String> strlist=new ArrayList(); char[] chr = str.toCharArray(); for (int i = 0 ; i < chr.length; i=i+2) { String s=chr[i]+""+chr[i+1]; strlist.add(s); } Collections.reverse(strlist); String result=""; for(String v:strlist){ result+=v; } return result; } /** * 16進制轉換成為string類型字符串 * @param s * @return */ public static String hexStringToString(String s) { if (s == null || s.equals("")) { return null; } s = s.replace(" ", ""); byte[] baKeyword = new byte[s.length() / 2]; for (int i = 0; i < baKeyword.length; i++) { try { baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16)); } catch (Exception e) { e.printStackTrace(); } } try { s = new String(baKeyword, "UTF-8"); new String(); } catch (Exception e1) { e1.printStackTrace(); } return s; } /** * 字符串轉化成為16進制字符串 * @param s * @return */ public static String strTo16(String s) { String str = ""; for (int i = 0; i < s.length(); i++) { int ch = (int) s.charAt(i); String s4 = Integer.toHexString(ch); str = str + s4; } return str; } //將16進制字符串自動補全到8位 並且倒序排序 public static String full8(String lenth) { int a = lenth.getBytes().length; int b = 8 - a; for (int i = 0; i < b; i++) { lenth = "0" + lenth; } return reverseString(lenth); } /** * xor運算 * * @param data * @return */ public static String getBCC(byte[] data) { String ret = ""; byte BCC[] = new byte[1]; for (int i = 0; i < data.length; i++) { BCC[0] = (byte) (BCC[0] ^ data[i]); } String hex = Integer.toHexString(BCC[0] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } ret += hex.toUpperCase(); return ret; } }
稍微修改一下就能用了
