| | |
| | | # Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties` |
| | | # should NOT be excluded as they contain compiler settings and other important |
| | | # information for Eclipse / Flash Builder. |
| | | /.idea |
| | | /out |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package Anchor; |
| | | |
| | | public class Anchor { |
| | | String anchorId;//ä¿¡æ ID |
| | | String distance;//æµè·è·ç¦» |
| | | |
| | | public String getAnchorId() { |
| | | return anchorId; |
| | | } |
| | | |
| | | public String getDistance() { |
| | | return distance; |
| | | } |
| | | |
| | | public void setAnchorId(String anchorId) { |
| | | this.anchorId = anchorId; |
| | | } |
| | | |
| | | public void setDistance(String distance) { |
| | | this.distance = distance; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package Anchor; |
| | | |
| | | import java.util.Vector; |
| | | |
| | | public class anchor_Dell { |
| | | static Vector<Anchor> anchors=new Vector<>(); |
| | | static int id=1001; |
| | | static int dis=1001; |
| | | |
| | | public anchor_Dell(){ |
| | | for (int i = 0; i < 50; i++) { |
| | | id++; |
| | | dis++; |
| | | addAnchor(id+"",dis+""); |
| | | } |
| | | } |
| | | |
| | | |
| | | public static Anchor findAnchor(String anchorId){ |
| | | for (Anchor anchor : anchors) { |
| | | String anchorId1 = anchor.getAnchorId(); |
| | | if (anchorId1.equals(anchorId)){ |
| | | return anchor; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static void addAnchor(String anchorId,String Distance){ |
| | | Anchor anchor = findAnchor(anchorId); |
| | | if (anchor==null){ |
| | | Anchor anchor1=new Anchor(); |
| | | anchor1.setAnchorId(anchorId); |
| | | anchor1.setDistance(Distance); |
| | | anchors.add(anchor1); |
| | | }else { |
| | | anchor.setDistance(Distance); |
| | | } |
| | | } |
| | | |
| | | public static Vector<Anchor> getAnchors() { |
| | | return anchors; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | Manifest-Version: 1.0 |
| | | Main-Class: index.homeMain |
| | | |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package baowen; |
| | | |
| | | public class Baowen { |
| | | String hex; |
| | | String ip; |
| | | String addtime; |
| | | int port; |
| | | String dataFrom;//æ°æ®æ¥æº 0:åå§æ°æ®ï¼1ï¼æå
æ°æ®ï¼-1ï¼æªç¥ |
| | | |
| | | public String getDataFrom() { |
| | | return dataFrom; |
| | | } |
| | | |
| | | public void setDataFrom(String dataFrom) { |
| | | this.dataFrom = dataFrom; |
| | | } |
| | | |
| | | public int getPort() { |
| | | return port; |
| | | } |
| | | public void setPort(int port) { |
| | | this.port = port; |
| | | } |
| | | int timestamp; |
| | | public String getHex() { |
| | | return hex; |
| | | } |
| | | public String getIp() { |
| | | return ip; |
| | | } |
| | | public String getAddtime() { |
| | | return addtime; |
| | | } |
| | | public int getTimestamp() { |
| | | return timestamp; |
| | | } |
| | | public void setHex(String hex) { |
| | | this.hex = hex; |
| | | } |
| | | public void setIp(String ip) { |
| | | this.ip = ip; |
| | | } |
| | | public void setAddtime(String addtime) { |
| | | this.addtime = addtime; |
| | | } |
| | | public void setTimestamp(int timestamp) { |
| | | this.timestamp = timestamp; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package baowen; |
| | | |
| | | |
| | | import index.JPanelMoudle.versionUpgradeComp; |
| | | |
| | | import tools.Tools; |
| | | |
| | | |
| | | |
| | | public class Dell_55AA03 { |
| | | private static String[] hex = null; |
| | | |
| | | public static void dell(String ip, String message, int port) { |
| | | String infom[] = message.split("55AA"); |
| | | int size = infom.length; |
| | | for (int i = 0; i < size; i++) { |
| | | String mes = "55AA" + infom[i]; |
| | | String[] hex = Tools.hex(message); |
| | | if (hex.length < 30) { |
| | | return; |
| | | } |
| | | String type = Tools.decodeHEX(hex[20] + hex[19]) + ""; |
| | | if (type.equals("3")) { |
| | | dell05(ip, message, port); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static void dell05(String ip, String message, int port) { |
| | | String[] infoArray = message.split("55AA"); |
| | | int size = infoArray.length; |
| | | |
| | | for (int i = 0; i < size; i++) { |
| | | String hexMessage = "55AA" + infoArray[i]; |
| | | if (isValidMessage(hexMessage)) { |
| | | ok_55AA05(ip, hexMessage, port); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private static boolean isValidMessage(String hexMessage) { |
| | | return hexMessage.startsWith("55AA0355") && hexMessage.length() == 178; |
| | | } |
| | | |
| | | public static void ok_55AA05(String ip, String hexMessage, int port) { |
| | | hexMessage = hexMessage.substring(14, hexMessage.length()); |
| | | hex = Tools.hex(hexMessage); |
| | | String firmwareVersion = "V" + Integer.parseInt(hex[1], 16) + "." + Integer.parseInt(hex[0], 16); |
| | | versionUpgradeComp.getReadVersion_jf().setText(firmwareVersion); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package baowen; |
| | | |
| | | |
| | | import index.JPanelMoudle.readParamsComp; |
| | | import index.JPanelMoudle.versionUpgradeComp; |
| | | |
| | | import tools.ShowMessage; |
| | | import tools.Tools; |
| | | |
| | | import java.math.BigInteger; |
| | | |
| | | |
| | | //æ ç¾å¿è·³å
|
| | | public class Dell_55AA34 { |
| | | static String[] hex;//åèæ°ç» |
| | | static String ip; |
| | | static int port; |
| | | |
| | | //55AA3403240000 |
| | | public static void dellrgs( String ip1,String infom, int port1) { |
| | | String jinDu="";//å级è¿åº¦ |
| | | ip = ip1; |
| | | port = port1; |
| | | hex = Tools.hex(infom); |
| | | if (hex.length>3) { |
| | | int datalenth1 = Tools.decodeHEX(hex[3]);//æ°æ®é¿åº¦ |
| | | datalenth1 = datalenth1 * 2 + 8; |
| | | int lenth = infom.length(); |
| | | if (lenth == datalenth1) { |
| | | int i = decodeHEX(hex[4]); |
| | | String tagid="-----"; |
| | | if (hex[3].equals("05")){ |
| | | tagid = hex[6] + hex[5]; |
| | | } |
| | | String anchorID = readParamsComp.getAnchorId_jf().getText(); |
| | | if (tagid.equals(anchorID)||tagid.equals("-----")){ |
| | | versionUpgradeComp.getUpdateAnchor_jf().setValue(i); |
| | | } |
| | | if (i==100||i==99){//设å¤å级è¿åº¦è¾¾å°100æè
99æ¶ |
| | | ShowMessage.zidingyi("ä¿¡æ åçº§å®æ"); |
| | | versionUpgradeComp.exitUpgradeXinBiao(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /**16è¿å¶è½¬ä¸º10è¿å¶*/ |
| | | public static int decodeHEX(String hexs){ |
| | | BigInteger bigint=new BigInteger(hexs, 16); |
| | | int numb=bigint.intValue(); |
| | | return numb; |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package baowen; |
| | | |
| | | |
| | | import index.JPanelMoudle.readParamsComp; |
| | | import tools.GetNowTime; |
| | | import tools.ShowMessage; |
| | | import tools.Tools; |
| | | |
| | | |
| | | /** |
| | | * åºç«çé
ç½®ä¿¡æ¯ |
| | | */ |
| | | public class Dell_55AA39 { |
| | | public static boolean readpeizhi_succ = false; |
| | | static String[] hex = null; |
| | | static String[] hex1 = null; |
| | | |
| | | |
| | | /** |
| | | * 55 AA 39 55 03 01 50 00 01 01 90 E8 03 0A 00 05 00 E1 FF 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0000 00 71 04 01 00 01 00 01 00 01 00 01 00 01 00 01 00 01 00 01 00 43 00 02 00 78 00 01 00 00 00 01 00 00 00 0000 00 00 00 00 00 00 00 00 00 00 00 00 A9 FA |
| | | */ |
| | | public static void dell(String ip, String message, int port) { |
| | | hex = Tools.hex(message);//åå§æ°æ® |
| | | int length = message.length(); |
| | | int size = Tools.decodeHEX(hex[3]); |
| | | int length2 = (size * 2) + 8; |
| | | if (length2 == length) { |
| | | ok_55AA39(ip, message, port, hex); |
| | | } else { |
| | | Dell_uanshi_byt.intsert(ip, message, GetNowTime.timestamp2(), GetNowTime.timestamp(), port); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | @SuppressWarnings("unused") |
| | | public static void ok_55AA39(String ip, String message, int port, String[] hex) { |
| | | String hex1 = hex[8]; |
| | | //è¯»åæ ç¾ |
| | | if (hex1.equals("02")) { |
| | | rwTag(ip, message, port,hex); |
| | | } else if (hex1.equals("12") || hex1.equals("11")) { |
| | | rwAnc(ip, message, port, hex1); |
| | | } |
| | | } |
| | | |
| | | //读ååºç«é
ç½® |
| | | private static void rwAnc(String ip, String message, int port, String hexr0w) { |
| | | String message1 = message.substring(22); |
| | | hex1 = Tools.hex(message1);//åå§æ°æ® |
| | | String ancId = null; //åºç«ID |
| | | String version = null; //åºç«çæ¬ |
| | | String cejuNum = null; //æµè·åºç«æ°é |
| | | String sendTime = null; //uwbä¿¡å·åéæ¶é´ |
| | | String loraGongLv = null; //loraåå°åç |
| | | String uwbGongLv = null; //uwbåç |
| | | String gatewayId = null; //uwbåç |
| | | if (hexr0w.equals("11")){ |
| | | System.out.println("ä¿®æ¹åºç«æå"); |
| | | }else { |
| | | try { |
| | | gatewayId = hex[7] + hex[6];//ç½å
³ID |
| | | version = "V" + Integer.parseInt(hex1[1], 16) + "." + Integer.parseInt(hex1[0], 16);//çæ¬å· |
| | | ancId = diZhi(2);//åºç«ID |
| | | cejuNum = Tools.decodeHEX(diZhi(5)) + "";//ç»ID |
| | | loraGongLv = getLoraGonglV(6);//loraåç |
| | | sendTime = Tools.decodeHEX(diZhi(10)) + "";//åéæ¶é´ |
| | | uwbGongLv = Tools.decodeHEX(diZhi(27)) + "";//uwbåç |
| | | |
| | | } catch (NumberFormatException e) { |
| | | ShowMessage.zidingyi_24("ä¿¡æ è¿ååæ°é误"); |
| | | return; |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | //è¯»åæ ç¾åå¤ |
| | | private static void rwTag(String ip, String message, int port,String[] hex) { |
| | | |
| | | String message1 = message.substring(22); |
| | | hex1 = Tools.hex(message1);//åå§æ°æ® |
| | | |
| | | String tagid = null;//模åID |
| | | String version = null;//çæ¬å· |
| | | String groupId = null;//å°ç»ID |
| | | String distance = null;//loraåç |
| | | String uwbGongLv = null;//uwåç |
| | | try { |
| | | version = "V" + Integer.parseInt(hex1[1], 16) + "." + Integer.parseInt(hex1[0], 16);//çæ¬å· |
| | | tagid = diZhi(2);//æ ç¾ID |
| | | groupId = Tools.decodeHEX(diZhi(5)) + "";//å°ç»ID |
| | | distance = Tools.decodeHEX(diZhi(6))+"";//æ ¡åè·ç¦» |
| | | uwbGongLv = Tools.decodeHEX(diZhi(27)) + "";//UWBåç |
| | | readParamsComp.getAnchorId_jf().setText(tagid); |
| | | readParamsComp.getAnchorVersion_jf().setText(version); |
| | | readParamsComp.getGroupId_jf().setText(groupId); |
| | | readParamsComp.getAnchorPower_jf().setText(uwbGongLv); |
| | | readParamsComp.getJiaoZhunDis_jf().setText(distance); |
| | | } catch (NumberFormatException e) { |
| | | ShowMessage.zidingyi_24("ä¿¡æ åæ°é误..."); |
| | | return; |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | private static String getLoraGonglV(int addr) { |
| | | String loraGongl="-1"; |
| | | int loraPower = Tools.decodeHEX(diZhi(addr)); |
| | | byte loraPower1 = (byte) loraPower; |
| | | loraGongl = loraPower1 + ""; |
| | | return loraGongl; |
| | | } |
| | | |
| | | |
| | | public static String diZhi(int addr) { |
| | | String s = hex1[(addr * 2) - 1] + hex1[(addr * 2) - 2]; |
| | | return s; |
| | | } |
| | | |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package baowen; |
| | | |
| | | import Anchor.anchor_Dell; |
| | | import tools.GetNowTime; |
| | | import tools.Tools; |
| | | |
| | | public class Dell_55AA40 { |
| | | |
| | | static String[] hex;//åèæ°ç» |
| | | |
| | | static int[] distance;//è·åè·ç¦»çéå |
| | | static String[] anchorid;//è·ååºç«çéå |
| | | |
| | | public static void dellrgs(String ip,String message, int port) { |
| | | message = message.replaceAll("\r\n|\r|\n", "");//ååºæææè¡åå车 |
| | | int lenth = message.length(); |
| | | hex = Tools.hex(message);//åå§æ°æ® |
| | | //int datalenth1 = Tools.decodeHEX(hex[3]);//æ°æ®é¿åº¦ |
| | | /* datalenth1 = datalenth1 * 2 + 8; |
| | | if (lenth == datalenth1) {*/ |
| | | String baoTou = diZhi(2);//å
头 |
| | | int anchorNum = Tools.decodeHEXToInt8(hex[3]); |
| | | get_anchorids(anchorNum); |
| | | get_distance_int(anchorNum); |
| | | addAnchor(anchorNum); |
| | | /* }else { |
| | | Dell_uanshi_byt.intsert(ip, message, GetNowTime.timestamp2(), GetNowTime.timestamp(), port); |
| | | }*/ |
| | | } |
| | | |
| | | |
| | | //æ·»å åºç«å¯¹è±¡ |
| | | private static void addAnchor(int anchorNum) { |
| | | for (int i = 0; i < anchorNum; i++) { |
| | | String anchorID = anchorid[i]; |
| | | int dis = distance[i]; |
| | | anchor_Dell.addAnchor(anchorID,dis+""); |
| | | } |
| | | } |
| | | |
| | | |
| | | //è·ååºç«çIDéå |
| | | public static void get_anchorids(int anchornum) { |
| | | anchorid = new String[anchornum]; |
| | | for (int i = 0; i < anchornum; i++) { |
| | | anchorid[i] = hex[5 + i * 2] + hex[4 + i * 2]; |
| | | } |
| | | } |
| | | |
| | | |
| | | //è·åæµè·è·ç¦» |
| | | public static void get_distance_int(int anchornum) { |
| | | int dis1 = anchornum * 2 +4 ; |
| | | int dis2 = dis1 + 1; |
| | | distance = new int[anchornum]; |
| | | for (int j = 0; j < anchornum; j++) { |
| | | distance[j] = Tools.decodeHEXToInt8( hex[dis2 + j * 2] + hex[dis1 + j * 2]); |
| | | } |
| | | } |
| | | |
| | | public static String diZhi(int addr) { |
| | | String s = hex[(addr * 2) - 1] + hex[(addr * 2) - 2]; |
| | | return s; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package baowen; |
| | | |
| | | import tools.GetNowTime; |
| | | |
| | | import java.util.Vector; |
| | | |
| | | public class Dell_Baowen implements Runnable { |
| | | |
| | | static Thread t; |
| | | |
| | | static Vector<Baowen> baowenvec = new Vector<Baowen>(); |
| | | static int sleeptime = 1; |
| | | |
| | | public void startThread() { |
| | | t = new Thread(this); |
| | | t.start(); |
| | | } |
| | | |
| | | public static void intsert(String ip, String hex, String time, int port) { |
| | | Baowen baowen = new Baowen(); |
| | | baowen.setHex(hex); |
| | | baowen.setIp(ip); |
| | | baowen.setAddtime(time); |
| | | baowen.setTimestamp(GetNowTime.timestamp()); |
| | | baowen.setPort(port); |
| | | baowenvec.add(baowen); |
| | | } |
| | | |
| | | |
| | | public void run() { |
| | | while (true) { |
| | | try { |
| | | dellhex(); |
| | | Thread.sleep(sleeptime);//ä¼ç æ¶é´ |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public void dellhex() { |
| | | int size = baowenvec.size(); |
| | | if (size == 0) { |
| | | sleeptime = 10; |
| | | return; |
| | | } |
| | | if (size > 100000) {//å¦ææ°æ®å¤§äº10䏿¡æ²¡æå¤ç忏
空 |
| | | baowenvec.removeAllElements(); |
| | | } else { |
| | | sleeptime = 0; |
| | | Baowen baowen0 = baowenvec.get(0); |
| | | String ip = baowen0.getIp(); |
| | | int port = baowen0.getPort(); |
| | | String hex = baowen0.getHex(); |
| | | String time = baowen0.getAddtime(); |
| | | int timestamp = baowen0.getTimestamp(); |
| | | int lenth = hex.length(); |
| | | try { |
| | | dellhex2(ip, port, hex, time, timestamp, lenth);//å¤çåå§æ°æ® |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | baowenvec.remove(baowen0);//å é¤å¤çè¿çæ¥æ |
| | | } |
| | | } |
| | | public static void dellhex2(String ip, int port, String hex, String time, int timestamp, int lenth) { |
| | | if (hex.startsWith("55AA39")) { |
| | | Dell_55AA39.dell(ip, hex,port); |
| | | }else if (hex.startsWith("55AA40")){ |
| | | Dell_55AA40.dellrgs(ip, hex,port); |
| | | }else if (hex.startsWith("55AA03")){ |
| | | Dell_55AA03.dell(ip, hex,port); |
| | | }else if (hex.startsWith("55AA34")){ |
| | | Dell_55AA34.dellrgs(ip, hex,port); |
| | | }else { |
| | | Dell_uanshi_byt.intsert(ip, hex, time, timestamp, port); |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package baowen; |
| | | |
| | | |
| | | import tools.Jiaoyan; |
| | | import tools.Judge.JugeNumber; |
| | | import tools.ShowMessage; |
| | | |
| | | import java.math.BigInteger; |
| | | import java.util.Vector; |
| | | |
| | | |
| | | /** |
| | | * å¤çåå§æ°æ® |
| | | */ |
| | | public class Dell_uanshi_byt { |
| | | |
| | | static Vector<YuanshiByt> yuanshivec = new Vector<>(); |
| | | |
| | | |
| | | /** |
| | | * æå
¥åå§æµè·æ°æ®ï¼æ¯ä¸ªipä¸ä¸ªéåæ¯ä¸ªIP䏿¥çæ°æ®è£
å
¥åèªIPä¹ä¸ |
| | | */ |
| | | public static void intsert(String ip, String hex, String time, int timestamp, int port) { |
| | | if (get_ys(ip) != null) { |
| | | int lenth = hex.length() / 2; |
| | | for (int i = 0; i < lenth; i++) { |
| | | String hex1 = hex.substring(2 * i, (i + 1) * 2); |
| | | get_ys(ip).getData().add(hex1); |
| | | hex1 = null; |
| | | } |
| | | get_ys(ip).setTime(time); |
| | | get_ys(ip).setTimestamp(timestamp); |
| | | dell_ysbyt(get_ys(ip), ip, port); |
| | | } else { |
| | | YuanshiByt ysbyt = new YuanshiByt(); |
| | | Vector<String> data1 = new Vector<>(); |
| | | StringBuffer okhex = new StringBuffer(); |
| | | ysbyt.setIp(ip); |
| | | ysbyt.setPort(port); |
| | | ysbyt.setData(data1); |
| | | ysbyt.setTimestamp(timestamp); |
| | | ysbyt.setTime(time); |
| | | ysbyt.setUsart_state(0); |
| | | ysbyt.setPack(okhex); |
| | | ysbyt.setDatalen(0); |
| | | ysbyt.setType(""); |
| | | int lenth = hex.length() / 2; |
| | | for (int i = 0; i < lenth; i++) { |
| | | String hex1 = hex.substring(2 * i, (i + 1) * 2); |
| | | data1.add(hex1); |
| | | hex1 = null; |
| | | } |
| | | yuanshivec.add(ysbyt); |
| | | dell_ysbyt(ysbyt, ip, port); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * éè¿IPæ¾å°è¯¥IPçæ°æ®éå |
| | | */ |
| | | public static YuanshiByt get_ys(String ip) { |
| | | YuanshiByt ysbyt = null; |
| | | int size = yuanshivec.size(); |
| | | for (int i = 0; i < size; i++) { |
| | | YuanshiByt ysbyt1 = yuanshivec.get(i); |
| | | String ip1 = ysbyt1.getIp(); |
| | | if (ip1.equals(ip)) { |
| | | ysbyt = ysbyt1; |
| | | break; |
| | | } |
| | | } |
| | | return ysbyt; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * å¤çåå§éåä¸çæ°æ® |
| | | */ |
| | | public static void dell_ysbyt(YuanshiByt ysbyt, String ip, int port) { |
| | | String time = ysbyt.getTime(); |
| | | int timestamp = ysbyt.getTimestamp(); |
| | | while (ysbyt.getData().size() > 0) { |
| | | String hex = null; |
| | | try { |
| | | hex = ysbyt.getData().get(0); |
| | | } catch (ArrayIndexOutOfBoundsException e) { |
| | | return; |
| | | } |
| | | int usart_state = ysbyt.getUsart_state(); |
| | | //å°åå§æ°æ®éåä¸çç¬¬ä¸æ¡æ°æ®ååºæ¾å
¥swichå¾ªç¯ |
| | | switch (usart_state) { |
| | | |
| | | case 0: |
| | | //å¦ææ°æ®æ¯55åæ§è¡ |
| | | if (hex.equals("55")) { |
| | | //å°æ°æ®å¤ççç¶æä¿®æ¹ä¸º1 |
| | | ysbyt.setUsart_state(1); |
| | | //åæ¶æå
¥æ°æ®å¨åç¬¦ä¸²ä¸ |
| | | ysbyt.getPack().append(hex); |
| | | } |
| | | break; |
| | | |
| | | case 1: |
| | | //å¦ææ°æ®æ¯AAåæ§è¡ |
| | | if (hex.equals("AA")) {//ç¶æ1æ
åµä¸ï¼çå¾
æ¥æ¶å°AAå
头2ï¼ç¶ååæç¶æ2 |
| | | ysbyt.setUsart_state(2); |
| | | ysbyt.getPack().append(hex); |
| | | } |
| | | break; |
| | | |
| | | case 2: |
| | | //æ°æ®ç±»åç»TYPE |
| | | ysbyt.setType(hex); |
| | | ysbyt.setUsart_state(3); |
| | | ysbyt.getPack().append(hex); |
| | | break; |
| | | |
| | | case 3: |
| | | //æ¥ææ°æ®æ®µæ°æ®é¿åº¦ |
| | | boolean a1 = JugeNumber.isLetterDigit(hex); |
| | | int d = 0; |
| | | if (a1) { |
| | | BigInteger bigint = new BigInteger(hex, 16); |
| | | d = bigint.intValue(); |
| | | } else { |
| | | ShowMessage.zidingyi("HEX to DEC errorï¼" + hex); |
| | | ysbyt.getPack().setLength(0); |
| | | ysbyt.setType(""); |
| | | ysbyt.setUsart_state(0); |
| | | break; |
| | | } |
| | | ysbyt.getPack().append(hex); |
| | | ysbyt.setUsart_state(4); |
| | | ysbyt.setDatalen(d); |
| | | break; |
| | | |
| | | case 4: |
| | | ysbyt.getPack().append(hex); |
| | | int lenth = ysbyt.getPack().toString().length(); |
| | | int a = ysbyt.getDatalen() * 2 + 8; |
| | | if (lenth == a) { |
| | | //æ°æ®å¤ççç¶æ |
| | | ysbyt.setUsart_state(0); |
| | | //æ ¡éª |
| | | String check = Jiaoyan.check2(ysbyt.getPack().toString()); |
| | | String panck_cmd = null; |
| | | try { |
| | | panck_cmd = ysbyt.getPack().toString().substring(lenth - 2, lenth) + ysbyt.getPack().substring(lenth - 4, lenth - 2); |
| | | } catch (Exception e) { |
| | | return; |
| | | } |
| | | |
| | | |
| | | //å¦ææ ¡éªæå |
| | | if (check.equals(panck_cmd)) { |
| | | //å¦ææ¯æµè·æ°æ® |
| | | String type = ysbyt.getType(); |
| | | switch (type) { |
| | | case "39": |
| | | Dell_55AA39.dell(ip, ysbyt.getPack().toString(), port); |
| | | break; |
| | | case "40": |
| | | Dell_55AA40.dellrgs(ip, ysbyt.getPack().toString(), port); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | ysbyt.getPack().setLength(0); |
| | | ysbyt.setDatalen(0); |
| | | ysbyt.setType(""); |
| | | } else if (lenth > a) { |
| | | ysbyt.getPack().setLength(0); |
| | | ysbyt.setDatalen(0); |
| | | ysbyt.setType(""); |
| | | } |
| | | |
| | | break; |
| | | |
| | | } |
| | | //å é¤å¤çè¿çæ°æ® |
| | | ysbyt.getData().removeElement(hex); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package baowen; |
| | | |
| | | import tools.GetNowTime; |
| | | import tools.Tools; |
| | | |
| | | import java.io.IOException; |
| | | import java.net.*; |
| | | |
| | | public class Dell_udpReceive implements Runnable{ |
| | | |
| | | int port=7000; |
| | | static DatagramSocket socket; |
| | | static int bytlenth=1024; |
| | | static DatagramPacket packet; |
| | | static boolean startFlag=false; |
| | | private static Thread receiveThread; |
| | | |
| | | public Dell_udpReceive(int port1){ |
| | | try { |
| | | startFlag=true; |
| | | port=port1; |
| | | socket = new DatagramSocket(port); |
| | | byte[] data = new byte[bytlenth]; // å建byteæ°ç» |
| | | packet= new DatagramPacket(data,bytlenth); |
| | | } catch (SocketException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /**å¯å¨çº¿ç¨çæ¹æ³*/ |
| | | public void startThread() { |
| | | receiveThread = new Thread(this); |
| | | receiveThread.start(); |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | while(startFlag) { |
| | | getdata(); |
| | | } |
| | | } |
| | | |
| | | public void getdata() { |
| | | if (socket != null) { |
| | | try { |
| | | socket.receive(packet); // æ¥æ¶æ°æ®å
|
| | | delludp_receive(packet); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static void delludp_receive(DatagramPacket packet) { |
| | | int lenth=packet.getLength(); |
| | | byte[] byt=subBytes(packet.getData(),0, lenth); |
| | | String ip=packet.getAddress().getHostAddress();//è·ååé端çIPå°å对象 |
| | | int port=packet.getPort(); |
| | | String datas = Tools.Bytes2HexString(byt);//䏲壿¥çæ°æ® |
| | | Dell_Baowen.intsert(ip, datas, GetNowTime.timestamp2(), port); |
| | | } |
| | | |
| | | public static byte[] subBytes(byte[] src, int begin, int count) { |
| | | byte[] bs = new byte[count]; |
| | | System.arraycopy(src, begin, bs, 0, count); |
| | | return bs; |
| | | } |
| | | |
| | | |
| | | /**å°æ°æ®åç»åºç«*/ |
| | | public static void sendData(byte[] data,String ip2,int port2) { |
| | | if(socket==null) { |
| | | return; |
| | | } |
| | | String datas1=Tools.Bytes2HexString(data); |
| | | DatagramPacket packet=null; |
| | | InetAddress address=null; |
| | | //å¦ææ°æ®ç±»åç¸å |
| | | try { |
| | | address=InetAddress.getByName(ip2); |
| | | } catch (UnknownHostException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | packet=new DatagramPacket(data, data.length, address, port2); |
| | | try { |
| | | socket.send(packet); |
| | | String datas=Tools.Bytes2HexString(data).substring(0, packet.getLength()*2); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** åæ¢çº¿ç¨å¹¶éæ¾èµæº */ |
| | | public static void stop() { |
| | | |
| | | if (receiveThread != null && receiveThread.isAlive()) { |
| | | try { |
| | | receiveThread.join(); // çå¾
线ç¨ç»æ |
| | | } catch (InterruptedException e) { |
| | | Thread.currentThread().interrupt(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static void stopThread() { |
| | | startFlag = false;//åæ¢æ°æ®æ¥æ¶ |
| | | if (socket != null && !socket.isClosed()) { |
| | | socket.close();//å
³ésocket对象 |
| | | } |
| | | stop(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package baowen; |
| | | |
| | | import gnu.io.SerialPort; |
| | | import index.JPanelMoudle.showDataComp; |
| | | import tools.ChuanKou.SerialPortUtil; |
| | | import tools.Jiaoyan; |
| | | import tools.Tools; |
| | | |
| | | public class WriteReadAnchor { |
| | | |
| | | //ç»æ«æå¨åéæä»¤ |
| | | public static void updateLora(String zhi, |
| | | byte diZhi, |
| | | byte duxie, |
| | | String anchorID, |
| | | SerialPort serialPort, |
| | | byte lenth) { |
| | | byte[] write = write_loraAnc(diZhi,lenth ,duxie, Integer.parseInt(zhi),anchorID); |
| | | if (write == null) { |
| | | return; |
| | | } |
| | | sendData(serialPort, write); |
| | | } |
| | | |
| | | public static void sendData(SerialPort serialPort, byte[] write) { |
| | | SerialPortUtil.sendData(serialPort,write); |
| | | showDataComp.addSendAreaTextMes(write); |
| | | } |
| | | |
| | | |
| | | /**@param start èµ·å§å°å |
| | | * @param lenthæ°æ®é¿åº¦ |
| | | * @param value[]å
·ä½çå¼*/ |
| | | private static byte[] write_loraAnc(byte start ,byte lenth,byte duXie,int value,String id) { |
| | | byte[] buf =new byte[12+lenth]; |
| | | byte[] buf1 =new byte[8+lenth]; |
| | | //å
头 |
| | | buf[0]=(byte) 0x55; |
| | | //å
头 |
| | | buf[1]=(byte) 0xAA; |
| | | //æä»¤ç±»å |
| | | buf1[0]=(byte) 0x30; |
| | | //æ°æ®é¿åº¦ |
| | | buf1[1]=(byte) ((byte) 8+lenth); |
| | | //è¯»æ¨¡å¼ |
| | | buf1[2]=(byte) duXie; |
| | | //èµ·å§å°å |
| | | buf1[3]=(byte) start; |
| | | |
| | | |
| | | buf1[4]= Tools.toByteArray(id)[1]; |
| | | buf1[5]=Tools.toByteArray(id)[0]; |
| | | buf1[6]=Tools.toByteArray(id)[1]; |
| | | buf1[7]=Tools.toByteArray(id)[0]; |
| | | |
| | | buf1[8]=Tools.intToRegisters(value)[3]; |
| | | buf1[9]=Tools.intToRegisters(value)[2]; |
| | | for(int i=0;i<buf1.length;i++) { |
| | | buf[i+2]=buf1[i]; |
| | | } |
| | | //æ ¡éªç |
| | | buf[buf.length-2]= Jiaoyan.check(buf1)[1]; |
| | | buf[buf.length-1]=Jiaoyan.check(buf1)[0]; |
| | | return buf; |
| | | } |
| | | |
| | | |
| | | |
| | | //è¯»åæ«æå¨çæ¬ |
| | | public static byte[] ReadData(byte ref, byte lenth) { |
| | | byte[] buf = new byte[10]; |
| | | byte[] buf1 = new byte[6]; |
| | | //å
头 |
| | | buf[0] = (byte) 0x55; |
| | | //å
头 |
| | | buf[1] = (byte) 0xAA; |
| | | |
| | | //æä»¤ç±»å(åæ°è¯»åæ¨¡å¼) |
| | | buf1[0] = (byte) 0x03; |
| | | //æ°æ®é¿åº¦ |
| | | buf1[1] = (byte) 0x06; |
| | | |
| | | //读模å¼(读模å¼) |
| | | buf1[2] = (byte) 0x01; |
| | | //èµ·å§å°å |
| | | buf1[3] = ref; |
| | | //æ°æ®é¿åº¦ |
| | | buf1[4] = lenth; |
| | | |
| | | //åºå®å¼(读模å¼ä¸ä¸º0x00) |
| | | buf1[5] = (byte) 0x00; |
| | | for (int i = 0; i < buf1.length; i++) { |
| | | buf[i + 2] = buf1[i]; |
| | | } |
| | | |
| | | //æ ¡éªç |
| | | buf[8] = Jiaoyan.check(buf1)[1]; |
| | | buf[9] = Jiaoyan.check(buf1)[0]; |
| | | ; |
| | | return buf; |
| | | } |
| | | |
| | | |
| | | /**@param start èµ·å§å°å |
| | | * @param lenthæ°æ®é¿åº¦ |
| | | * @param value[]å
·ä½çå¼*/ |
| | | public static byte[] write_lora(byte start ,byte lenth,int value) { |
| | | byte[] buf =new byte[9+lenth]; |
| | | byte[] buf1 =new byte[5+lenth]; |
| | | //å
头 |
| | | buf[0]=(byte) 0x55; |
| | | //å
头 |
| | | buf[1]=(byte) 0xAA; |
| | | //æä»¤ç±»å |
| | | buf1[0]=(byte) 0x03; |
| | | //æ°æ®é¿åº¦ |
| | | buf1[1]=(byte) ((byte) 5+lenth); |
| | | //è¯»æ¨¡å¼ |
| | | buf1[2]=(byte) 0x02; |
| | | //èµ·å§å°å |
| | | buf1[3]=(byte) start; |
| | | |
| | | //读åé¿åº¦ |
| | | buf1[4]=(byte) lenth; |
| | | |
| | | buf1[5]=Tools.intToRegisters(value)[3]; |
| | | buf1[6]=Tools.intToRegisters(value)[2]; |
| | | for(int i=0;i<buf1.length;i++) { |
| | | buf[i+2]=buf1[i]; |
| | | } |
| | | //æ ¡éªç |
| | | buf[buf.length-2]=Jiaoyan.check(buf1)[1]; |
| | | buf[buf.length-1]=Jiaoyan.check(buf1)[0]; |
| | | return buf; |
| | | |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package baowen; |
| | | import java.util.Vector; |
| | | |
| | | /**åå§æ°æ®*/ |
| | | public class YuanshiByt { |
| | | String time; |
| | | int timestamp; |
| | | int port; |
| | | String ip; |
| | | Vector<String> data; |
| | | StringBuffer pack; |
| | | int datalen;//æ°æ®çé¿åº¦ |
| | | String type;//æ°æ®ç±»å |
| | | //å¤çæ°æ®å°ä»ä¹ç¶æäº |
| | | int usart_state; |
| | | |
| | | public String getIp() { |
| | | return ip; |
| | | } |
| | | public Vector<String> getData() { |
| | | return data; |
| | | } |
| | | public void setIp(String ip) { |
| | | this.ip = ip; |
| | | } |
| | | public void setData(Vector<String> data) { |
| | | this.data = data; |
| | | } |
| | | public StringBuffer getPack() { |
| | | return pack; |
| | | } |
| | | public void setPack(StringBuffer pack) { |
| | | this.pack = pack; |
| | | } |
| | | public String getTime() { |
| | | return time; |
| | | } |
| | | public int getTimestamp() { |
| | | return timestamp; |
| | | } |
| | | public void setTime(String time) { |
| | | this.time = time; |
| | | } |
| | | public void setTimestamp(int timestamp) { |
| | | this.timestamp = timestamp; |
| | | } |
| | | public int getUsart_state() { |
| | | return usart_state; |
| | | } |
| | | public void setUsart_state(int usart_state) { |
| | | this.usart_state = usart_state; |
| | | } |
| | | public int getPort() { |
| | | return port; |
| | | } |
| | | public void setPort(int port) { |
| | | this.port = port; |
| | | } |
| | | public int getDatalen() { |
| | | return datalen; |
| | | } |
| | | public void setDatalen(int datalen) { |
| | | this.datalen = datalen; |
| | | } |
| | | public String getType() { |
| | | return type; |
| | | } |
| | | public void setType(String type) { |
| | | this.type = type; |
| | | } |
| | | |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package index; |
| | | |
| | | import index.JPanelMoudle.*; |
| | | |
| | | import java.awt.*; |
| | | import javax.swing.*; |
| | | |
| | | |
| | | /** |
| | | * @author zsh |
| | | */ |
| | | public class GCUWBreadIndex extends JFrame { |
| | | static ImageIcon icon = new ImageIcon("image/scan.png"); // è¯·æ¿æ¢ä¸ºä½ 徿 çè·¯å¾ |
| | | public GCUWBreadIndex() { |
| | | // è·åå±å¹çå辨ç |
| | | GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
| | | GraphicsDevice gd = ge.getDefaultScreenDevice(); |
| | | Rectangle screenBounds = gd.getDefaultConfiguration().getBounds(); |
| | | int screenWidth = screenBounds.width; |
| | | int screenHeight = screenBounds.height; |
| | | |
| | | // æ ¹æ®å±å¹å°ºå¯¸è°æ´çªå£å¤§å° |
| | | setSize(screenWidth / 2, screenHeight / 2); // 设置çªå£ä¸ºå±å¹å°ºå¯¸çä¸å |
| | | setLocationRelativeTo(null); // å±
䏿¾ç¤ºçªå£ |
| | | |
| | | // çªå£å±æ§ |
| | | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| | | setTitle("GC UWBä¿¡æ æ«æè½¯ä»¶V1.2"); |
| | | setIconImage(icon.getImage()); |
| | | initComponents(); |
| | | } |
| | | |
| | | private void initComponents() { |
| | | try { |
| | | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | Container contentPane = getPane(); |
| | | anchor_TableList(contentPane); |
| | | readComList(contentPane); |
| | | readWriteAnchorParams(contentPane); |
| | | DataShow(contentPane); |
| | | versionUpgrade(contentPane); |
| | | pack(); |
| | | setLocationRelativeTo(getOwner()); |
| | | } |
| | | |
| | | |
| | | //鿩䏲å£çé¢ |
| | | private void readComList(Container contentPane) { |
| | | ComMoudleComp.getCom_jp().setLayout(new GridBagLayout()); |
| | | int[] columnWidths = {110, 110, 220}; |
| | | int[] rowHeights = {46, 46, 0, 46}; |
| | | double[] columnWeights = {1.0, 1.0, 1.0}; |
| | | double[] rowWeights = {1.0, 1.0, 1.0, 1.0}; |
| | | //设置å¸å±å¨çåæ° |
| | | setGridBagLayoutParams(ComMoudleComp.getCom_jp(), columnWidths, rowHeights, columnWeights, rowWeights); |
| | | // æ·»å ç»ä»¶ |
| | | addLabelAndField(ComMoudleComp.getCom_jp(), ComMoudleComp.getChuanKou_jl(), ComMoudleComp.getChuanKou_jcom(), 0, 0,2); |
| | | addLabelAndField(ComMoudleComp.getCom_jp(), ComMoudleComp.getBotelv_jl(), ComMoudleComp.getBoteLV_jcm(), 0, 1,2); |
| | | // æ·»å æé® |
| | | addButton(ComMoudleComp.getCom_jp(), ComMoudleComp.getOpen(), 0, 3, 2, 1); |
| | | addButton(ComMoudleComp.getCom_jp(), ComMoudleComp.getClose(), 2, 3, 1, 1); |
| | | // æåå°å®¹å¨æ·»å å° contentPane |
| | | contentPane.add(ComMoudleComp.getCom_jp(), |
| | | new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, |
| | | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
| | | new Insets(0, 0, 5, 5), 0, 0)); |
| | | } |
| | | |
| | | //ä¿¡æ å表模å |
| | | private void anchor_TableList(Container contentPane) { |
| | | anchorListComp.getAncList_jp().setLayout(new GridBagLayout()); |
| | | int[] columnWidths = {449, 0}; |
| | | int[] rowHeights = {480, 50, 0}; |
| | | double[] columnWeights = {1.0, 1.0E-4}; |
| | | double[] rowWeights = {1.0, 0.0, 1.0E-4}; |
| | | // 设置 GridBagLayout é
ç½® |
| | | setGridBagLayoutParams(anchorListComp.getAncList_jp(), columnWidths, rowHeights, columnWeights, rowWeights); |
| | | // æ·»å ç»ä»¶ |
| | | addScrollPane(anchorListComp.getAncList_jp(), anchorListComp.getScrollPane1(), anchorListComp.getAncTable(), 0, 0, 1, 1); |
| | | addButton(anchorListComp.getAncList_jp(), anchorListComp.getSelect_anc(), 0, 1, 1, 1); |
| | | |
| | | // æåå°å®¹å¨æ·»å å° contentPane |
| | | contentPane.add(anchorListComp.getAncList_jp(), |
| | | new GridBagConstraints(0, 1, 1, 2, 0.0, 0.0, |
| | | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
| | | new Insets(0, 0, 0, 5), 0, 0)); |
| | | } |
| | | |
| | | //åºä»¶å级çé¢ |
| | | private void versionUpgrade(Container contentPane) { |
| | | versionUpgradeComp.getVersionUpdate().setLayout(new GridBagLayout()); |
| | | int[] columnWidths = {143, 350, 0}; |
| | | int[] rowHeights = {38, 54, 50, 58, 54, 0}; |
| | | double[] columnWeights = {1.0, 1.0, 1.0}; |
| | | double[] rowWeights = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; |
| | | // 设置 GridBagLayout é
ç½® |
| | | setGridBagLayoutParams(versionUpgradeComp.getVersionUpdate(), columnWidths, rowHeights, columnWeights, rowWeights); |
| | | addLabelAndField(versionUpgradeComp.getVersionUpdate(), versionUpgradeComp.getLabel9(), versionUpgradeComp.getLabel9(), 0, 0,2); |
| | | addLabelAndField(versionUpgradeComp.getVersionUpdate(), versionUpgradeComp.getReadVersion_jb(), versionUpgradeComp.getReadVersion_jf(), 0, 1,2); |
| | | addLabelAndField(versionUpgradeComp.getVersionUpdate(), versionUpgradeComp.getChooseVersion(), versionUpgradeComp.getChooseVersion_jf(), 0, 2,2); |
| | | addLabelAndField(versionUpgradeComp.getVersionUpdate(), versionUpgradeComp.getWrite_saoMiao(), versionUpgradeComp.getJindu_SaoMiaoQi(), 0, 3,2); |
| | | addLabelAndField(versionUpgradeComp.getVersionUpdate(), versionUpgradeComp.getUpdateAnchor_jb(), versionUpgradeComp.getUpdateAnchor_jf(), 0, 4,2); |
| | | // æ·»å å° contentPane |
| | | contentPane.add(versionUpgradeComp.getVersionUpdate(), new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, |
| | | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
| | | new Insets(0, 0, 0, 5), 0, 0)); |
| | | } |
| | | |
| | | |
| | | //读ååæ°æ¨¡å |
| | | private void readWriteAnchorParams(Container contentPane) { |
| | | readParamsComp.getReadAncParam_jp().setLayout(new GridBagLayout()); |
| | | int[] columnWidths = {139, 124*2,30, 160, 0};//æ¯åç宽度 |
| | | int[] rowHeights = {50, 50, 50, 50, 50, 50, 50, 50, 50, 0};//æ¯è¡çé«åº¦ |
| | | double[] columnWeights = {1.0, 1.0, 1.0,1.0, 0}; |
| | | double[] rowWeights = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0}; |
| | | // 设置 GridBagLayout é
ç½® |
| | | setGridBagLayoutParams(readParamsComp.getReadAncParam_jp(), columnWidths, rowHeights, columnWeights, rowWeights); |
| | | // æ·»å ç»ä»¶ |
| | | addLabelAndField(readParamsComp.getReadAncParam_jp(), readParamsComp.getAnchorId(), readParamsComp.getAnchorId_jf(), 0, 1,3); |
| | | addLabelAndField(readParamsComp.getReadAncParam_jp(), readParamsComp.getAnchorVersion(), readParamsComp.getAnchorVersion_jf(), 0, 2,3); |
| | | addLabelAndFieldAndButton(readParamsComp.getReadAncParam_jp(), readParamsComp.getGroupId(), readParamsComp.getGroupId_jf(), readParamsComp.getSaveParam_GroupID(),0, 3); |
| | | addLabelAndFieldAndButton(readParamsComp.getReadAncParam_jp(), readParamsComp.getAnchorPower(), readParamsComp.getAnchorPower_jf(), readParamsComp.getSaveParam_anchorPower(),0, 4); |
| | | addLabelAndFieldAndButton(readParamsComp.getReadAncParam_jp(), readParamsComp.getJiaoZhunDis(), readParamsComp.getJiaoZhunDis_jf(), readParamsComp.getSaveParam_jiaoZhunDis(),0, 5); |
| | | addLabelAndFieldAndButton(readParamsComp.getReadAncParam_jp(), readParamsComp.getLabel6(), readParamsComp.getTextField6(), readParamsComp.getSaveParam_label6(),0, 6); |
| | | addLabelAndFieldAndButton(readParamsComp.getReadAncParam_jp(), readParamsComp.getLabel7(), readParamsComp.getTextField7(),readParamsComp.getSaveParam_label7(), 0, 7); |
| | | addLabelAndFieldAndButton(readParamsComp.getReadAncParam_jp(), readParamsComp.getLabel8(), readParamsComp.getTextField8(), readParamsComp.getSaveParam_label8(),0, 8); |
| | | // æ·»å æé® |
| | | addButton(readParamsComp.getReadAncParam_jp(), readParamsComp.getReadParam(), 0, 0, 4, 1); |
| | | |
| | | // æåå°å®¹å¨æ·»å å° contentPane |
| | | contentPane.add(readParamsComp.getReadAncParam_jp(), |
| | | new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0, |
| | | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
| | | new Insets(0, 0, 5, 5), 0, 0)); |
| | | } |
| | | |
| | | |
| | | //æ°æ®æ¥æ¶è§£ææ¨¡å |
| | | private void DataShow(Container contentPane) { |
| | | showDataComp.getDataShow().setViewportView(showDataComp.getTextArea1()); |
| | | contentPane.add(showDataComp.getDataShow(), |
| | | new GridBagConstraints(2, 0, 1, 3, 0.0, 0.0, |
| | | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
| | | new Insets(0, 0, 0, 0), 0, 0)); |
| | | } |
| | | |
| | | // ç®åçæ ç¾åè¾å
¥æ¡æ·»å æ¹æ³ |
| | | private void addLabelAndField(Container panel, JComponent label, JComponent textField, int gridx, int gridy,int width) { |
| | | panel.add(label, new GridBagConstraints(gridx, gridy, 1, 1, 0.0, 0.0, |
| | | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
| | | new Insets(0, 0, 5, 5), 0, 0)); |
| | | panel.add(textField, new GridBagConstraints(gridx + 1, gridy, width,1, 0.0, 0.0, |
| | | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
| | | new Insets(0, 0, 5, 0), 0, 0)); |
| | | } |
| | | |
| | | // ç®åçæ ç¾åè¾å
¥ æé®æ·»å æ¹æ³ |
| | | private void addLabelAndFieldAndButton(Container panel, JComponent label, JComponent textField, JComponent button,int gridx, int gridy) { |
| | | panel.add(label, new GridBagConstraints(gridx, gridy, 1, 1, 0.0, 0.0, |
| | | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
| | | new Insets(0, 0, 5, 5), 0, 0)); |
| | | |
| | | panel.add(textField, new GridBagConstraints(gridx + 1, gridy, 1, 1, 0.0, 0.0, |
| | | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
| | | new Insets(0, 0, 5, 0), 0, 0)); |
| | | |
| | | panel.add(button, new GridBagConstraints(gridx + 3, gridy, 1, 1, 0.0, 0.0, |
| | | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
| | | new Insets(0, 0, 5, 0), 0, 0)); |
| | | } |
| | | |
| | | // ç®åçæé®æ·»å æ¹æ³ |
| | | private void addButton(Container panel, JButton button, int gridx, int gridy, int gridwidth, int gridheight) { |
| | | panel.add(button, new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 0.0, 0.0, |
| | | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
| | | new Insets(0, 0, 5, 0), 0, 0)); |
| | | } |
| | | |
| | | // æ·»å JScrollPane çæ¹æ³ |
| | | private void addScrollPane(Container panel, JScrollPane scrollPane, JTable table, int gridx, int gridy, int gridwidth, int gridheight) { |
| | | scrollPane.setViewportView(table); |
| | | panel.add(scrollPane, new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 0.0, 0.0, |
| | | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
| | | new Insets(0, 0, 5, 0), 0, 0)); |
| | | } |
| | | |
| | | //å
¨å±é¡µé¢ |
| | | private Container getPane() { |
| | | Container contentPane = getContentPane(); |
| | | contentPane.setLayout(new GridBagLayout()); |
| | | // 设置éåºçªå£å¤§å°çå¸å± |
| | | ((GridBagLayout) contentPane.getLayout()).columnWidths = new int[]{0, 504, 374, 0}; // å¯ä»¥è°æ´å®½åº¦ |
| | | ((GridBagLayout) contentPane.getLayout()).rowHeights = new int[]{170, 268, 315, 0}; // å¯ä»¥è°æ´é«åº¦ |
| | | // 设置伸缩æéï¼ç¡®ä¿å
容å¨çªå£æ¾å¤§æ¶èªéåº |
| | | ((GridBagLayout) contentPane.getLayout()).columnWeights = new double[]{1.0, 1.0, 1.0, 1.0E-4}; |
| | | ((GridBagLayout) contentPane.getLayout()).rowWeights = new double[]{1.0, 1.0, 1.0, 1.0E-4}; |
| | | return contentPane; |
| | | } |
| | | |
| | | private void setGridBagLayoutParams(JPanel jPanel, int[] columnWidths, int[] rowHeights, double[] columnWeights, double[] rowWeights) { |
| | | GridBagLayout layout = (GridBagLayout) jPanel.getLayout(); |
| | | layout.columnWidths = columnWidths; |
| | | layout.rowHeights = rowHeights; |
| | | layout.columnWeights = columnWeights; |
| | | layout.rowWeights = rowWeights; |
| | | } |
| | | |
| | | public static void startFrame(){ |
| | | SwingUtilities.invokeLater(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | new GCUWBreadIndex().setVisible(true); |
| | | } |
| | | }); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package index.JPanelMoudle; |
| | | |
| | | import Anchor.anchor_Dell; |
| | | import baowen.Dell_Baowen; |
| | | import gnu.io.SerialPort; |
| | | import gnu.io.SerialPortEvent; |
| | | import tools.ChuanKou.SerialPortManager; |
| | | import tools.ChuanKou.SerialPortUtil; |
| | | import tools.GetNowTime; |
| | | import tools.ShowMessage; |
| | | import tools.Tools; |
| | | |
| | | import javax.swing.*; |
| | | import javax.swing.border.TitledBorder; |
| | | import java.awt.event.ActionEvent; |
| | | import java.awt.event.ActionListener; |
| | | import java.awt.event.FocusEvent; |
| | | import java.awt.event.FocusListener; |
| | | import java.util.TooManyListenersException; |
| | | |
| | | public class ComMoudleComp { |
| | | |
| | | |
| | | private static JPanel com_jp;//鿩䏲壿¨¡æ¿ |
| | | |
| | | private static JLabel chuanKou_jl;//串å£å·æ ç¾ |
| | | private static JLabel botelv_jl;//æ³¢ç¹çæ ç¾ |
| | | private static JComboBox chuanKou_jcom;//串å£å·ä¸ææ¡ |
| | | private static JComboBox boteLV_jcm;//æ³¢ç¹çä¸ææ¡ |
| | | private static JButton open;//æå¼ä¸²å£æé® |
| | | private static JButton close;//å
³éä¸²å£æé® |
| | | static String[] coms = null; |
| | | static String com = null;//éä¸çä¸²å£ |
| | | static boolean chuankou = false;//䏲壿¯å¦è¢«æå¼ |
| | | static SerialPort serialPort = null;//串å£å¯¹è±¡ |
| | | |
| | | // è·å鿩䏲å£é¢æ¿ï¼è¥ä¸ºç©ºåå建 |
| | | public static JPanel getCom_jp() { |
| | | if (com_jp == null) { |
| | | com_jp = new JPanel(); // åå§åè¯¥é¢æ¿ |
| | | com_jp.setBorder(new TitledBorder("鿩䏲å£")); |
| | | } |
| | | return com_jp; |
| | | } |
| | | |
| | | // è·å 串å£å·æ ç¾ï¼è¥ä¸ºç©ºåå建 |
| | | public static JLabel getChuanKou_jl() { |
| | | if (chuanKou_jl == null) { |
| | | chuanKou_jl = new JLabel(" 串å£å·ï¼"); // åå§å该æ ç¾ |
| | | } |
| | | return chuanKou_jl; |
| | | } |
| | | |
| | | // è·åæ³¢ç¹çæ ç¾ï¼è¥ä¸ºç©ºåå建 |
| | | public static JLabel getBotelv_jl() { |
| | | if (botelv_jl == null) { |
| | | botelv_jl = new JLabel(" æ³¢ç¹çï¼"); // åå§å该æ ç¾ |
| | | } |
| | | return botelv_jl; |
| | | } |
| | | |
| | | // è·å 串å£å·ä¸ææ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static JComboBox getChuanKou_jcom() { |
| | | if (chuanKou_jcom == null) { |
| | | chuanKou_jcom = new JComboBox(); // åå§åè¯¥ä¸ææ¡ |
| | | chuanKou_jcom.addFocusListener(new FocusListener() { |
| | | @Override |
| | | public void focusGained(FocusEvent e) { |
| | | int comnum = SerialPortManager.get_all_port().size(); |
| | | if (coms.length != comnum) { |
| | | coms = new String[SerialPortManager.get_all_port().size()]; |
| | | for (int i = 0; i < SerialPortManager.get_all_port().size(); i++) { |
| | | coms[i] = SerialPortManager.get_all_port().get(i); |
| | | } |
| | | ComboBoxModel<String> coModel = new DefaultComboBoxModel<>(coms);//䏿å表模å |
| | | chuanKou_jcom.setModel(coModel); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void focusLost(FocusEvent e) { |
| | | |
| | | } |
| | | }); |
| | | if (SerialPortManager.get_all_port().size() != 0) { |
| | | coms = new String[SerialPortManager.get_all_port().size()]; |
| | | for (int i = 0; i < SerialPortManager.get_all_port().size(); i++) { |
| | | coms[i] = SerialPortManager.get_all_port().get(i); |
| | | } |
| | | } else { |
| | | coms = new String[1]; |
| | | coms[0] = "æªè¯å«å°comå£"; |
| | | } |
| | | ComboBoxModel<String> coModel = new DefaultComboBoxModel<>(coms);//䏿å表模å |
| | | chuanKou_jcom.setModel(coModel); |
| | | } |
| | | return chuanKou_jcom; |
| | | } |
| | | |
| | | |
| | | // è·åæ³¢ç¹ç䏿æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static JComboBox getBoteLV_jcm() { |
| | | if (boteLV_jcm == null) { |
| | | String[] com = {"115200", "921600", "19200", "9600"}; |
| | | boteLV_jcm = new JComboBox(com); // åå§åè¯¥ä¸ææ¡ |
| | | } |
| | | return boteLV_jcm; |
| | | } |
| | | |
| | | |
| | | // è·å æå¼ä¸²å£ æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getOpen() { |
| | | if (open == null) { |
| | | open = new JButton("æå¼ä¸²å£"); // åå§å该æé® |
| | | open.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | com = (String) chuanKou_jcom.getSelectedItem(); |
| | | if (com == null || com.equals("æªè¯å«å°comå£")) { |
| | | ShowMessage.zidingyi("æªè¯å«å°comå£,请æå
¥é
置线并éå¯è½¯ä»¶"); |
| | | comshuaxin(); |
| | | return; |
| | | } |
| | | int bote = Integer.parseInt((String) boteLV_jcm.getSelectedItem()); |
| | | if (!chuankou) { |
| | | serialPort = SerialPortManager.open_chuankou(com, bote); |
| | | if (serialPort == null) { |
| | | return; |
| | | } |
| | | chuankou = true;//串å£è¢«æå¼ |
| | | open.setEnabled(false); |
| | | close.setEnabled(true); |
| | | try { |
| | | SerialPortUtil.setListenerToSerialPort(serialPort, event -> { |
| | | |
| | | //æ°æ®éç¥ |
| | | if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) { |
| | | if (!versionUpgradeComp.isWrite()) { |
| | | byte[] bytes = SerialPortUtil.readData(serialPort); |
| | | if (bytes == null) { |
| | | return; |
| | | } |
| | | |
| | | showDataComp.addAreaTextMes(bytes,"127.0.0.1",8888); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | |
| | | } catch (TooManyListenersException e1) { |
| | | // TODO èªå¨çæç catch å |
| | | e1.printStackTrace(); |
| | | } |
| | | } else { |
| | | ShowMessage.zidingyi("请å
å
³éå·²æå¼ç串å£"); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | return open; |
| | | } |
| | | |
| | | // è·å å
³éä¸²å£ æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getClose() { |
| | | if (close == null) { |
| | | close = new JButton("å
³é串å£"); // åå§å该æé® |
| | | close.setEnabled(false); |
| | | close.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | try { |
| | | if (chuankou) { |
| | | SerialPortUtil.closeSerialPort(serialPort); |
| | | open.setEnabled(true); |
| | | close.setEnabled(false); |
| | | chuankou = false; |
| | | anchor_Dell.getAnchors().removeAllElements();//æ¸
空信æ åºç«çéå |
| | | anchorListComp.updateTable();//æ¸
空信æ å表 |
| | | } else { |
| | | ShowMessage.zidingyi("䏲壿ªæå¼"); |
| | | } |
| | | } catch (Exception ex) { |
| | | throw new RuntimeException(ex); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | return close; |
| | | } |
| | | |
| | | |
| | | //éæ°å·æ°ä¸²å£ |
| | | public static void comshuaxin() { |
| | | int comnum = SerialPortManager.get_all_port().size(); |
| | | if (comnum != 0) { |
| | | coms = new String[SerialPortManager.get_all_port().size()]; |
| | | for (int i = 0; i < SerialPortManager.get_all_port().size(); i++) { |
| | | coms[i] = SerialPortManager.get_all_port().get(i); |
| | | } |
| | | } else { |
| | | coms = new String[1]; |
| | | coms[0] = "æªè¯å«å°comå£"; |
| | | } |
| | | ComboBoxModel<String> coModel = new DefaultComboBoxModel<>(coms);//䏿å表模å |
| | | chuanKou_jcom.setModel(coModel); |
| | | chuanKou_jcom.repaint(); |
| | | } |
| | | |
| | | public static SerialPort getSerialPort() { |
| | | return serialPort; |
| | | } |
| | | |
| | | public static boolean isChuankou() { |
| | | return chuankou; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package index.JPanelMoudle; |
| | | |
| | | |
| | | import Anchor.Anchor; |
| | | import Anchor.anchor_Dell; |
| | | import tools.ShowMessage; |
| | | |
| | | import javax.swing.*; |
| | | import javax.swing.border.TitledBorder; |
| | | import javax.swing.table.DefaultTableCellRenderer; |
| | | import javax.swing.table.DefaultTableModel; |
| | | import javax.swing.table.TableCellRenderer; |
| | | |
| | | import java.awt.*; |
| | | import java.awt.event.ActionEvent; |
| | | import java.awt.event.ActionListener; |
| | | import java.util.HashSet; |
| | | import java.util.Iterator; |
| | | import java.util.Set; |
| | | import java.util.Vector; |
| | | |
| | | public class anchorListComp { |
| | | private static JPanel ancList_jp;//åºç«åè¡¨é¢æ¿ |
| | | |
| | | private static JTable ancTable;//åºç«å表 |
| | | private static DefaultTableModel model;//è¡¨æ ¼æ¨¡å |
| | | private static JButton select_anc;//æ«æä¿¡æ æé® |
| | | private static JScrollPane scrollPane1;//è§£ææ°æ®æ»å¨é¢æ¿ |
| | | |
| | | private static Vector<Vector<String>> rowData = null; |
| | | private static Set<Anchor> existingIds = new HashSet<>(); |
| | | ; |
| | | |
| | | // è·å ancList_jp 颿¿ï¼è¥ä¸ºç©ºåå建 |
| | | public static JPanel getAncList_jp() { |
| | | if (ancList_jp == null) { |
| | | ancList_jp = new JPanel(); // åå§åè¯¥é¢æ¿ |
| | | ancList_jp.setBorder(new TitledBorder("ä¿¡æ å表")); |
| | | } |
| | | return ancList_jp; |
| | | } |
| | | |
| | | public static JTable getAncTable() { |
| | | if (ancTable == null) { |
| | | String[] columnNames = {"设å¤ID", "æµè·è·ç¦»(cm)"}; // è¡¨æ ¼åå |
| | | Vector<String> columnName = new Vector<>(); |
| | | for (int i = 0; i < columnNames.length; i++) { |
| | | columnName.add(columnNames[i]); |
| | | } |
| | | |
| | | // è¡¨æ ¼åå§å |
| | | ancTable = new JTable(rowData, columnName) { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | // ç¦æ¢ç¼è¾ |
| | | public boolean isCellEditable(int row, int column) { |
| | | return false; |
| | | } |
| | | |
| | | // éå prepareRenderer å®ç°éè¡åè² |
| | | @Override |
| | | public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { |
| | | Component c = super.prepareRenderer(renderer, row, column); |
| | | |
| | | // 设置éè¡åè²ï¼å¶æ°è¡ä¸ºç½è²ï¼å¥æ°è¡ä¸ºç°è² |
| | | if (!isRowSelected(row)) { |
| | | if (row % 2 == 0) { |
| | | c.setBackground(new Color(240, 240, 240)); // å¶æ°è¡ç°è² |
| | | } else { |
| | | c.setBackground(Color.white); // 奿°è¡ç½è² |
| | | } |
| | | } |
| | | return c; |
| | | } |
| | | }; |
| | | ancTable.setColumnSelectionAllowed(false); |
| | | ancTable.getTableHeader().setReorderingAllowed(false); |
| | | // è®¾ç½®è¡¨æ ¼æ ·å¼ |
| | | setShowStyle(); |
| | | // 设置åå¤´æ ·å¼ |
| | | ancTable.getTableHeader().setPreferredSize(new Dimension(ancTable.getTableHeader().getPreferredSize().width, 40)); |
| | | DefaultTableCellRenderer headerRenderer = new DefaultTableCellRenderer(); |
| | | headerRenderer.setHorizontalAlignment(SwingConstants.CENTER); // å头æåå±
ä¸ |
| | | for (int i = 0; i < ancTable.getColumnCount(); i++) { |
| | | ancTable.getColumnModel().getColumn(i).setHeaderRenderer(headerRenderer); |
| | | } |
| | | // 设置è¡é« |
| | | ancTable.setRowHeight(35); // 设置æ¯è¡çé«åº¦ä¸º35åç´ |
| | | // è·åè¡¨æ ¼æ¨¡åå¹¶å è½½æ°æ® |
| | | model = (DefaultTableModel) ancTable.getModel(); |
| | | getRowData(anchor_Dell.getAnchors()); |
| | | ancTable.revalidate(); |
| | | ancTable.repaint(); |
| | | } |
| | | return ancTable; |
| | | } |
| | | |
| | | |
| | | public static void getRowData(Vector<Anchor> s2_vector) { |
| | | // éåæ°æ°æ® |
| | | for (Anchor s2 : s2_vector) { |
| | | String anchorId = s2.getAnchorId().trim(); |
| | | String newDistance = s2.getDistance().trim(); |
| | | |
| | | boolean found = false; |
| | | |
| | | // éåç°ææ¨¡åï¼å¤ææ¯å¦åå¨ç¸åç AnchorId |
| | | for (int i = 0; i < model.getRowCount(); i++) { |
| | | String existingId = ((String) model.getValueAt(i, 0)).trim(); |
| | | String existingDistance = ((String) model.getValueAt(i, 1)).trim(); |
| | | if (existingId.equals(anchorId)) { |
| | | found = true; |
| | | // 妿è·ç¦»ä¸åï¼æ´æ°æ¨¡å |
| | | if (!existingDistance.equals(newDistance)) { |
| | | model.setValueAt(newDistance, i, 1); // æ´æ°è·ç¦» |
| | | } |
| | | break; |
| | | } |
| | | } |
| | | // å¦ææ²¡ææ¾å°ï¼æ·»å æ°è¡ |
| | | if (!found) { |
| | | Vector<String> row = new Vector<>(); |
| | | row.add(anchorId); |
| | | row.add(newDistance); |
| | | model.addRow(row); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // è·å select_anc æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getSelect_anc() { |
| | | if (select_anc == null) { |
| | | select_anc = new JButton("æ«æä¿¡æ "); // åå§å该æé® |
| | | select_anc.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | if (ComMoudleComp.isChuankou()) { |
| | | updateTable(); |
| | | ShowMessage.zidingyi("æ£å¨éæ°æ«æä¿¡æ "); |
| | | } else { |
| | | ShowMessage.zidingyi("请å
æå¼ä¸²å£"); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | return select_anc; |
| | | } |
| | | |
| | | // è·å è§£ææ°æ®æ»å¨é¢æ¿ï¼è¥ä¸ºç©ºåå建 |
| | | public static JScrollPane getScrollPane1() { |
| | | if (scrollPane1 == null) { |
| | | scrollPane1 = new JScrollPane(); // åå§å该æ»å¨é¢æ¿ |
| | | } |
| | | return scrollPane1; |
| | | } |
| | | |
| | | |
| | | //è®¾ç½®è¡¨æ ¼æ¾ç¤ºé£æ ¼ |
| | | private static void setShowStyle() { |
| | | // è·åè¡¨æ ¼çåæ° |
| | | int columnCount = ancTable.getColumnCount(); |
| | | // å建ä¸ä¸ªé»è®¤çåå
æ ¼æ¸²æå¨ï¼ç¨äºå±
䏿¾ç¤º |
| | | DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); |
| | | renderer.setHorizontalAlignment(SwingConstants.CENTER); |
| | | // 为æ¯ä¸å设置该渲æå¨ |
| | | for (int i = 0; i < columnCount; i++) { |
| | | ancTable.getColumnModel().getColumn(i).setCellRenderer(renderer); |
| | | } |
| | | // 为å头设置å±
䏿¸²æå¨ |
| | | DefaultTableCellRenderer headerRenderer = new DefaultTableCellRenderer(); |
| | | headerRenderer.setHorizontalAlignment(SwingConstants.CENTER); |
| | | for (int i = 0; i < columnCount; i++) { |
| | | // 设置åå¤´çæ¸²æå¨ |
| | | ancTable.getTableHeader().getColumnModel().getColumn(i).setHeaderRenderer(headerRenderer); |
| | | } |
| | | } |
| | | |
| | | |
| | | //å·æ°è¡¨æ ¼çæ¹æ³ |
| | | public static void updateTable() { |
| | | if (model != null) { |
| | | // æ¸
空ææè¡ |
| | | existingIds.clear(); |
| | | model.setRowCount(0); |
| | | anchor_Dell.getAnchors().removeAllElements();//å 餿æä¿¡æ |
| | | getRowData(anchor_Dell.getAnchors()); |
| | | scrollPane1.validate(); |
| | | } |
| | | } |
| | | |
| | | public static String getChooseAnchorId() { |
| | | String anchorID = null; |
| | | int selectedRow = anchorListComp.getAncTable().getSelectedRow(); |
| | | if (selectedRow != -1) { |
| | | anchorID = (String) anchorListComp.getAncTable().getValueAt(selectedRow, 0); |
| | | } |
| | | return anchorID; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package index.JPanelMoudle; |
| | | |
| | | import baowen.WriteReadAnchor; |
| | | import tools.Judge.JugeIn; |
| | | import tools.ShowMessage; |
| | | |
| | | import javax.swing.*; |
| | | import javax.swing.border.TitledBorder; |
| | | import java.awt.event.ActionEvent; |
| | | import java.awt.event.ActionListener; |
| | | |
| | | public class readParamsComp { |
| | | |
| | | private static JPanel readAncParam_jp;//读åä¿¡æ é
ç½®é¢æ¿ |
| | | private static JButton readParam;//读ååæ°æé® |
| | | private static JLabel anchorId;//åºç«ID |
| | | private static JLabel anchorVersion;//ä¿¡æ çæ¬å· |
| | | private static JLabel groupId;//é讯å°ç» |
| | | private static JLabel anchorPower;//ä¿¡æ åç |
| | | private static JLabel jiaoZhunDis;//æ ¡åè·ç¦» |
| | | private static JLabel label6;//ä¿çåæ°1 |
| | | private static JLabel label7;//ä¿çåæ°2 |
| | | private static JLabel label8;//ä¿çåæ°3 |
| | | private static JTextField anchorId_jf; |
| | | private static JTextField anchorVersion_jf; |
| | | private static JTextField groupId_jf; |
| | | private static JTextField anchorPower_jf; |
| | | private static JTextField jiaoZhunDis_jf; |
| | | private static JTextField textField6; |
| | | private static JTextField textField7; |
| | | private static JTextField textField8; |
| | | |
| | | |
| | | private static JButton saveParam_groupId; |
| | | private static JButton saveParam_anchorPower; |
| | | private static JButton saveParam_jiaoZhunDis; |
| | | private static JButton saveParam_label6; |
| | | private static JButton saveParam_label7; |
| | | private static JButton saveParam_label8; |
| | | |
| | | |
| | | // è·å readAncParam_jp 颿¿ï¼è¥ä¸ºç©ºåå建 |
| | | public static JPanel getReadAncParam_jp() { |
| | | if (readAncParam_jp == null) { |
| | | readAncParam_jp = new JPanel(); // åå§åè¯¥é¢æ¿ |
| | | readAncParam_jp.setBorder(new TitledBorder("读åä¿¡æ åæ°")); |
| | | } |
| | | return readAncParam_jp; |
| | | } |
| | | |
| | | |
| | | // è·å readParam æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getReadParam() { |
| | | if (readParam == null) { |
| | | readParam = new JButton("读ååæ°"); // åå§å该æé® |
| | | readParam.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | if (!ComMoudleComp.isChuankou()) { |
| | | ShowMessage.zidingyi("请å
æå¼ä¸²å£"); |
| | | return; |
| | | } |
| | | String chooseAnchorId = anchorListComp.getChooseAnchorId(); |
| | | if (chooseAnchorId != null) { |
| | | WriteReadAnchor.updateLora("0", (byte) 0x00, (byte) 0x02, chooseAnchorId, ComMoudleComp.getSerialPort(),(byte) 0x02); |
| | | ShowMessage.zidingyi(chooseAnchorId + ":读åæä»¤å·²åé"); |
| | | } else { |
| | | ShowMessage.zidingyi("请å¨ä¿¡æ å表ä¸éä¸éè¦è¯»åç设å¤"); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | |
| | | } |
| | | return readParam; |
| | | } |
| | | |
| | | // è·å saveParam æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getSaveParam_GroupID() { |
| | | if (saveParam_groupId == null) { |
| | | saveParam_groupId = new JButton("ä¿åé讯å°ç»"); // åå§å该æé® |
| | | saveParam_groupId.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | if (!ComMoudleComp.isChuankou()) { |
| | | ShowMessage.zidingyi("请å
æå¼ä¸²å£"); |
| | | return; |
| | | } |
| | | if (getAnchorId_jf().getText().length()!=4){ |
| | | ShowMessage.zidingyi("ä¿¡æ ID䏿£ç¡®"); |
| | | return; |
| | | } |
| | | String ancID = getAnchorId_jf().getText();//åºç«ID |
| | | String zuId = getGroupId_jf().getText();//é讯å°ç» |
| | | boolean groudID = JugeIn.shuru("é讯å°ç»", zuId); |
| | | if (groudID){ |
| | | WriteReadAnchor.updateLora(zuId, (byte) 0x0A, (byte) 0x01, ancID, ComMoudleComp.getSerialPort(),(byte) 0x02); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | return saveParam_groupId; |
| | | } |
| | | |
| | | // è·å saveParam æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getSaveParam_anchorPower() { |
| | | if (saveParam_anchorPower == null) { |
| | | saveParam_anchorPower = new JButton("ä¿åä¿¡æ åç"); // åå§å该æé® |
| | | saveParam_anchorPower.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | if (!ComMoudleComp.isChuankou()) { |
| | | ShowMessage.zidingyi("请å
æå¼ä¸²å£"); |
| | | return; |
| | | } |
| | | if (getAnchorId_jf().getText().length()!=4){ |
| | | ShowMessage.zidingyi("ä¿¡æ ID䏿£ç¡®"); |
| | | return; |
| | | } |
| | | String ancID = getAnchorId_jf().getText();//åºç«ID |
| | | String power = getAnchorPower_jf().getText();//ä¿¡æ åç |
| | | boolean poweraAnc = JugeIn.shuru("ä¿¡æ åç", power); |
| | | if (poweraAnc){ |
| | | WriteReadAnchor.updateLora(power, (byte) 0x36, (byte) 0x01, ancID, ComMoudleComp.getSerialPort(),(byte) 0x02); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | return saveParam_anchorPower; |
| | | } |
| | | |
| | | // è·å saveParam æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getSaveParam_jiaoZhunDis() { |
| | | if (saveParam_jiaoZhunDis == null) { |
| | | saveParam_jiaoZhunDis = new JButton("ä¿åæ ¡åè·ç¦»"); // åå§å该æé® |
| | | saveParam_jiaoZhunDis.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | if (!ComMoudleComp.isChuankou()) { |
| | | ShowMessage.zidingyi("请å
æå¼ä¸²å£"); |
| | | return; |
| | | } |
| | | if (getAnchorId_jf().getText().length()!=4){ |
| | | ShowMessage.zidingyi("ä¿¡æ ID䏿£ç¡®"); |
| | | return; |
| | | } |
| | | String ancID = getAnchorId_jf().getText();//åºç«ID |
| | | String dis = getJiaoZhunDis_jf().getText();//æ ¡åè·ç¦» |
| | | boolean distance = JugeIn.shuru("æ ¡åè·ç¦»", dis); |
| | | if (distance){ |
| | | WriteReadAnchor.updateLora(dis, (byte) 0x0C, (byte) 0x01, ancID, ComMoudleComp.getSerialPort(),(byte) 0x02); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | return saveParam_jiaoZhunDis; |
| | | } |
| | | |
| | | |
| | | // è·å saveParam æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getSaveParam_label6() { |
| | | if (saveParam_label6 == null) { |
| | | saveParam_label6 = new JButton("ä¿ååæ°"); // åå§å该æé® |
| | | saveParam_label6.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | if (!ComMoudleComp.isChuankou()) { |
| | | ShowMessage.zidingyi("请å
æå¼ä¸²å£"); |
| | | return; |
| | | } |
| | | if (getAnchorId_jf().getText().length()!=4){ |
| | | ShowMessage.zidingyi("ä¿¡æ ID䏿£ç¡®"); |
| | | return; |
| | | } |
| | | String ancID = getAnchorId_jf().getText();//åºç«ID |
| | | } |
| | | }); |
| | | } |
| | | return saveParam_label6; |
| | | } |
| | | |
| | | |
| | | // è·å saveParam æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getSaveParam_label7() { |
| | | if (saveParam_label7 == null) { |
| | | saveParam_label7 = new JButton("ä¿ååæ°"); // åå§å该æé® |
| | | saveParam_label7.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | if (!ComMoudleComp.isChuankou()) { |
| | | ShowMessage.zidingyi("请å
æå¼ä¸²å£"); |
| | | return; |
| | | } |
| | | if (getAnchorId_jf().getText().length()!=4){ |
| | | ShowMessage.zidingyi("ä¿¡æ ID䏿£ç¡®"); |
| | | return; |
| | | } |
| | | String ancID = getAnchorId_jf().getText();//åºç«ID |
| | | } |
| | | }); |
| | | } |
| | | return saveParam_label7; |
| | | } |
| | | |
| | | |
| | | // è·å saveParam æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getSaveParam_label8() { |
| | | if (saveParam_label8 == null) { |
| | | saveParam_label8 = new JButton("ä¿ååæ°"); // åå§å该æé® |
| | | saveParam_label8.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | if (!ComMoudleComp.isChuankou()) { |
| | | ShowMessage.zidingyi("请å
æå¼ä¸²å£"); |
| | | return; |
| | | } |
| | | if (getAnchorId_jf().getText().length()!=4){ |
| | | ShowMessage.zidingyi("ä¿¡æ ID䏿£ç¡®"); |
| | | return; |
| | | } |
| | | String ancID = getAnchorId_jf().getText();//åºç«ID |
| | | } |
| | | }); |
| | | } |
| | | return saveParam_label8; |
| | | } |
| | | |
| | | |
| | | |
| | | // è·å label1 æ ç¾ï¼è¥ä¸ºç©ºåå建 |
| | | public static JLabel getAnchorId() { |
| | | if (anchorId == null) { |
| | | anchorId = new JLabel(" ä¿¡æ ID"); // åå§å该æ ç¾ |
| | | } |
| | | return anchorId; |
| | | } |
| | | |
| | | // è·å label2 æ ç¾ï¼è¥ä¸ºç©ºåå建 |
| | | public static JLabel getAnchorVersion() { |
| | | if (anchorVersion == null) { |
| | | anchorVersion = new JLabel(" ä¿¡æ çæ¬å·"); // åå§å该æ ç¾ |
| | | } |
| | | return anchorVersion; |
| | | } |
| | | |
| | | // è·å label3 æ ç¾ï¼è¥ä¸ºç©ºåå建 |
| | | public static JLabel getGroupId() { |
| | | if (groupId == null) { |
| | | groupId = new JLabel(" é讯å°ç»"); // åå§å该æ ç¾ |
| | | } |
| | | return groupId; |
| | | } |
| | | |
| | | // è·å label4 æ ç¾ï¼è¥ä¸ºç©ºåå建 |
| | | public static JLabel getAnchorPower() { |
| | | if (anchorPower == null) { |
| | | anchorPower = new JLabel(" ä¿¡æ åç"); // åå§å该æ ç¾ |
| | | } |
| | | return anchorPower; |
| | | } |
| | | |
| | | // è·å label5 æ ç¾ï¼è¥ä¸ºç©ºåå建 |
| | | public static JLabel getJiaoZhunDis() { |
| | | if (jiaoZhunDis == null) { |
| | | jiaoZhunDis = new JLabel(" æ ¡åè·ç¦»"); // åå§å该æ ç¾ |
| | | } |
| | | return jiaoZhunDis; |
| | | } |
| | | |
| | | // è·å label6 æ ç¾ï¼è¥ä¸ºç©ºåå建 |
| | | public static JLabel getLabel6() { |
| | | if (label6 == null) { |
| | | label6 = new JLabel(" ä¿çåæ°1"); // åå§å该æ ç¾ |
| | | } |
| | | return label6; |
| | | } |
| | | |
| | | // è·å label7 æ ç¾ï¼è¥ä¸ºç©ºåå建 |
| | | public static JLabel getLabel7() { |
| | | if (label7 == null) { |
| | | label7 = new JLabel(" ä¿çåæ°2"); // åå§å该æ ç¾ |
| | | } |
| | | return label7; |
| | | } |
| | | |
| | | // è·å label8 æ ç¾ï¼è¥ä¸ºç©ºåå建 |
| | | public static JLabel getLabel8() { |
| | | if (label8 == null) { |
| | | label8 = new JLabel(" ä¿çåæ°3"); // åå§å该æ ç¾ |
| | | } |
| | | return label8; |
| | | } |
| | | |
| | | |
| | | // è·å textField1 ææ¬æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static JTextField getAnchorId_jf() { |
| | | if (anchorId_jf == null) { |
| | | anchorId_jf = new JTextField(); // åå§åè¯¥ææ¬æ¡ |
| | | anchorId_jf.setEnabled(false); |
| | | } |
| | | return anchorId_jf; |
| | | } |
| | | |
| | | // è·å textField2 ææ¬æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static JTextField getAnchorVersion_jf() { |
| | | if (anchorVersion_jf == null) { |
| | | anchorVersion_jf = new JTextField(); // åå§åè¯¥ææ¬æ¡ |
| | | anchorVersion_jf.setEnabled(false); |
| | | } |
| | | return anchorVersion_jf; |
| | | } |
| | | |
| | | // è·å textField3 ææ¬æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static JTextField getGroupId_jf() { |
| | | if (groupId_jf == null) { |
| | | groupId_jf = new JTextField(); // åå§åè¯¥ææ¬æ¡ |
| | | } |
| | | return groupId_jf; |
| | | } |
| | | |
| | | // è·å textField4 ææ¬æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static JTextField getAnchorPower_jf() { |
| | | if (anchorPower_jf == null) { |
| | | anchorPower_jf = new JTextField(); // åå§åè¯¥ææ¬æ¡ |
| | | } |
| | | return anchorPower_jf; |
| | | } |
| | | |
| | | // è·å textField5 ææ¬æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static JTextField getJiaoZhunDis_jf() { |
| | | if (jiaoZhunDis_jf == null) { |
| | | jiaoZhunDis_jf = new JTextField(); // åå§åè¯¥ææ¬æ¡ |
| | | } |
| | | return jiaoZhunDis_jf; |
| | | } |
| | | |
| | | // è·å textField6 ææ¬æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static JTextField getTextField6() { |
| | | if (textField6 == null) { |
| | | textField6 = new JTextField(); // åå§åè¯¥ææ¬æ¡ |
| | | } |
| | | return textField6; |
| | | } |
| | | |
| | | // è·å textField7 ææ¬æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static JTextField getTextField7() { |
| | | if (textField7 == null) { |
| | | textField7 = new JTextField(); // åå§åè¯¥ææ¬æ¡ |
| | | } |
| | | return textField7; |
| | | } |
| | | |
| | | // è·å textField8 ææ¬æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static JTextField getTextField8() { |
| | | if (textField8 == null) { |
| | | textField8 = new JTextField(); // åå§åè¯¥ææ¬æ¡ |
| | | } |
| | | return textField8; |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package index.JPanelMoudle; |
| | | |
| | | import baowen.Dell_Baowen; |
| | | import tools.GetNowTime; |
| | | import tools.Tools; |
| | | |
| | | import javax.swing.*; |
| | | import javax.swing.border.TitledBorder; |
| | | import java.awt.*; |
| | | import java.awt.event.MouseAdapter; |
| | | import java.awt.event.MouseEvent; |
| | | import java.nio.charset.Charset; |
| | | |
| | | public class showDataComp { |
| | | |
| | | private static JScrollPane dataShow;//è§£ææ°æ®æ»å¨é¢æ¿ |
| | | private static JTextArea textArea1; |
| | | private static JPopupMenu popupMenu;//å³é®èåå表 |
| | | static boolean time=true;//æ¾ç¤ºæ¶é´æ³ |
| | | static boolean ascll=false;//ascllæ¾ç¤º |
| | | |
| | | // è·å textArea1 ææ¬åºï¼è¥ä¸ºç©ºåå建 |
| | | public static JTextArea getTextArea1() { |
| | | if (textArea1 == null) { |
| | | textArea1 = new JTextArea(); // åå§åè¯¥ææ¬åº |
| | | textArea1.addMouseListener(new MouseAdapter() { |
| | | @Override |
| | | public void mousePressed(MouseEvent e) { |
| | | maybeShowPopup(e); |
| | | } |
| | | |
| | | @Override |
| | | public void mouseReleased(MouseEvent e) { |
| | | maybeShowPopup(e); |
| | | } |
| | | |
| | | // æ¾ç¤ºå³é®èå |
| | | private void maybeShowPopup(MouseEvent e) { |
| | | if (e.isPopupTrigger()) { // 夿æ¯å¦æ¯å³é®ç¹å» |
| | | getPopupMenu().show(e.getComponent(), e.getX(), e.getY()); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | return textArea1; |
| | | } |
| | | // è·å è§£ææ°æ®æ»å¨é¢æ¿ï¼è¥ä¸ºç©ºåå建 |
| | | public static JScrollPane getDataShow() { |
| | | if (dataShow == null) { |
| | | dataShow = new JScrollPane(); // åå§å该æ»å¨é¢æ¿ |
| | | dataShow.setBorder(new TitledBorder("æ¥æ¶æ°æ®è§£æ")); |
| | | } |
| | | return dataShow; |
| | | } |
| | | |
| | | public static JPopupMenu getPopupMenu(){ |
| | | if (popupMenu == null) { |
| | | popupMenu = new JPopupMenu(); |
| | | popupMenu.setBackground(new Color(245, 245, 245)); // 设置èåèæ¯è² |
| | | popupMenu.setFont(new Font("Segoe UI", Font.PLAIN, 14)); // 设置åä½ |
| | | |
| | | // å¨æè®¡ç®èåé¡¹çææ¬ |
| | | String showAscll = ascll ? "HEXæ¾ç¤º" : "ASCLLæ¾ç¤º"; |
| | | String noShowtime = time ? "éèæ¶é´æ³" : "æ¾ç¤ºæ¶é´æ³"; |
| | | |
| | | // å建èå项 |
| | | JMenuItem clear = new JMenuItem("æ¸
空çªå£"); |
| | | JMenuItem ascllShow = new JMenuItem(showAscll); |
| | | JMenuItem timeNoShow = new JMenuItem(noShowtime); |
| | | |
| | | // æ·»å å¨ä½çå¬å¨ |
| | | clear.addActionListener(e -> textArea1.setText("")); |
| | | ascllShow.addActionListener(e -> setAscll()); |
| | | timeNoShow.addActionListener(e -> setTimeShow()); |
| | | |
| | | // é¼ æ æ¬åæ¶æ¹åèæ¯é¢è² |
| | | clear.addMouseListener(createMouseAdapter(clear)); |
| | | ascllShow.addMouseListener(createMouseAdapter(ascllShow)); |
| | | timeNoShow.addMouseListener(createMouseAdapter(timeNoShow)); |
| | | |
| | | // æ·»å èå项å°èå |
| | | popupMenu.add(clear); |
| | | popupMenu.addSeparator(); |
| | | popupMenu.add(ascllShow); |
| | | popupMenu.addSeparator(); |
| | | popupMenu.add(timeNoShow); |
| | | } else { |
| | | // 妿popupMenuå·²ç»åå»ºï¼æ´æ°èå项 |
| | | // å¨ææ´æ°èåé¡¹ææ¬ |
| | | ((JMenuItem) popupMenu.getComponent(2)).setText(ascll ? "HEXæ¾ç¤º" : "ASCLLæ¾ç¤º"); |
| | | ((JMenuItem) popupMenu.getComponent(4)).setText(time ? "éèæ¶é´æ³" : "æ¾ç¤ºæ¶é´æ³"); |
| | | } |
| | | |
| | | return popupMenu; |
| | | } |
| | | |
| | | // é¼ æ æ¬åæ¶æ¹åèæ¯é¢è²çéç¨æ¹æ³ |
| | | private static MouseAdapter createMouseAdapter(JMenuItem item) { |
| | | return new MouseAdapter() { |
| | | @Override |
| | | public void mouseEntered(MouseEvent e) { |
| | | item.setBackground(new Color(220, 220, 220)); // 设置æ¬åæ¶çé¢è² |
| | | } |
| | | |
| | | @Override |
| | | public void mouseExited(MouseEvent e) { |
| | | item.setBackground(new Color(240, 240, 240)); // æ¢å¤æ£å¸¸é¢è² |
| | | } |
| | | }; |
| | | } |
| | | |
| | | |
| | | public static void addAreaTextMes(byte[] Datas,String ip,int port) { |
| | | String message = ascll ? new String(Datas, Charset.forName("GBK")): Tools.Bytes2HexString(Datas); |
| | | if (time) { |
| | | getTextArea1().append(GetNowTime.timestamp2() + ":æ¶â" + message + "\n"); |
| | | }else { |
| | | getTextArea1().append( message + "\n"); |
| | | } |
| | | if (port!=9999) { |
| | | Dell_Baowen.intsert(ip, message, GetNowTime.timestamp2(), port); |
| | | } |
| | | // è·åææ¬åºåçé¿åº¦ï¼å³æåä¸è¡çç»æä½ç½®ï¼ |
| | | int length = textArea1.getDocument().getLength(); |
| | | // 设置å
æ ä½ç½®å°ææ¬çæ«å°¾ |
| | | textArea1.setCaretPosition(length); |
| | | } |
| | | |
| | | public static void addSendAreaTextMes(byte[] Datas){ |
| | | String message = ascll ? new String(Datas, Charset.forName("GBK")): Tools.Bytes2HexString(Datas); |
| | | if (time) { |
| | | getTextArea1().append(GetNowTime.timestamp2()+":åâ"+message+"\n"); |
| | | }else { |
| | | getTextArea1().append( message + "\n"); |
| | | } |
| | | // è·åææ¬åºåçé¿åº¦ï¼å³æåä¸è¡çç»æä½ç½®ï¼ |
| | | int length = textArea1.getDocument().getLength(); |
| | | // 设置å
æ ä½ç½®å°ææ¬çæ«å°¾ |
| | | textArea1.setCaretPosition(length); |
| | | } |
| | | |
| | | public static void setTimeShow(){ |
| | | if (time){ |
| | | time=false; |
| | | }else { |
| | | time=true; |
| | | } |
| | | } |
| | | |
| | | public static void setAscll(){ |
| | | if (ascll){ |
| | | ascll=false; |
| | | }else { |
| | | ascll=true; |
| | | } |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package index.JPanelMoudle; |
| | | |
| | | import baowen.WriteReadAnchor; |
| | | import gnu.io.SerialPort; |
| | | import tools.ChuanKou.SerialPortUtil; |
| | | import tools.CustomProgressBar; |
| | | import tools.ShowMessage; |
| | | import ymodem.getFilesDataBytes; |
| | | import ymodem.YModemUpgradeThread; |
| | | |
| | | import javax.swing.*; |
| | | import javax.swing.border.TitledBorder; |
| | | |
| | | |
| | | import java.awt.*; |
| | | import java.awt.event.ActionEvent; |
| | | import java.awt.event.ActionListener; |
| | | import java.io.File; |
| | | |
| | | |
| | | public class versionUpgradeComp { |
| | | private static JPanel versionUpdate;//çæ¬åçº§é¢æ¿ |
| | | private static JLabel label9;//空æ ç¾ï¼è¿è¡å ä½ |
| | | private static JTextField readVersion_jf;//è¯»åæ«æå¨çæ¬ææ¬æ¡ |
| | | private static JTextField chooseVersion_jf;//éæ©åºä»¶ææ¬æ¡ |
| | | |
| | | private static JButton chooseVersion;//éæ©åºä»¶æé® |
| | | private static JButton write_saoMiao;//åå
¥æ«æå¨æé® |
| | | public static boolean write=false;//åå
¥æ«æå¨æé® |
| | | public static boolean xinBiaoUp=false;//å级信æ çæ¬æé® |
| | | private static JButton readVersion_jb;//读åçæ¬æé® |
| | | private static JButton updateAnchor_jb;//å级信æ çæ¬æé® |
| | | |
| | | private static CustomProgressBar jindu_SaoMiaoQi;//åå
¥æ«æå¨è¿åº¦æ¡ |
| | | private static CustomProgressBar updateAnchor_jf;//å级信æ çæ¬è¿åº¦æ¡ |
| | | |
| | | private static YModemUpgradeThread upgradeThread; // ä¿å线ç¨å¼ç¨ |
| | | private static Color color = new Color(6, 176, 37);; // ä¿å线ç¨å¼ç¨ |
| | | |
| | | // è·åçæ¬åçº§é¢æ¿ï¼è¥ä¸ºç©ºåå建 |
| | | public static JPanel getVersionUpdate() { |
| | | if (versionUpdate == null) { |
| | | versionUpdate = new JPanel(); // åå§åè¯¥é¢æ¿ |
| | | versionUpdate.setBorder(new TitledBorder("åºä»¶å级")); |
| | | } |
| | | return versionUpdate; |
| | | } |
| | | |
| | | // è·å空æ ç¾ï¼è¥ä¸ºç©ºåå建 |
| | | public static JLabel getLabel9() { |
| | | if (label9 == null) { |
| | | label9 = new JLabel(); // åå§å该æ ç¾ |
| | | } |
| | | return label9; |
| | | } |
| | | |
| | | |
| | | |
| | | // è·å éæ©åºä»¶ æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getChooseVersion() { |
| | | if (chooseVersion == null) { |
| | | chooseVersion = new JButton("éæ©åºä»¶"); // åå§å该æé® |
| | | chooseVersion.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | // æå¼æä»¶éæ©å¯¹è¯æ¡ |
| | | String currentDirectory = System.getProperty("user.dir"); |
| | | JFileChooser fileChooser = new JFileChooser(); |
| | | // 设置æä»¶éæ©å¯¹è¯æ¡ä¸ºå¯éæ©æä»¶ |
| | | fileChooser.setDialogTitle("éæ©åºä»¶"); |
| | | fileChooser.setCurrentDirectory(new File(currentDirectory)); |
| | | int result = fileChooser.showOpenDialog(readParamsComp.getReadAncParam_jp()); |
| | | if (result == JFileChooser.APPROVE_OPTION) { |
| | | // å¦æç¨æ·éæ©äºæä»¶ |
| | | File selectedFile = fileChooser.getSelectedFile(); |
| | | // æ¾ç¤ºéæ©çæä»¶è·¯å¾ |
| | | getChooseVersion_jf().setText(selectedFile.getPath());//æä»¶åç§°); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | return chooseVersion; |
| | | } |
| | | |
| | | // è·å åå
¥æ«æå¨ æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getWrite_saoMiao() { |
| | | if (write_saoMiao == null) { |
| | | write_saoMiao = new JButton("åå
¥æ«æå¨"); // åå§å该æé® |
| | | write_saoMiao.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | if(!write) { |
| | | String filePath = getChooseVersion_jf().getText(); |
| | | if (!ComMoudleComp.isChuankou()) { |
| | | ShowMessage.zidingyi("䏲壿ªè¯å«"); |
| | | return; |
| | | } |
| | | if (filePath == null || filePath.length() < 10) { |
| | | ShowMessage.zidingyi("åºä»¶ä¸æ£ç¡®"); |
| | | return; |
| | | } |
| | | SerialPort serialPort = ComMoudleComp.getSerialPort(); |
| | | write = true; |
| | | byte[] bytes = WriteReadAnchor.write_lora((byte) 0x10, (byte) 0x2, 1); |
| | | YModemUpgradeThread.shengji(serialPort,filePath,bytes); |
| | | write_saoMiao.setText("åæ¶åå
¥"); |
| | | jindu_SaoMiaoQi.setStringPainted(true); // æ¾ç¤ºç¾åæ¯æå |
| | | }else { |
| | | YModemUpgradeThread.setSendDataFlag(true); |
| | | exitUpgrade(); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | return write_saoMiao; |
| | | } |
| | | |
| | | |
| | | |
| | | // è·å è¯»åæ«æå¨çæ¬ æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getReadVersion_jb() { |
| | | if (readVersion_jb == null) { |
| | | readVersion_jb = new JButton("è¯»åæ«æå¨çæ¬"); // åå§å该æé® |
| | | readVersion_jb.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | |
| | | if (!ComMoudleComp.isChuankou()) { |
| | | ShowMessage.zidingyi("䏲壿ªè¯å«"); |
| | | return; |
| | | } |
| | | ShowMessage.zidingyi("è¯»åæ«æå¨çæ¬æä»¤åéæå"); |
| | | byte[] byt = WriteReadAnchor.ReadData((byte) 0x2, (byte) 0x50); |
| | | WriteReadAnchor.sendData(ComMoudleComp.getSerialPort(), byt); |
| | | } |
| | | |
| | | }); |
| | | } |
| | | return readVersion_jb; |
| | | } |
| | | |
| | | // è·å å级信æ çæ¬ æé®ï¼è¥ä¸ºç©ºåå建 |
| | | public static JButton getUpdateAnchor_jb() { |
| | | if (updateAnchor_jb == null) { |
| | | updateAnchor_jb = new JButton("å级信æ çæ¬"); // åå§å该æé® |
| | | updateAnchor_jb.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | if (!xinBiaoUp) { |
| | | |
| | | if (!ComMoudleComp.isChuankou()) { |
| | | ShowMessage.zidingyi("䏲壿ªè¯å«"); |
| | | return; |
| | | } |
| | | String text = readParamsComp.getAnchorId_jf().getText(); |
| | | if (text.length() != 4) { |
| | | ShowMessage.zidingyi("ä¿¡æ ID䏿£ç¡®,请å
读åä¿¡æ åæ°"); |
| | | return; |
| | | } |
| | | SerialPort serialPort = ComMoudleComp.getSerialPort(); |
| | | xinBiaoUp=true; |
| | | updateAnchor_jb.setText("éåºä¿¡æ å级"); |
| | | WriteReadAnchor.updateLora("0", (byte) 0xAA,(byte) 0x06, text,serialPort,(byte) 0x02); |
| | | }else { |
| | | exitUpgradeXinBiao(); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | return updateAnchor_jb; |
| | | } |
| | | |
| | | |
| | | // è·å è¯»åæ«æå¨çæ¬ ææ¬æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static JTextField getReadVersion_jf() { |
| | | if (readVersion_jf == null) { |
| | | readVersion_jf = new JTextField(); // åå§åè¯¥ææ¬æ¡ |
| | | } |
| | | return readVersion_jf; |
| | | } |
| | | |
| | | // è·å éæ©åºä»¶ ææ¬æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static JTextField getChooseVersion_jf() { |
| | | if (chooseVersion_jf == null) { |
| | | chooseVersion_jf = new JTextField(); // åå§åè¯¥ææ¬æ¡ |
| | | chooseVersion_jf.setEnabled(false); |
| | | } |
| | | return chooseVersion_jf; |
| | | } |
| | | |
| | | // è·å åå
¥æ«æå¨ è¿åº¦æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static CustomProgressBar getJindu_SaoMiaoQi() { |
| | | if (jindu_SaoMiaoQi == null) { |
| | | jindu_SaoMiaoQi = new CustomProgressBar(); // åå§å该è¿åº¦æ¡ |
| | | jindu_SaoMiaoQi.setForeground(color); // 设置è¿åº¦æ¡åæ¯è²ä¸ºç»¿è² |
| | | // èªå®ä¹UIæ¥ä¿®æ¹ææ¬é¢è² |
| | | } |
| | | return jindu_SaoMiaoQi; |
| | | } |
| | | |
| | | |
| | | // è·å å级信æ çæ¬ è¿åº¦æ¡ï¼è¥ä¸ºç©ºåå建 |
| | | public static CustomProgressBar getUpdateAnchor_jf() { |
| | | if (updateAnchor_jf == null) { |
| | | updateAnchor_jf = new CustomProgressBar(); // åå§å该è¿åº¦æ¡ |
| | | updateAnchor_jf.setForeground(color); // 设置è¿åº¦æ¡åæ¯è²ä¸ºç»¿è² |
| | | } |
| | | return updateAnchor_jf; |
| | | } |
| | | |
| | | |
| | | public static boolean isWrite() { |
| | | return write; |
| | | } |
| | | |
| | | public static void exitUpgrade() { |
| | | write=false; |
| | | write_saoMiao.setText("åå
¥æ«æå¨"); |
| | | jindu_SaoMiaoQi.setValue(0); // æ¾ç¤ºç¾åæ¯æå |
| | | } |
| | | |
| | | |
| | | public static void exitUpgradeXinBiao() { |
| | | xinBiaoUp=false; |
| | | updateAnchor_jb.setText("å级信æ çæ¬"); |
| | | updateAnchor_jf.setValue(0); // æ¾ç¤ºç¾åæ¯æå |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package index; |
| | | |
| | | import Anchor.anchor_Dell; |
| | | import index.JPanelMoudle.ComMoudleComp; |
| | | import index.JPanelMoudle.anchorListComp; |
| | | |
| | | import java.util.Timer; |
| | | import java.util.TimerTask; |
| | | |
| | | public class Timer100 { |
| | | |
| | | Timer timer; |
| | | int time=0; |
| | | |
| | | public void ding_shi_qi() { |
| | | Integer cacheTime = 100;//触å卿¶é´100æ¯«ç§æ§è¡ä¸æ¬¡ |
| | | timer = new Timer(); |
| | | timer.schedule(new TimerTask() { |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | time++; |
| | | if (time==10){ |
| | | if (ComMoudleComp.isChuankou()) { |
| | | anchorListComp.getRowData(anchor_Dell.getAnchors()); |
| | | }else { |
| | | anchorListComp.updateTable();//æ¸
空信æ å表 |
| | | } |
| | | time=0; |
| | | } |
| | | } catch (Exception e) { |
| | | // TODO èªå¨çæç catch å |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | }, 100, cacheTime); |
| | | |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package index; |
| | | |
| | | import Anchor.anchor_Dell; |
| | | import baowen.Dell_Baowen; |
| | | |
| | | import javax.swing.*; |
| | | |
| | | public class homeMain { |
| | | public static void main(String[] args) { |
| | | new Dell_Baowen().startThread();//å¯å¨æ¥æè§£æçº¿ç¨ |
| | | System.setProperty("sun.java2d.dpi", "96"); // 设置DPIå¼ä¸º96ï¼æ åDPIï¼ |
| | | GCUWBreadIndex.startFrame();//å¯å¨å¾å½¢åçé¢ |
| | | new Timer100().ding_shi_qi();//100毫ç§å®æ¶å¨ |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools; |
| | | /**èªå¨å
³éçå¯¹è¯æ¡*/ |
| | | |
| | | import javax.swing.*; |
| | | import java.util.Timer; |
| | | import java.util.TimerTask; |
| | | |
| | | public class AutoCloseDialog { |
| | | |
| | | |
| | | static ImageIcon icon = new ImageIcon("image/warn.png"); // è¯·æ¿æ¢ä¸ºä½ 徿 çè·¯å¾ |
| | | |
| | | /**3ç§éæ¶å¤±çå¯¹è¯æ¡*/ |
| | | public static JOptionPane getDialog(String message) { |
| | | JOptionPane op = new JOptionPane(message, |
| | | JOptionPane.INFORMATION_MESSAGE); |
| | | op.setIcon(icon); |
| | | final JDialog dialog = op.createDialog("æç¤ºä¿¡æ¯, 2ç§åèªå¨å
³éï¼"); |
| | | |
| | | // å建ä¸ä¸ªæ°è®¡æ¶å¨ |
| | | Timer timer = new Timer(); |
| | | |
| | | // 3ç§åæ§è¡è¯¥ä»»å¡ |
| | | timer.schedule(new TimerTask() { |
| | | public void run() { |
| | | dialog.setVisible(false); |
| | | dialog.dispose(); |
| | | } |
| | | }, 2000); |
| | | dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); |
| | | dialog.setAlwaysOnTop(true); |
| | | dialog.setModal(false); |
| | | dialog.setVisible(true); |
| | | dialog.setIconImage(icon.getImage()); |
| | | return op; |
| | | } |
| | | |
| | | |
| | | /**24hæ¶å¤±çå¯¹è¯æ¡*/ |
| | | public static JOptionPane getDialogno(String message) { |
| | | JOptionPane op = new JOptionPane(message,JOptionPane.INFORMATION_MESSAGE); |
| | | op.setIcon(icon); |
| | | final JDialog dialog = op.createDialog("æç¤ºä¿¡æ¯ï¼"); |
| | | |
| | | // å建ä¸ä¸ªæ°è®¡æ¶å¨ |
| | | Timer timer = new Timer(); |
| | | |
| | | // 24å°æ¶ååæ§è¡è¯¥ä»»å¡ |
| | | timer.schedule(new TimerTask() { |
| | | public void run() { |
| | | dialog.setVisible(false); |
| | | dialog.dispose(); |
| | | } |
| | | }, 86400000); |
| | | dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); |
| | | dialog.setAlwaysOnTop(true); |
| | | dialog.setModal(false); |
| | | dialog.setVisible(true); |
| | | dialog.setIconImage(icon.getImage()); |
| | | return op; |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools.ChuanKou; |
| | | |
| | | /** |
| | | * CRC16æ ¡éª |
| | | * |
| | | * @author yangle |
| | | */ |
| | | public class CRC16 { |
| | | |
| | | private static final char crctable[] = { 0x0000, 0x1021, 0x2042, 0x3063, |
| | | 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, |
| | | 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, |
| | | 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b, 0xa35a, |
| | | 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, 0x1401, |
| | | 0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, |
| | | 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672, 0x1611, 0x0630, |
| | | 0x76d7, 0x66f6, 0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, |
| | | 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, 0x48c4, 0x58e5, 0x6886, 0x78a7, |
| | | 0x0840, 0x1861, 0x2802, 0x3823, 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, |
| | | 0x8948, 0x9969, 0xa90a, 0xb92b, 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, |
| | | 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, |
| | | 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, |
| | | 0x2c22, 0x3c03, 0x0c60, 0x1c41, 0xedae, 0xfd8f, 0xcdec, 0xddcd, |
| | | 0xad2a, 0xbd0b, 0x8d68, 0x9d49, 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, |
| | | 0x3e13, 0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe, 0xdfdd, 0xcffc, |
| | | 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, 0x9188, 0x81a9, 0xb1ca, 0xa1eb, |
| | | 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3, |
| | | 0x5004, 0x4025, 0x7046, 0x6067, 0x83b9, 0x9398, 0xa3fb, 0xb3da, |
| | | 0xc33d, 0xd31c, 0xe37f, 0xf35e, 0x02b1, 0x1290, 0x22f3, 0x32d2, |
| | | 0x4235, 0x5214, 0x6277, 0x7256, 0xb5ea, 0xa5cb, 0x95a8, 0x8589, |
| | | 0xf56e, 0xe54f, 0xd52c, 0xc50d, 0x34e2, 0x24c3, 0x14a0, 0x0481, |
| | | 0x7466, 0x6447, 0x5424, 0x4405, 0xa7db, 0xb7fa, 0x8799, 0x97b8, |
| | | 0xe75f, 0xf77e, 0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, |
| | | 0x6657, 0x7676, 0x4615, 0x5634, 0xd94c, 0xc96d, 0xf90e, 0xe92f, |
| | | 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806, 0x6827, |
| | | 0x18c0, 0x08e1, 0x3882, 0x28a3, 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, |
| | | 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75, 0x5a54, 0x6a37, 0x7a16, |
| | | 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, |
| | | 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 0x7c26, 0x6c07, 0x5c64, 0x4c45, |
| | | 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, |
| | | 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, |
| | | 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 }; |
| | | |
| | | public static char calc(byte[] bytes) { |
| | | char crc = 0x0000; |
| | | for (byte b : bytes) { |
| | | crc = (char) ((crc << 8) ^ crctable[((crc >> 8) ^ b) & 0x00ff]); |
| | | } |
| | | return (char) (crc); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools.ChuanKou; |
| | | |
| | | |
| | | import gnu.io.*; |
| | | import tools.ShowMessage; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Enumeration; |
| | | import java.util.List; |
| | | public class SerialPortManager { |
| | | static Enumeration<CommPortIdentifier> portList; |
| | | static CommPortIdentifier portId; |
| | | |
| | | |
| | | /** |
| | | * ååºæ¬å°è¿æ¥çææä¸²å£åç§°éå |
| | | * @param args |
| | | */ |
| | | @SuppressWarnings("unchecked")//åè¯ç¼è¯å¨å¿½ç¥ unchecked è¦åä¿¡æ¯ï¼å¦ä½¿ç¨Listï¼ArrayListçæªè¿è¡åæ°å产ççè¦åä¿¡æ¯ã |
| | | public static List<String> get_all_port(){ |
| | | List<String> all_port=new ArrayList<String>(); |
| | | portList = CommPortIdentifier.getPortIdentifiers(); |
| | | while(portList.hasMoreElements()){ |
| | | portId = (CommPortIdentifier)portList.nextElement(); |
| | | if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){ |
| | | all_port.add(portId.getName()); |
| | | } |
| | | } |
| | | return all_port; |
| | | } |
| | | |
| | | /**æå¼ä¸²å£ |
| | | * @param chuankou 串å£å· |
| | | * @param botelv æ³¢ç¹ç*/ |
| | | public static SerialPort open_chuankou(String chuankou,int botelv) { |
| | | SerialPort serialPort=null; |
| | | try { |
| | | serialPort =SerialPortUtil.openSerialPort(chuankou,botelv); |
| | | } catch (NoSuchPortException e) { |
| | | // TODO èªå¨çæç catch å |
| | | ShowMessage.zidingyi(chuankou+"ä¸åå¨ï¼"); |
| | | } catch (PortInUseException e) { |
| | | // TODO èªå¨çæç catch å |
| | | ShowMessage.zidingyi(chuankou+"已被å ç¨ï¼"); |
| | | } catch (UnsupportedCommOperationException e) { |
| | | // TODO èªå¨çæç catch å |
| | | ShowMessage.zidingyi(chuankou+"䏿¯ææä½ï¼"); |
| | | } |
| | | return serialPort; |
| | | |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools.ChuanKou; |
| | | |
| | | import gnu.io.SerialPort; |
| | | |
| | | /** |
| | | * @name: SerialPortParameter |
| | | * @author: tuacy. |
| | | * @date: 2019/6/26. |
| | | * @version: 1.0 |
| | | * @Description: 串å£åæ° |
| | | */ |
| | | public final class SerialPortParameter { |
| | | |
| | | /** |
| | | * 串å£åç§°(COM0ãCOM1ãCOM2çç) |
| | | */ |
| | | private String serialPortName; |
| | | /** |
| | | * æ³¢ç¹ç |
| | | * é»è®¤ï¼115200 |
| | | */ |
| | | private int baudRate; |
| | | /** |
| | | * æ°æ®ä½ é»è®¤8ä½ |
| | | * å¯ä»¥è®¾ç½®çå¼ï¼SerialPort.DATABITS_5ãSerialPort.DATABITS_6ãSerialPort.DATABITS_7ãSerialPort.DATABITS_8 |
| | | * é»è®¤ï¼SerialPort.DATABITS_8 |
| | | */ |
| | | private int dataBits; |
| | | /** |
| | | * åæ¢ä½ |
| | | * å¯ä»¥è®¾ç½®çå¼ï¼SerialPort.STOPBITS_1ãSerialPort.STOPBITS_2ãSerialPort.STOPBITS_1_5 |
| | | * é»è®¤ï¼SerialPort.STOPBITS_1 |
| | | */ |
| | | private int stopBits; |
| | | /** |
| | | * æ ¡éªä½ |
| | | * å¯ä»¥è®¾ç½®çå¼ï¼SerialPort.PARITY_NONEãSerialPort.PARITY_ODDãSerialPort.PARITY_EVENãSerialPort.PARITY_MARKãSerialPort.PARITY_SPACE |
| | | * é»è®¤ï¼SerialPort.PARITY_NONE |
| | | */ |
| | | private int parity; |
| | | |
| | | public SerialPortParameter(String serialPortName) { |
| | | this.serialPortName = serialPortName; |
| | | this.baudRate = 115200; |
| | | this.dataBits = SerialPort.DATABITS_8; |
| | | this.stopBits = SerialPort.STOPBITS_1; |
| | | this.parity = SerialPort.PARITY_NONE; |
| | | } |
| | | |
| | | public SerialPortParameter(String serialPortName, int baudRate) { |
| | | this.serialPortName = serialPortName; |
| | | this.baudRate = baudRate; |
| | | this.dataBits = SerialPort.DATABITS_8; |
| | | this.stopBits = SerialPort.STOPBITS_1; |
| | | this.parity = SerialPort.PARITY_NONE; |
| | | } |
| | | |
| | | public String getSerialPortName() { |
| | | return serialPortName; |
| | | } |
| | | |
| | | public void setSerialPortName(String serialPortName) { |
| | | this.serialPortName = serialPortName; |
| | | } |
| | | |
| | | public int getBaudRate() { |
| | | return baudRate; |
| | | } |
| | | |
| | | public void setBaudRate(int baudRate) { |
| | | this.baudRate = baudRate; |
| | | } |
| | | |
| | | public int getDataBits() { |
| | | return dataBits; |
| | | } |
| | | |
| | | public void setDataBits(int dataBits) { |
| | | this.dataBits = dataBits; |
| | | } |
| | | |
| | | public int getStopBits() { |
| | | return stopBits; |
| | | } |
| | | |
| | | public void setStopBits(int stopBits) { |
| | | this.stopBits = stopBits; |
| | | } |
| | | |
| | | public int getParity() { |
| | | return parity; |
| | | } |
| | | |
| | | public void setParity(int parity) { |
| | | this.parity = parity; |
| | | } |
| | | } |
| | | |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools.ChuanKou; |
| | | |
| | | import gnu.io.*; |
| | | import index.JPanelMoudle.ComMoudleComp; |
| | | import tools.ShowMessage; |
| | | import tools.Tools; |
| | | |
| | | |
| | | import javax.tools.Tool; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.Enumeration; |
| | | import java.util.List; |
| | | import java.util.TooManyListenersException; |
| | | |
| | | /** |
| | | * @name: SerialPortUtil |
| | | * @author: tuacy. |
| | | * @date: 2019/6/26. |
| | | * @version: 1.0 |
| | | * @Description: 串å£å·¥å
·ç±» |
| | | */ |
| | | @SuppressWarnings("unused") |
| | | public class SerialPortUtil { |
| | | |
| | | /** |
| | | * è·å¾ç³»ç»å¯ç¨ç端å£åç§°å表(COM0ãCOM1ãCOM2çç) |
| | | * |
| | | * @return List<String>å¯ç¨ç«¯å£åç§°å表 |
| | | */ |
| | | |
| | | // è¾å
¥æµ |
| | | private static InputStream inputStream; |
| | | // è¾åºæµ |
| | | private static OutputStream outputStream; |
| | | private static String data; |
| | | // ä¿å串å£è¿åä¿¡æ¯åå
è¿å¶ |
| | | |
| | | private static String dataHex; |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | public static List<String> getSerialPortList() { |
| | | List<String> systemPorts = new ArrayList<>(); |
| | | //è·å¾ç³»ç»å¯ç¨çç«¯å£ |
| | | Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers(); |
| | | while (portList.hasMoreElements()) { |
| | | String portName = portList.nextElement().getName();//è·å¾ç«¯å£çåå |
| | | systemPorts.add(portName); |
| | | } |
| | | return systemPorts; |
| | | } |
| | | |
| | | /** |
| | | * æå¼ä¸²å£ |
| | | * |
| | | * @param serialPortName 串å£åç§° |
| | | * @return SerialPort 串å£å¯¹è±¡ |
| | | * @throws NoSuchPortException 对åºä¸²å£ä¸åå¨ |
| | | * @throws PortInUseException 串å£å¨ä½¿ç¨ä¸ |
| | | * @throws UnsupportedCommOperationException 䏿¯ææä½æä½ |
| | | */ |
| | | public static SerialPort openSerialPort(String serialPortName) |
| | | throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException { |
| | | SerialPortParameter parameter = new SerialPortParameter(serialPortName); |
| | | return openSerialPort(parameter); |
| | | } |
| | | |
| | | /** |
| | | * æå¼ä¸²å£ |
| | | * |
| | | * @param serialPortName 串å£åç§° |
| | | * @param baudRate æ³¢ç¹ç |
| | | * @return SerialPort 串å£å¯¹è±¡ |
| | | * @throws NoSuchPortException 对åºä¸²å£ä¸åå¨ |
| | | * @throws PortInUseException 串å£å¨ä½¿ç¨ä¸ |
| | | * @throws UnsupportedCommOperationException 䏿¯ææä½æä½ |
| | | */ |
| | | public static SerialPort openSerialPort(String serialPortName, int baudRate) |
| | | throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException { |
| | | SerialPortParameter parameter =new SerialPortParameter(serialPortName, baudRate); |
| | | return openSerialPort(parameter); |
| | | } |
| | | |
| | | /** |
| | | * æå¼ä¸²å£ |
| | | * |
| | | * @param serialPortName 串å£åç§° |
| | | * @param baudRate æ³¢ç¹ç |
| | | * @param timeout 䏲壿å¼è¶
æ¶æ¶é´ |
| | | * @return SerialPort 串å£å¯¹è±¡ |
| | | * @throws NoSuchPortException 对åºä¸²å£ä¸åå¨ |
| | | * @throws PortInUseException 串å£å¨ä½¿ç¨ä¸ |
| | | * @throws UnsupportedCommOperationException 䏿¯ææä½æä½ |
| | | */ |
| | | public static SerialPort openSerialPort(String serialPortName, int baudRate, int timeout) |
| | | throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException { |
| | | SerialPortParameter parameter = new SerialPortParameter(serialPortName, baudRate); |
| | | return openSerialPort(parameter, timeout); |
| | | } |
| | | |
| | | /** |
| | | * æå¼ä¸²å£ |
| | | * |
| | | * @param parameter 串å£åæ° |
| | | * @return SerialPort 串å£å¯¹è±¡ |
| | | * @throws NoSuchPortException 对åºä¸²å£ä¸åå¨ |
| | | * @throws PortInUseException 串å£å¨ä½¿ç¨ä¸ |
| | | * @throws UnsupportedCommOperationException 䏿¯ææä½æä½ |
| | | */ |
| | | public static SerialPort openSerialPort(SerialPortParameter parameter) |
| | | throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException { |
| | | return openSerialPort(parameter, 2000); |
| | | } |
| | | |
| | | /** |
| | | * æå¼ä¸²å£ |
| | | * |
| | | * @param parameter 串å£åæ° |
| | | * @param timeout 䏲壿å¼è¶
æ¶æ¶é´ |
| | | * @return SerialPort串å£å¯¹è±¡ |
| | | * @throws NoSuchPortException 对åºä¸²å£ä¸åå¨ |
| | | * @throws PortInUseException 串å£å¨ä½¿ç¨ä¸ |
| | | * @throws UnsupportedCommOperationException 䏿¯ææä½æä½ |
| | | */ |
| | | public static SerialPort openSerialPort(SerialPortParameter parameter, int timeout) |
| | | throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException { |
| | | //éè¿ç«¯å£åç§°å¾å°ç«¯å£ |
| | | CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(parameter.getSerialPortName()); |
| | | //æå¼ç«¯å£ï¼ï¼èªå®ä¹ååï¼æå¼è¶
æ¶æ¶é´ï¼ |
| | | CommPort commPort = portIdentifier.open(parameter.getSerialPortName(), timeout); |
| | | //夿æ¯ä¸æ¯ä¸²å£ |
| | | if (commPort instanceof SerialPort) { |
| | | SerialPort serialPort = (SerialPort) commPort; |
| | | //设置串å£åæ°ï¼æ³¢ç¹çï¼æ°æ®ä½8ï¼åæ¢ä½1ï¼æ ¡éªä½æ ï¼ |
| | | serialPort.setSerialPortParams(parameter.getBaudRate(), parameter.getDataBits(), parameter.getStopBits(), parameter.getParity()); |
| | | ShowMessage.zidingyi(parameter.getSerialPortName()+"æå¼æåï¼"); |
| | | //System.out.println("å¼å¯ä¸²å£æåï¼ä¸²å£åç§°ï¼" + parameter.getSerialPortName()); |
| | | return serialPort; |
| | | } else { |
| | | //æ¯å
¶ä»ç±»åçç«¯å£ |
| | | throw new NoSuchPortException(); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * @throws Exception |
| | | * å
³éä¸²å£ |
| | | * @author LinWenLi |
| | | * @date 2018å¹´7æ21æ¥ä¸å3:45:43 |
| | | * @Description: å
³éä¸²å£ |
| | | * @param: |
| | | * @return: void |
| | | * @throws |
| | | */ |
| | | public static void closeSerialPort(SerialPort serialPort) throws Exception { |
| | | if (serialPort != null) { |
| | | serialPort.notifyOnDataAvailable(false); |
| | | serialPort.removeEventListener(); |
| | | if (inputStream != null) { |
| | | try { |
| | | inputStream.close(); |
| | | inputStream = null; |
| | | } catch (IOException e) { |
| | | ShowMessage.zidingyi("å
³éè¾å
¥æµæ¶åçIOå¼å¸¸"); |
| | | } |
| | | } |
| | | if (outputStream != null) { |
| | | try { |
| | | outputStream.close(); |
| | | outputStream = null; |
| | | } catch (IOException e) { |
| | | ShowMessage.zidingyi("å
³éè¾å
¥æµæ¶åçIOå¼å¸¸"); |
| | | } |
| | | } |
| | | serialPort.close(); |
| | | serialPort = null; |
| | | ShowMessage.zidingyi("串å£å
³éæå"); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * åéä¿¡æ¯å°ä¸²å£ |
| | | * @author LinWenLi |
| | | * @date 2018å¹´7æ21æ¥ä¸å3:45:22 |
| | | * @param: serialPort 串å£å¯¹è±¡ |
| | | * @return: data åéçæ°æ® |
| | | * @throws |
| | | */ |
| | | public static void sendData(SerialPort serialPort, byte[] data) { |
| | | try { |
| | | outputStream = serialPort.getOutputStream(); |
| | | outputStream.write(data); |
| | | outputStream.flush(); |
| | | } catch (NullPointerException e) { |
| | | try { |
| | | throw new Exception("æ¾ä¸å°ä¸²å£"); |
| | | } catch (Exception e1) { |
| | | // TODO èªå¨çæç catch å |
| | | e1.printStackTrace(); |
| | | } |
| | | } catch (IOException e) { |
| | | try { |
| | | throw new Exception("åéä¿¡æ¯å°ä¸²å£æ¶åçIOå¼å¸¸"); |
| | | } catch (Exception e1) { |
| | | // TODO èªå¨çæç catch å |
| | | e1.printStackTrace(); |
| | | } |
| | | }finally { |
| | | try { |
| | | if (outputStream != null) { |
| | | outputStream.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 读å串å£è¿åä¿¡æ¯ |
| | | * @author LinWenLi |
| | | * @date 2018å¹´7æ21æ¥ä¸å3:43:04 |
| | | * @return: byte[] |
| | | */ |
| | | public static byte[] readData(SerialPort serialPort) { |
| | | byte[] bytes=null; |
| | | try { |
| | | Thread.sleep(10); |
| | | inputStream = serialPort.getInputStream(); |
| | | // éè¿è¾å
¥æµå¯¹è±¡çavailableæ¹æ³è·åæ°ç»åèé¿åº¦ |
| | | bytes = new byte[inputStream.available()]; |
| | | // ä»çº¿è·¯ä¸è¯»åæ°æ®æµ |
| | | int len = 0; |
| | | while ((len = inputStream.read(bytes)) != -1) {// ç´æ¥è·åå°çæ°æ® |
| | | data = new String(bytes, 0, len).trim();// 转为åå
è¿å¶æ°æ® |
| | | dataHex = Tools.Bytes2HexString(bytes); |
| | | inputStream.close(); |
| | | inputStream = null; |
| | | break; |
| | | } |
| | | } catch (IOException e) { |
| | | try { |
| | | SerialPortUtil.closeSerialPort(serialPort); |
| | | ShowMessage.zidingyi_24("串å£éä¿¡å¼å¸¸å·²å
³éï¼"); |
| | | SerialPortUtil.getSerialPortList().clear(); |
| | | System.exit(-1); |
| | | } catch (Exception e1) { |
| | | // TODO èªå¨çæç catch å |
| | | e1.printStackTrace(); |
| | | } |
| | | } catch (InterruptedException e) { |
| | | //throw new RuntimeException(e); |
| | | } |
| | | return bytes; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * ç»ä¸²å£è®¾ç½®çå¬ |
| | | * |
| | | * @param serialPort serialPort è¦è¯»åçä¸²å£ |
| | | * @param listener SerialPortEventListenerçå¬å¯¹è±¡ |
| | | * @throws TooManyListenersException çå¬å¯¹è±¡å¤ªå¤ |
| | | */ |
| | | public static void setListenerToSerialPort(SerialPort serialPort, SerialPortEventListener listener) throws TooManyListenersException { |
| | | //ç»ä¸²å£æ·»å äºä»¶çå¬ |
| | | |
| | | if(serialPort!=null) { |
| | | |
| | | serialPort.addEventListener(listener); |
| | | //ä¸²å£ææ°æ®çå¬ |
| | | serialPort.notifyOnDataAvailable(true); |
| | | //䏿äºä»¶çå¬ |
| | | serialPort.notifyOnBreakInterrupt(true); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | | |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools.ChuanKou; |
| | | |
| | | import java.io.*; |
| | | import java.nio.ByteBuffer; |
| | | import java.nio.ByteOrder; |
| | | |
| | | /** |
| | | * XModemåè®® |
| | | * |
| | | * @author |
| | | */ |
| | | @SuppressWarnings("all") |
| | | public class Xmodem { |
| | | |
| | | // å¼å§ |
| | | private final byte SOH = 0x01; |
| | | // ç»æ |
| | | private final byte EOT = 0x04; |
| | | // åºç |
| | | private final byte ACK = 0x06; |
| | | // éä¼ |
| | | private final byte NAK = 0x15; |
| | | // æ æ¡ä»¶ç»æ |
| | | private final byte CAN = 0x18; |
| | | |
| | | // 以128åèåçå½¢å¼ä¼ è¾æ°æ® |
| | | private final int SECTOR_SIZE = 128; |
| | | // æå¤§éè¯¯ï¼æ åºçï¼å
æ° |
| | | private final int MAX_ERRORS = 10; |
| | | |
| | | // è¾å
¥æµï¼ç¨äºè¯»å䏲壿°æ® |
| | | private InputStream inputStream; |
| | | // è¾åºæµï¼ç¨äºåé䏲壿°æ® |
| | | private OutputStream outputStream; |
| | | |
| | | public Xmodem(InputStream inputStream, OutputStream outputStream) { |
| | | this.inputStream = inputStream; |
| | | this.outputStream = outputStream; |
| | | } |
| | | |
| | | /** |
| | | * åéæ°æ® |
| | | * |
| | | * @param filePath |
| | | * æä»¶è·¯å¾ |
| | | */ |
| | | public void send(final String filePath) { |
| | | new Thread() { |
| | | public void run() { |
| | | try { |
| | | // é误å
æ° |
| | | int errorCount; |
| | | // å
åºå· |
| | | byte blockNumber = 0x01; |
| | | // æ ¡éªå |
| | | int checkSum; |
| | | // 读åå°ç¼å²åºçåèæ°é |
| | | int nbytes; |
| | | // åå§åæ°æ®ç¼å²åº |
| | | byte[] sector = new byte[SECTOR_SIZE]; |
| | | // 读åæä»¶åå§å |
| | | DataInputStream inputStream = new DataInputStream( |
| | | new FileInputStream(filePath)); |
| | | |
| | | while ((nbytes = inputStream.read(sector)) > 0) { |
| | | // 妿æåä¸å
æ°æ®å°äº128个åèï¼ä»¥0xffè¡¥é½ |
| | | if (nbytes < SECTOR_SIZE) { |
| | | for (int i = nbytes; i < SECTOR_SIZE; i++) { |
| | | sector[i] = (byte) 0xff; |
| | | } |
| | | } |
| | | |
| | | // åä¸å
æ°æ®æå¤åé10次 |
| | | errorCount = 0; |
| | | while (errorCount < MAX_ERRORS) { |
| | | // ç»å
|
| | | // æ§å¶å符 + å
åºå· + å
åºå·çåç + æ°æ®åºæ®µ + æ ¡éªå |
| | | putData(SOH); |
| | | putData(blockNumber); |
| | | putData(~blockNumber); |
| | | checkSum = CRC16.calc(sector) & 0x00ffff; |
| | | putChar(sector, (short) checkSum); |
| | | outputStream.flush(); |
| | | |
| | | // è·ååºçæ°æ® |
| | | byte data = getData(); |
| | | // 妿æ¶å°åºçæ°æ®åè·³åºå¾ªç¯ï¼åéä¸ä¸å
æ°æ® |
| | | // æªæ¶å°åºçï¼é误å
æ°+1ï¼ç»§ç»éå |
| | | if (data == ACK) { |
| | | break; |
| | | } else { |
| | | ++errorCount; |
| | | } |
| | | } |
| | | // å
åºå·èªå¢ |
| | | blockNumber = (byte) ((++blockNumber) % 256); |
| | | } |
| | | |
| | | // æææ°æ®åé宿åï¼åéç»ææ è¯ |
| | | boolean isAck = false; |
| | | while (!isAck) { |
| | | putData(EOT); |
| | | isAck = getData() == ACK; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | }; |
| | | }.start(); |
| | | } |
| | | |
| | | /** |
| | | * æ¥æ¶æ°æ® |
| | | * |
| | | * @param filePath |
| | | * æä»¶è·¯å¾ |
| | | * @return æ¯å¦æ¥æ¶å®æ |
| | | * @throws IOException |
| | | * å¼å¸¸ |
| | | */ |
| | | public boolean receive(String filePath) throws Exception { |
| | | // é误å
æ° |
| | | int errorCount = 0; |
| | | // å
åºå· |
| | | byte blocknumber = 0x01; |
| | | // æ°æ® |
| | | byte data; |
| | | // æ ¡éªå |
| | | int checkSum; |
| | | // åå§åæ°æ®ç¼å²åº |
| | | byte[] sector = new byte[SECTOR_SIZE]; |
| | | // åå
¥æä»¶åå§å |
| | | DataOutputStream outputStream = new DataOutputStream( |
| | | new FileOutputStream(filePath)); |
| | | |
| | | // åéå符Cï¼CRCæ¹å¼æ ¡éª |
| | | putData((byte) 0x43); |
| | | |
| | | while (true) { |
| | | if (errorCount > MAX_ERRORS) { |
| | | outputStream.close(); |
| | | return false; |
| | | } |
| | | |
| | | // è·ååºçæ°æ® |
| | | data = getData(); |
| | | if (data != EOT) { |
| | | try { |
| | | // å¤ææ¥æ¶å°çæ¯å¦æ¯å¼å§æ è¯ |
| | | if (data != SOH) { |
| | | errorCount++; |
| | | continue; |
| | | } |
| | | |
| | | // è·åå
åºå· |
| | | data = getData(); |
| | | // 夿å
åºå·æ¯å¦æ£ç¡® |
| | | if (data != blocknumber) { |
| | | errorCount++; |
| | | continue; |
| | | } |
| | | |
| | | // è·åå
åºå·çåç |
| | | byte _blocknumber = (byte) ~getData(); |
| | | // 夿å
åºå·çåç æ¯å¦æ£ç¡® |
| | | if (data != _blocknumber) { |
| | | errorCount++; |
| | | continue; |
| | | } |
| | | |
| | | // è·åæ°æ® |
| | | for (int i = 0; i < SECTOR_SIZE; i++) { |
| | | sector[i] = getData(); |
| | | } |
| | | |
| | | // è·åæ ¡éªå |
| | | checkSum = (getData() & 0xff) << 8; |
| | | checkSum |= (getData() & 0xff); |
| | | // å¤ææ ¡éªåæ¯å¦æ£ç¡® |
| | | int crc = CRC16.calc(sector); |
| | | if (crc != checkSum) { |
| | | errorCount++; |
| | | continue; |
| | | } |
| | | |
| | | // åéåºç |
| | | putData(ACK); |
| | | // å
åºå·èªå¢ |
| | | blocknumber++; |
| | | // å°æ°æ®åå
¥æ¬å° |
| | | outputStream.write(sector); |
| | | // é误å
æ°å½é¶ |
| | | errorCount = 0; |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | |
| | | } finally { |
| | | // 妿åºéåééä¼ æ è¯ |
| | | if (errorCount != 0) { |
| | | putData(NAK); |
| | | } |
| | | } |
| | | } else { |
| | | break; |
| | | } |
| | | } |
| | | |
| | | // å
³éè¾åºæµ |
| | | outputStream.close(); |
| | | // åéåºç |
| | | putData(ACK); |
| | | |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * è·åæ°æ® |
| | | * |
| | | * @return æ°æ® |
| | | * @throws IOException |
| | | * å¼å¸¸ |
| | | */ |
| | | private byte getData() throws IOException { |
| | | return (byte) inputStream.read(); |
| | | } |
| | | |
| | | /** |
| | | * åéæ°æ® |
| | | * |
| | | * @param data |
| | | * æ°æ® |
| | | * @throws IOException |
| | | * å¼å¸¸ |
| | | */ |
| | | private void putData(int data) throws IOException { |
| | | outputStream.write((byte) data); |
| | | } |
| | | |
| | | /** |
| | | * åéæ°æ® |
| | | * |
| | | * @param data |
| | | * æ°æ® |
| | | * @param checkSum |
| | | * æ ¡éªå |
| | | * @throws IOException |
| | | * å¼å¸¸ |
| | | */ |
| | | private void putChar(byte[] data, short checkSum) throws IOException { |
| | | ByteBuffer bb = ByteBuffer.allocate(data.length + 2).order( |
| | | ByteOrder.BIG_ENDIAN); |
| | | bb.put(data); |
| | | bb.putShort(checkSum); |
| | | outputStream.write(bb.array()); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools; |
| | | |
| | | import javax.swing.*; |
| | | import java.awt.*; |
| | | |
| | | public class CustomProgressBar extends JProgressBar { |
| | | private Color textColor = Color.BLACK; // é»è®¤ææ¬é¢è² |
| | | |
| | | public CustomProgressBar() { |
| | | super(); |
| | | setStringPainted(true); // å
许æ¾ç¤ºææ¬ |
| | | } |
| | | |
| | | public void setTextColor(Color textColor) { |
| | | this.textColor = textColor; |
| | | } |
| | | |
| | | @Override |
| | | protected void paintComponent(Graphics g) { |
| | | // å
è°ç¨ç¶ç±»çpaintComponentï¼ç»å¶è¿åº¦æ¡çèæ¯å忝 |
| | | super.paintComponent(g); |
| | | |
| | | if (isStringPainted()) { |
| | | // è·åè¿åº¦ç¾åæ¯çææ¬ |
| | | String text = String.format("%d%%", (int)(getPercentComplete() * 100)); |
| | | Graphics2D g2d = (Graphics2D) g; |
| | | |
| | | // è·åææ¬åºåçç©å½¢åºå |
| | | FontMetrics metrics = g2d.getFontMetrics(); |
| | | int x = (getWidth() - metrics.stringWidth(text)) / 2; |
| | | int y = (getHeight() + metrics.getAscent()) / 2; |
| | | |
| | | // æ¸
餿æ¬åºåï¼é¿å
éå½± |
| | | g2d.setColor(getBackground()); // 使ç¨èæ¯è²æ¥æ¸
é¤åºå |
| | | g2d.fillRect(x, y - metrics.getAscent(), metrics.stringWidth(text), metrics.getHeight()); |
| | | |
| | | // è®¾ç½®ææ¬é¢è²ååä½ |
| | | g2d.setColor(textColor); |
| | | g2d.setFont(getFont().deriveFont(Font.BOLD)); |
| | | |
| | | // ç»å¶ææ¬ |
| | | g2d.drawString(text, x, y); |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.TimeZone; |
| | | |
| | | public class GetNowTime { |
| | | |
| | | public static String now() {//è·åæ¶é´æ¹æ³ |
| | | String msg=""; |
| | | Date date = new Date(); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); |
| | | msg+="["+sdf.format(date)+"]"; |
| | | return msg; |
| | | |
| | | } |
| | | |
| | | /**è·åå¹´ææ¥æ¶åç§æ¯«ç§æ¶é´ 2020-05-30 12:33:45.23*/ |
| | | public static String timestamp2() { |
| | | String d=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(Calendar.getInstance().getTime()); |
| | | d="["+d+"]"; |
| | | return d; |
| | | } |
| | | |
| | | |
| | | public static int timestamp() { |
| | | |
| | | int time=0; |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
| | | sdf.setTimeZone(TimeZone.getTimeZone("UTC")); |
| | | String inputString =sss(); |
| | | Date date = null; |
| | | try { |
| | | date = sdf.parse("1970-01-01 " + inputString); |
| | | } catch (ParseException e) { |
| | | // TODO èªå¨çæç catch å |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | time=(int) date.getTime(); |
| | | return time; |
| | | } |
| | | |
| | | /**è·åæ¯«ç§æ¶é´æ³21:49:27.519*/ |
| | | public static String sss() {//è·åæ¶é´æ¹æ³ |
| | | String msg=""; |
| | | Date date = new Date(); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); |
| | | msg+=sdf.format(date); |
| | | return msg; |
| | | |
| | | } |
| | | |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools; |
| | | import java.math.BigInteger; |
| | | |
| | | public class Jiaoyan { |
| | | |
| | | /**è·åæ ¡éªç é¤å»å
头ç¸å åå*/ |
| | | public static byte[] check(byte[] byt) { |
| | | |
| | | //åèæ°ç»è½¬ä¸º16è¿å¶å符串/2åçé¿åº¦ |
| | | int size=BinaryToHexString2(byt).length()/2; |
| | | String[] hex=hex(BinaryToHexString2(byt)); |
| | | |
| | | //æ±å |
| | | int sum=0; |
| | | for(int i=0;i<size;i++) { |
| | | sum+=decodeHEX(hex[i]); |
| | | } |
| | | |
| | | //åå转为16è¿å¶å符串 |
| | | String hex16=Integer.toHexString(~sum); |
| | | |
| | | //å符串çé¿åº¦ |
| | | int lenth=hex16.length(); |
| | | |
| | | byte[] jiaoyan=hexStringToByteArray(hex16.substring(lenth-4, lenth)); |
| | | |
| | | return jiaoyan; |
| | | |
| | | } |
| | | |
| | | |
| | | /**è·åæ ¡éªç é¤å»å
头ç¸å åå*/ |
| | | public static String check2(String byt) { |
| | | |
| | | //åèæ°ç»è½¬ä¸º16è¿å¶å符串/2åçé¿åº¦ |
| | | int size=byt.length()/2; |
| | | String[] hex=hex(byt); |
| | | |
| | | //æ±å |
| | | int sum=0; |
| | | for(int i=2;i<size-2;i++) { |
| | | sum+=decodeHEX(hex[i]); |
| | | } |
| | | |
| | | //åå转为16è¿å¶å符串 |
| | | String hex16=Integer.toHexString(~sum); |
| | | |
| | | //å符串çé¿åº¦ |
| | | int lenth=hex16.length(); |
| | | |
| | | String jiaoyan=hex16.substring(lenth-4, lenth); |
| | | |
| | | return jiaoyan.toUpperCase(); |
| | | |
| | | } |
| | | |
| | | /**16è¿å¶è½¬ä¸º10è¿å¶*/ |
| | | public static int decodeHEX(String hexs){ |
| | | BigInteger bigint=new BigInteger(hexs, 16); |
| | | int numb=bigint.intValue(); |
| | | return numb; |
| | | |
| | | } |
| | | |
| | | /**å°16è¿å¶å符串转为hexå符串æ°ç»2个å符串ä¸ä¸ª*/ |
| | | public static String[] hex(String message) { |
| | | int size=message.length()/2; |
| | | String[] hex=new String[size]; |
| | | for(int i=0;i<size;i++) { |
| | | hex[i]=message.substring(i*2, 2+i*2); |
| | | } |
| | | |
| | | return hex; |
| | | } |
| | | |
| | | |
| | | /**å°åèæ°ç»è½¬æ¢ä¸º16è¿å¶å符串æ ç©ºæ ¼*/ |
| | | public static String BinaryToHexString2(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; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 16è¿å¶è¡¨ç¤ºçå符串转æ¢ä¸ºåèæ°ç» |
| | | * |
| | | * @param hexString 16è¿å¶è¡¨ç¤ºçå符串 |
| | | * @return byte[] åèæ°ç» |
| | | */ |
| | | public static byte[] hexStringToByteArray(String hexString) { |
| | | hexString = hexString.replaceAll(" ", ""); |
| | | int len = hexString.length(); |
| | | byte[] bytes = new byte[len / 2]; |
| | | for (int i = 0; i < len; i += 2) { |
| | | // 两ä½ä¸ç»ï¼è¡¨ç¤ºä¸ä¸ªåè,æè¿æ ·è¡¨ç¤ºç16è¿å¶å符串ï¼è¿åæä¸ä¸ªåè |
| | | bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character |
| | | .digit(hexString.charAt(i+1), 16)); |
| | | } |
| | | return bytes; |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools.Judge; |
| | | |
| | | |
| | | import tools.ShowMessage; |
| | | |
| | | |
| | | |
| | | public class JugeIn { |
| | | |
| | | /**夿è¾å
¥æ¯å¦åæ³ï¼å¦è¾å
¥ä¸ºkong*/ |
| | | public static boolean shuru(String name,String str) { |
| | | boolean inru=true; |
| | | |
| | | if(str.length()<1) { |
| | | ShowMessage.zidingyi(name+"ä¸è½ä¸ºç©º..."); |
| | | inru=false; |
| | | }else if(!JugeNumber.isDigit(str)) { |
| | | ShowMessage.zidingyi(name+"è¾å
¥å
容åªè½ä¸ºæ´æ°..."); |
| | | inru=false; |
| | | } |
| | | |
| | | return inru; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools.Judge; |
| | | |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | |
| | | public class JugeNumber { |
| | | |
| | | // 纯æ°å |
| | | private static String DIGIT_REGEX = "^-?\\d+$"; |
| | | |
| | | // 嫿æ°å |
| | | private static String CONTAIN_DIGIT_REGEX = ".*[0-9].*"; |
| | | |
| | | // çº¯åæ¯ |
| | | private static String LETTER_REGEX = "[a-zA-Z]+"; |
| | | |
| | | // å
å«åæ¯ |
| | | private static String CONTAIN_LETTER_REGEX = ".*[a-zA-z].*"; |
| | | |
| | | // çº¯ä¸æ |
| | | private static String CHINESE_REGEX = "[\u4e00-\u9fa5]"; |
| | | |
| | | // ä»
ä»
å
å«åæ¯åæ°å |
| | | private static String LETTER_DIGIT_REGEX = "^[a-z0-9A-Z]+$"; |
| | | |
| | | private static String CHINESE_LETTER_REGEX = "([\u4e00-\u9fa5]+|[a-zA-Z]+)"; |
| | | |
| | | private static String CHINESE_LETTER_DIGIT_REGEX = "^[a-z0-9A-Z\u4e00-\u9fa5]+$"; |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /*æ¹æ³äºï¼æ¨èï¼é度æå¿« |
| | | * 夿æ¯å¦ä¸ºæ´æ° |
| | | * @param str ä¼ å
¥çå符串 |
| | | * @return æ¯æ´æ°è¿åtrue,å¦åè¿åfalse |
| | | */ |
| | | public static boolean isInteger(String str) { |
| | | Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); |
| | | return pattern.matcher(str).matches(); |
| | | } |
| | | |
| | | /** 夿å符串æ¯å¦ä»
嫿æ°åå忝*/ |
| | | public static boolean isLetterDigit(String str) { |
| | | return str.matches(LETTER_DIGIT_REGEX); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * æ¯å¦ä¸ºæ±åï¼ä¸å
æ¬æ ç¹ç¬¦å· |
| | | * |
| | | * @param con |
| | | * @return true æ¯æ±å |
| | | */ |
| | | public static boolean isChinese(String con) { |
| | | Pattern pattern = Pattern.compile(CHINESE_REGEX); |
| | | for (int i = 0; i < con.length(); i = i + 1) { |
| | | if (!pattern.matcher( |
| | | String.valueOf(con.charAt(i))).find()) { |
| | | return false; |
| | | } |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * ç¨æ£å表达å¼å¤æåç¬¦ä¸²ä¸æ¯å¦ ä»
å
å«è±æåæ¯ãæ°ååæ±å |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isLetterDigitOrChinese(String str) { |
| | | return str.matches(CHINESE_LETTER_DIGIT_REGEX); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * å§åä¸å¯å
嫿±åååæ¯ï¼æ å
¶å®å符 |
| | | * |
| | | * @param passengerName |
| | | * @return |
| | | */ |
| | | public static boolean checkChineseLetter(String passengerName) { |
| | | Pattern pattern = Pattern.compile(CHINESE_LETTER_REGEX); |
| | | Matcher matcher = pattern.matcher(passengerName); |
| | | if (matcher.matches()) { |
| | | //ä¸å
å«ç¹æ®å符 |
| | | return true; |
| | | } else { |
| | | //å
å«äºç¹æ®å符 |
| | | return false; |
| | | } |
| | | } |
| | | /** |
| | | * 夿ä¸ä¸ªå符串æ¯å¦å
嫿 ç¹ç¬¦å·ï¼ä¸ææè
è±ææ ç¹ç¬¦å·ï¼ï¼true å
å«ã<br/> |
| | | * åçï¼å¯¹åå符串å䏿¬¡æ¸
æ´ï¼æ¸
æ´ææææ ç¹ç¬¦å·ã<br/> |
| | | * æ¤æ¶ï¼å¦æå
¥å ret å
嫿 ç¹ç¬¦å·ï¼é£ä¹æ¸
æ´ååå符串é¿åº¦ä¸åï¼è¿åtrueï¼å¦åï¼é¿åº¦ç¸çï¼è¿åfalseã<br/> |
| | | * |
| | | * @param ret |
| | | * @return true å
å«ä¸è±ææ ç¹ç¬¦å· |
| | | */ |
| | | public static boolean checkPunctuation(String ret) { |
| | | boolean b = false; |
| | | String tmp = ret; |
| | | // replaceAlléé¢çæ£åå¹é
å¯ä»¥æ¸
空å符串ä¸çä¸è±ææ ç¹ç¬¦å·ï¼åªä¿çæ°åãè±æå䏿ã |
| | | tmp = tmp.replaceAll("\\p{P}", ""); |
| | | if (ret.length() != tmp.length()) { |
| | | b = true; |
| | | } |
| | | return b; |
| | | } |
| | | |
| | | /**夿å符串æ¯å¦ä¸ºçº¯æ°å*/ |
| | | public static boolean isDigit(String ret) { |
| | | return ret.matches(DIGIT_REGEX); |
| | | } |
| | | |
| | | public static boolean isLetter(String ret) { |
| | | |
| | | return ret.matches(LETTER_REGEX); |
| | | } |
| | | |
| | | public static boolean hasDigit(String ret) { |
| | | return ret.matches(CONTAIN_DIGIT_REGEX); |
| | | } |
| | | |
| | | /**夿æ¯å¦å
å«åæ¯*/ |
| | | public static boolean hasLetter(String ret) { |
| | | return ret.matches(CONTAIN_LETTER_REGEX); |
| | | } |
| | | |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools; |
| | | |
| | | public class ShowMessage { |
| | | |
| | | |
| | | /**èªå®ä¹çå
容æç¤º*/ |
| | | public static void zidingyi(String zidingyi) { |
| | | AutoCloseDialog.getDialog(zidingyi); |
| | | } |
| | | |
| | | /**软件è¯ç¨æç»æ*/ |
| | | public static void zhuce_off() { |
| | | AutoCloseDialog.getDialogno("软件è¯ç¨æå·²ç»æï¼å¦éç»§ç»ä½¿ç¨è¯·ååäº¬åææºæ§å
¬å¸è´ä¹°æ³¨åç ï¼"); |
| | | } |
| | | |
| | | /**24å°æ¶æ¶å¤±çå¯¹è¯æ¡*/ |
| | | public static void zidingyi_24(String str) { |
| | | AutoCloseDialog.getDialogno(str); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package tools; |
| | | |
| | | import java.math.BigInteger; |
| | | |
| | | @SuppressWarnings("unused") |
| | | public class Tools { |
| | | |
| | | |
| | | |
| | | /** |
| | | * å°åå
è¿å¶çåç¬¦ä¸²è½¬æ¢æåèæ°ç» |
| | | * |
| | | * @param hexString |
| | | * @return |
| | | */ |
| | | public static byte[] hexStrToBinaryStr(String hexString) { |
| | | |
| | | |
| | | if (hexString==null) { |
| | | return null; |
| | | } |
| | | |
| | | hexString = hexString.replaceAll(" ", ""); |
| | | |
| | | int len = hexString.length(); |
| | | int index = 0; |
| | | |
| | | |
| | | byte[] bytes = new byte[len / 2]; |
| | | |
| | | while (index < len) { |
| | | |
| | | String sub = hexString.substring(index, index + 2); |
| | | bytes[index/2] = (byte)Integer.parseInt(sub,16); |
| | | index += 2; |
| | | } |
| | | |
| | | |
| | | return bytes; |
| | | } |
| | | |
| | | /** |
| | | * å°åå
è¿å¶çåç¬¦ä¸²è½¬æ¢æåèæ°ç»ä¸å»é¤ç©ºæ ¼ |
| | | * |
| | | * @param hexString |
| | | * @return |
| | | */ |
| | | public static byte[] hexStrToBinaryStr2(String hexString) { |
| | | |
| | | if (hexString==null) { |
| | | return null; |
| | | } |
| | | |
| | | int len = hexString.length(); |
| | | |
| | | int index = 0; |
| | | |
| | | |
| | | byte[] bytes = new byte[len / 2]; |
| | | |
| | | while (index < len) { |
| | | String sub = hexString.substring(index, index + 2); |
| | | bytes[index/2] = (byte)Integer.parseInt(sub,16); |
| | | index += 2; |
| | | } |
| | | |
| | | |
| | | return bytes; |
| | | } |
| | | |
| | | |
| | | |
| | | public static final int registersToInt(byte[] bytes) { |
| | | return ( |
| | | ((bytes[0] & 0xff) << 24) | |
| | | ((bytes[1] & 0xff) << 16) | |
| | | ((bytes[2] & 0xff) << 8) | |
| | | (bytes[3] & 0xff) |
| | | ); |
| | | }//registersToInt |
| | | |
| | | |
| | | // hexתASCII |
| | | public static String hexToAscii(String hex) { |
| | | StringBuilder ascii = new StringBuilder(); |
| | | // å»é¤ HEX å符串ä¸çç©ºæ ¼ |
| | | hex = hex.replaceAll(" ", ""); |
| | | |
| | | // æ¯ä¸¤ä¸ªå符为ä¸ç»ï¼è½¬æ¢ä¸ºå¯¹åºçåèå¹¶æ¼æ¥æ ASCII å符 |
| | | for (int i = 0; i < hex.length(); i += 2) { |
| | | // ç´æ¥ä½¿ç¨ charAt è·åæ¯ä¸¤ä¸ªå符çåå
è¿å¶æ°å |
| | | char hexChar1 = hex.charAt(i); |
| | | char hexChar2 = hex.charAt(i + 1); |
| | | |
| | | // ç»ååå
è¿å¶å符为ä¸ä¸ªæ°å |
| | | int decimalValue = (Character.digit(hexChar1, 16) << 4) + Character.digit(hexChar2, 16); |
| | | |
| | | // 转æ¢ä¸ºå¯¹åºç ASCII å符并添å å°ç»æä¸ |
| | | ascii.append((char) decimalValue); |
| | | } |
| | | |
| | | return ascii.toString(); // è¿åæ¼æ¥åç ASCII å符串 |
| | | } |
| | | |
| | | |
| | | |
| | | public static String byteToAscii(byte[] bytes) { |
| | | // å°åèæ°ç»è½¬æ¢ä¸º ASCII å符串 |
| | | String asciiString = new String(bytes, java.nio.charset.StandardCharsets.US_ASCII); |
| | | return asciiString; |
| | | } |
| | | |
| | | /**å°åèæ°ç»è½¬ä¸º16è¿å¶å符串*/ |
| | | public static String Bytes2HexString(byte[] b) { |
| | | String ret = ""; |
| | | for (int i = 0; i < b.length; i++) { |
| | | String hex = Integer.toHexString(b[i] & 0xFF); |
| | | if (hex.length() == 1) { |
| | | hex = '0' + hex; |
| | | } |
| | | ret += hex.toUpperCase(); |
| | | } |
| | | return ret; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | public static final float registersToFloat(byte[] bytes) { |
| | | return Float.intBitsToFloat(( |
| | | ((bytes[0] & 0xff) << 24) | |
| | | ((bytes[1] & 0xff) << 16) | |
| | | ((bytes[2] & 0xff) << 8) | |
| | | (bytes[3] & 0xff) |
| | | )); |
| | | }// |
| | | @SuppressWarnings("unused") |
| | | private static String intToHex(int n) { |
| | | StringBuffer s = new StringBuffer(); |
| | | String a; |
| | | char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; |
| | | while(n != 0){ |
| | | s = s.append(b[n%16]); |
| | | n = n/16; |
| | | } |
| | | a = s.reverse().toString(); |
| | | return a; |
| | | } |
| | | |
| | | //å°è¿åçæ°æ®è½¬å为10è¿å¶æ°æ® |
| | | public static int ormntoint(byte[] bytes){ |
| | | int aa=0; |
| | | aa=bytes[0]-48 +(bytes[1]-48)*10+(bytes[2]-48)*100+(bytes[3]-48)*1000; |
| | | return aa; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 16è¿å¶çå符串表示转æåèæ°ç» |
| | | * |
| | | * @param hexString 16è¿å¶æ ¼å¼çå符串 |
| | | * @return 转æ¢åçåèæ°ç» |
| | | **/ |
| | | public static byte[] toByteArray(String hexString) { |
| | | byte[] byteArray=null; |
| | | if(hexString !=null || hexString !="") { |
| | | hexString = hexString.replaceAll(" ", ""); |
| | | byteArray = new byte[hexString.length() / 2]; |
| | | int k = 0; |
| | | for (int i = 0; i < byteArray.length; i++) {//å 为æ¯16è¿å¶ï¼æå¤åªä¼å ç¨4ä½ï¼è½¬æ¢æåèéè¦ä¸¤ä¸ª16è¿å¶çå符ï¼é«ä½å¨å
|
| | | byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff); |
| | | byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff); |
| | | byteArray[i] = (byte) (high << 4 | low); |
| | | k += 2; |
| | | } |
| | | } |
| | | return byteArray; |
| | | } |
| | | |
| | | |
| | | public static byte[] intToRegisters(int v) { |
| | | byte[] registers = new byte[4]; |
| | | registers[0] = (byte) (0xff & (v >> 24)); |
| | | registers[1] = (byte) (0xff & (v >> 16)); |
| | | registers[2] = (byte) (0xff & (v >> 8)); |
| | | registers[3] = (byte) (0xff & v); |
| | | return registers; |
| | | } |
| | | |
| | | |
| | | |
| | | //转æåèæ°ç» |
| | | public static String[] hex(String message) { |
| | | int size=message.length()/2; |
| | | String[] hex=new String[size]; |
| | | for(int i=0;i<size;i++) { |
| | | hex[i]=message.substring(i*2, 2+i*2); |
| | | } |
| | | |
| | | return hex; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 16è¿å¶è½¬ä¸º10è¿å¶ |
| | | */ |
| | | public static int decodeHEX(String hexs) { |
| | | BigInteger bigint = new BigInteger(hexs, 16); |
| | | int numb = bigint.intValue(); |
| | | return numb; |
| | | } |
| | | |
| | | public static byte decodeHEXToInt8(String hexStr) { |
| | | BigInteger bigInt = new BigInteger(hexStr, 16); |
| | | // å°æ ç¬¦å·æ´æ°è½¬æ¢ä¸ºæç¬¦å·ç8使´æ° |
| | | byte result = bigInt.byteValue(); |
| | | return result; |
| | | } |
| | | } |
| | | |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package ymodem; |
| | | |
| | | import java.util.Arrays; |
| | | |
| | | /** |
| | | * Uses table for irreducible polynomial: 1 + x^2 + x^15 + x^16 |
| | | */ |
| | | |
| | | public class CRC16 { |
| | | private static int[] table = { |
| | | 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, |
| | | 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, |
| | | 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, |
| | | 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, |
| | | 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, |
| | | 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, |
| | | 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, |
| | | 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, |
| | | 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, |
| | | 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, |
| | | 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, |
| | | 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, |
| | | 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, |
| | | 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, |
| | | 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, |
| | | 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, |
| | | 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, |
| | | 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, |
| | | 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, |
| | | 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, |
| | | 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, |
| | | 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, |
| | | 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, |
| | | 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, |
| | | 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, |
| | | 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, |
| | | 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, |
| | | 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, |
| | | 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, |
| | | 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, |
| | | 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, |
| | | 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0, |
| | | }; |
| | | |
| | | public static int getCRCLength() { |
| | | return 2; |
| | | } |
| | | |
| | | public static long calcCRC(byte[] block) { |
| | | int crc = 0x0000; |
| | | for (byte b : block) { |
| | | crc = ((crc << 8) ^ table[((crc >> 8) ^ (0xff & b))]) & 0xFFFF; |
| | | } |
| | | |
| | | return crc; |
| | | } |
| | | |
| | | public static byte[] getCRC16(byte[] block) { |
| | | byte[] crcBytes = new byte[getCRCLength()]; |
| | | long crcValue = calcCRC(block); |
| | | for (int i = 0; i < getCRCLength(); i++) { |
| | | crcBytes[getCRCLength() - i - 1] = (byte) ((crcValue >> (8 * i)) & 0xFF); |
| | | } |
| | | return crcBytes; |
| | | } |
| | | |
| | | public static byte[] getCRC16(byte[] block,int start) { |
| | | byte[] crcBytes = new byte[getCRCLength()]; |
| | | long crcValue = calcCRC(Arrays.copyOfRange(block, start, block.length-2)); |
| | | for (int i = 0; i < getCRCLength(); i++) { |
| | | crcBytes[getCRCLength() - i - 1] = (byte) ((crcValue >> (8 * i)) & 0xFF); |
| | | } |
| | | return crcBytes; |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package ymodem; |
| | | |
| | | import gnu.io.SerialPort; |
| | | import index.JPanelMoudle.showDataComp; |
| | | import index.JPanelMoudle.versionUpgradeComp; |
| | | import tools.ChuanKou.SerialPortUtil; |
| | | import tools.ShowMessage; |
| | | import tools.Tools; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.IOException; |
| | | |
| | | public class YModemUpgradeThread extends Thread { |
| | | |
| | | private static byte[][] dataBytes; // æ°æ®å¸§ |
| | | private static byte[] startBytes; // èµ·å§å¸§ |
| | | private static byte[] endBytes ; // ç»æå¸§ |
| | | private static final long TIMEOUT = 30000; // è¶
è¿30ç§æªååºåéåºåçº§æ¨¡å¼ |
| | | private static SerialPort serialPort; |
| | | |
| | | static int numSend = 0; |
| | | static boolean SendDataFlag = false; |
| | | |
| | | |
| | | public static void shengji(SerialPort serialPort1, String filePath1,byte[] byt){ |
| | | SendDataFlag=false; |
| | | numSend=0; |
| | | serialPort = serialPort1; |
| | | SerialPortUtil.sendData(serialPort, byt); |
| | | ComSendUpdateFile(filePath1); |
| | | chushihua(); |
| | | } |
| | | |
| | | // æé 彿°ï¼æ¥æ¶ä¸²å£åæä»¶è·¯å¾ |
| | | public static void chushihua() { |
| | | if (versionUpgradeComp.isWrite()) { |
| | | if (serialPort == null) { |
| | | return; |
| | | } else { |
| | | new YModemUpgradeThread().startThread(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public void startThread() { |
| | | Thread t = new Thread(this); |
| | | t.start(); |
| | | } |
| | | |
| | | public void run() { |
| | | try { |
| | | // è°ç¨å级è¿ç¨ |
| | | upgtade(); |
| | | } catch (Exception e) { |
| | | Thread.currentThread().interrupt(); // æ¢å¤ä¸ææ å¿ |
| | | System.out.println("线ç¨è¢«ä¸æï¼æ£å¨éåºå级è¿ç¨..."); |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | //å级åºä»¶ |
| | | private void upgtade(){ |
| | | long startTime = System.currentTimeMillis(); |
| | | long l = System.currentTimeMillis() - startTime; |
| | | boolean b = l< TIMEOUT; |
| | | while (b&&!SendDataFlag) { |
| | | b=System.currentTimeMillis()-startTime<TIMEOUT; |
| | | // çå¾
ååº |
| | | byte[] response = SerialPortUtil.readData(serialPort); |
| | | // å¦æè®¾å¤æ²¡æååºï¼ååºä¸ºç©ºï¼,å认为æ¯è¶
æ¶ |
| | | if (response == null || response.length == 0) { |
| | | continue; // å¦ææ²¡æååºï¼ç»§ç»çå¾
|
| | | } |
| | | showDataComp.addAreaTextMes(response,"127.0.0.1",9999); |
| | | if (response[0] == ymodem.ACK) { |
| | | numSend++; |
| | | sendData(numSend); |
| | | } else if (response[0] == ymodem.NAK || response[0] == ymodem.C) { |
| | | // æ¶å°NAKæCAï¼éåºå级 |
| | | sendData(numSend); |
| | | }else if (response[0]==ymodem.CA){ |
| | | break; |
| | | } |
| | | } |
| | | if (!b){ |
| | | ShowMessage.zidingyi("æ°æ®æ¥æ¶è¶
æ¶,éåºå级模å¼"); |
| | | } |
| | | versionUpgradeComp.exitUpgrade(); |
| | | } |
| | | |
| | | |
| | | //åéæ°æ®å
|
| | | public static void sendData(int shuJuZhen) { |
| | | int length = dataBytes.length; |
| | | if (shuJuZhen == 0) { |
| | | SerialPortUtil.sendData(serialPort, startBytes); |
| | | getProgress(shuJuZhen, length); |
| | | } else if (shuJuZhen <= length && shuJuZhen > 0) { |
| | | SerialPortUtil.sendData(serialPort, dataBytes[shuJuZhen - 1]); |
| | | getProgress(shuJuZhen, length); |
| | | } else if (shuJuZhen == length + 1||shuJuZhen == length + 2) { |
| | | SerialPortUtil.sendData(serialPort,new byte[]{(byte) ymodem.EOT}); |
| | | getProgress(shuJuZhen, length); |
| | | } else if (shuJuZhen == length + 3) { |
| | | SerialPortUtil.sendData(serialPort,endBytes); |
| | | getProgress(shuJuZhen, length); |
| | | ShowMessage.zidingyi("åçº§å®æ"); |
| | | SendDataFlag=true; |
| | | numSend = 0; |
| | | } |
| | | } |
| | | |
| | | //计ç®å½åå
çè¿åº¦ |
| | | private static void getProgress(double shuJuZhen, int length) { |
| | | int i =(int) Math.round((shuJuZhen / (length +3)) * 100); |
| | | versionUpgradeComp.getJindu_SaoMiaoQi().setValue(i); |
| | | } |
| | | |
| | | //è·åè¦åéçå¸§æ°æ® |
| | | public static void ComSendUpdateFile(String filePath){ |
| | | try { |
| | | File file = new File(filePath); |
| | | if (!file.exists()) { //妿æä»¶ä¸åå¨ |
| | | ShowMessage.zidingyi("æä»¶ä¸åå¨" + filePath); //æåºæä»¶æ¾ä¸å°å¼å¸¸ |
| | | return; |
| | | } |
| | | getFilesDataBytes.setNum(0); |
| | | startBytes = getFilesDataBytes.getStartData(file);//è·åèµ·å§å¸§æ°æ®å
|
| | | dataBytes = getFilesDataBytes.getData(file);//è·åæ°æ®å¸§æ°æ®å
|
| | | endBytes = getFilesDataBytes.getEndData();//è·åç»æå¸§æ°æ®å
|
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | public static boolean isSendDataFlag() { |
| | | return SendDataFlag; |
| | | } |
| | | |
| | | public static void setSendDataFlag(boolean sendDataFlag) { |
| | | SendDataFlag = sendDataFlag; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package ymodem; |
| | | |
| | | |
| | | |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | |
| | | public class getFilesDataBytes { |
| | | public static final int SOH = 0x01; // 128åèæ°æ®å
|
| | | public static final int STX = 0x02; // 1024åèæ°æ®å
|
| | | private static int num = 0; // 帧åºå· |
| | | |
| | | // è·åæä»¶çèµ·å§æ°æ®å
|
| | | public static byte[] getStartData(File file) { |
| | | int index = 0; |
| | | byte[] dat = new byte[133]; |
| | | byte[] nameBytes = file.getName().getBytes(); |
| | | byte[] lengthBytes = String.valueOf(file.length()).getBytes(); |
| | | dat[index++] = SOH; |
| | | dat[index++] = (byte) num++; |
| | | dat[index++] = (byte) ~dat[1]; |
| | | // ç»åæä»¶åæ°æ® |
| | | for (int i = 0; i < nameBytes.length; i++) { |
| | | dat[i + index] = nameBytes[i]; |
| | | } |
| | | index += nameBytes.length; |
| | | dat[index++] = 0x00; |
| | | // ç»åæä»¶é¿åº¦æ°æ® |
| | | for (int i = 0; i < lengthBytes.length; i++) { |
| | | dat[i + index] = lengthBytes[i]; |
| | | } |
| | | index += lengthBytes.length; |
| | | dat[index++] = 0x00; |
| | | byte[] crcDat = CRC16.getCRC16(dat, 3); |
| | | dat[dat.length - 2] = crcDat[0]; |
| | | dat[dat.length - 1] = crcDat[1]; |
| | | return dat; |
| | | } |
| | | |
| | | // è·åç»æå¸§æ°æ® |
| | | public static byte[] getEndData() { |
| | | byte[] dat = new byte[133]; |
| | | dat[0] = SOH; |
| | | dat[1] = (byte) 0x00; |
| | | dat[2] = (byte) 0xFF; |
| | | byte[] crcDat = CRC16.getCRC16(dat, 3); |
| | | dat[dat.length - 2] = crcDat[0]; |
| | | dat[dat.length - 1] = crcDat[1]; |
| | | return dat; |
| | | } |
| | | |
| | | /** |
| | | * è·åæ°æ®å¸§æ°æ® |
| | | * |
| | | * @param file æä»¶ |
| | | * @return æ°æ®å¸§æ°ç» |
| | | * @throws IOException |
| | | */ |
| | | public static byte[][] getData(File file) throws IOException { |
| | | long len = file.length(); |
| | | int size = (int) Math.ceil(len / 1024d); |
| | | int size1 = (int) Math.ceil(len / 1024d); |
| | | double v = len % 1024d; |
| | | if (v==0){ |
| | | int i = size + 1; |
| | | size1=i; |
| | | } |
| | | //è·åæ°æ®å¸§ä¸ªæ° |
| | | byte[][] result = new byte[size][1029]; |
| | | FileInputStream fis = new FileInputStream(file); |
| | | for (int i = 0; i < size1-1 ; i++) { //çææ°æ®å¸§ |
| | | result[i][0] = STX; //1024åèæ°æ®å
头 |
| | | result[i][1] = (byte) (num++); //èªå¨å¡«å
帧åºå· |
| | | if (num > 255) { //帧åºå·æå¤§255 |
| | | num = 0; |
| | | } |
| | | result[i][2] = (byte) ~result[i][1]; //åå帧åºå· |
| | | fis.read(result[i], 3, 1024); |
| | | byte[] crc16 = CRC16.getCRC16(result[i], 3); //è·åcrc16æ ¡éª |
| | | result[i][result[i].length - 2] = crc16[0]; |
| | | result[i][result[i].length - 1] = crc16[1]; //å¡«å
crc16æ ¡éª |
| | | } |
| | | int endLen = (int) len % 1024; |
| | | //妿æåä¸å¸§ä¸æ¯æ´1024个åè |
| | | if (endLen > 0) { |
| | | //妿æåä¸å¸§é¿åº¦å°äºçäº128 |
| | | if (endLen <= 128) { |
| | | //æåä¸å¸§ä½¿ç¨128åèä¼ è¾ |
| | | result[size - 1] = new byte[133]; |
| | | result[size - 1][0] = SOH; //128åèæ°æ®å
头 |
| | | int n = fis.read(result[size - 1], 3, 128); //å¡«å
128个åèæ°æ® |
| | | for (int i = n + 3; i < 131; i++) { //å©ä½ä½å¡«å
0x1A |
| | | result[size - 1][i] = 0x1A; |
| | | } |
| | | } else { |
| | | //æåä¸å¸§ä½¿ç¨1024åèä¼ è¾ |
| | | result[size - 1][0] = STX; //1024åèæ°æ®å
头 |
| | | int n = fis.read(result[size - 1], 3, 1024); //å¡«å
1024ä¸ªæ°æ® |
| | | for (int i = n + 3; i < 1027; i++) { //å©ä½ä½å¡«å
0x1A |
| | | result[size - 1][i] = 0x1A; |
| | | } |
| | | } |
| | | result[size - 1][1] = (byte) (num++); //èªå¨å¡«å
帧åºå· |
| | | if (num > 255) { //帧åºå·æå¤§255 |
| | | num = 0; |
| | | } |
| | | result[size - 1][2] = (byte) ~result[size - 1][1]; //åå帧åºå· |
| | | byte[] crc16 = CRC16.getCRC16(result[size - 1], 3); //è·åcrc16æ ¡éª |
| | | result[size - 1][result[size - 1].length - 2] = crc16[0]; |
| | | result[size - 1][result[size - 1].length - 1] = crc16[1]; //å¡«å
crc16æ ¡éª |
| | | } |
| | | fis.close(); |
| | | return result; |
| | | } |
| | | |
| | | |
| | | |
| | | public static int getNum() { |
| | | return num; |
| | | } |
| | | |
| | | public static void setNum(int num) { |
| | | getFilesDataBytes.num = num; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package ymodem; |
| | | |
| | | import baowen.Dell_udpReceive; |
| | | import gnu.io.SerialPort; |
| | | import index.JPanelMoudle.showDataComp; |
| | | import index.JPanelMoudle.versionUpgradeComp; |
| | | import tools.ChuanKou.SerialPortUtil; |
| | | import tools.ShowMessage; |
| | | import tools.Tools; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.net.*; |
| | | |
| | | public class netWorkUpgradeThread extends Thread { |
| | | |
| | | |
| | | private static byte[][] dataBytes; // æ°æ®å¸§ |
| | | private static byte[] startBytes; // èµ·å§å¸§ |
| | | private static byte[] endBytes ; // ç»æå¸§ |
| | | |
| | | private static final long TIMEOUT = 600000; // è¶
è¿30ç§æªååºåéåºåçº§æ¨¡å¼ |
| | | static int numSend = 0; |
| | | static boolean SendDataFlag = false; |
| | | static String ip = "";//ç®æ 设å¤ipå°å |
| | | static int port;//ç®æ 设å¤ç«¯å£ |
| | | static DatagramSocket socket; |
| | | static int bytlenth=1024; |
| | | static DatagramPacket packet; |
| | | |
| | | |
| | | public static void shengji(String ip1,int port1, String filePath1,byte[] byt,int nowPort){ |
| | | SendDataFlag=false; |
| | | numSend=0; |
| | | ip = ip1; |
| | | port = port1; |
| | | Dell_udpReceive.sendData(byt,ip,port); |
| | | ComSendUpdateFile(filePath1); |
| | | Dell_udpReceive.stopThread();//æåè§£æçº¿ç¨ |
| | | chushihua(nowPort); |
| | | |
| | | new Dell_udpReceive(nowPort);//éæ°å¼å§è§£æçº¿ç¨ |
| | | } |
| | | |
| | | // æé 彿°ï¼æ¥æ¶ä¸²å£åæä»¶è·¯å¾ |
| | | public static void chushihua(int nowPort) { |
| | | if (versionUpgradeComp.isWrite()) { |
| | | if (socket==null){ |
| | | try { |
| | | socket = new DatagramSocket(nowPort);//å建socket |
| | | byte[] data = new byte[bytlenth]; // å建byteæ°ç» |
| | | packet= new DatagramPacket(data,bytlenth); |
| | | new netWorkUpgradeThread().startThread(); |
| | | } catch (SocketException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | }else { |
| | | new netWorkUpgradeThread().startThread(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public void startThread() { |
| | | Thread t = new Thread(this); |
| | | t.start(); |
| | | } |
| | | |
| | | public void run() { |
| | | try { |
| | | // è°ç¨å级è¿ç¨ |
| | | upgtade(); |
| | | stopThread(); |
| | | } catch (Exception e) { |
| | | Thread.currentThread().interrupt(); // æ¢å¤ä¸ææ å¿ |
| | | System.out.println("线ç¨è¢«ä¸æï¼æ£å¨éåºå级è¿ç¨..."); |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | //å级åºä»¶ |
| | | private void upgtade(){ |
| | | long startTime = System.currentTimeMillis(); |
| | | boolean b = System.currentTimeMillis() - startTime < TIMEOUT; |
| | | while (b&&!SendDataFlag) { |
| | | // çå¾
ååº |
| | | byte[] recDat = getRecDat(); |
| | | // å¦æè®¾å¤æ²¡æååºï¼ååºä¸ºç©ºï¼,å认为æ¯è¶
æ¶ |
| | | if (recDat == null || recDat.length == 0) { |
| | | continue; |
| | | } |
| | | |
| | | showDataComp.addAreaTextMes(recDat,ip,port); |
| | | if (recDat[0] == ymodem.ACK) { |
| | | numSend++; |
| | | sendData(numSend); |
| | | } else if (recDat[0] == ymodem.NAK || recDat[0] == ymodem.C) { |
| | | // æ¶å°NAKæCAï¼éåºå级 |
| | | sendData(numSend); |
| | | }else if (recDat[0]==ymodem.CA){ |
| | | break; |
| | | } |
| | | } |
| | | versionUpgradeComp.exitUpgrade(); |
| | | } |
| | | |
| | | |
| | | //åéæ°æ®å
|
| | | public static void sendData(int shuJuZhen) { |
| | | int length = dataBytes.length; |
| | | if (shuJuZhen == 0) { |
| | | sendData(startBytes,ip,port); |
| | | getProgress(shuJuZhen, length); |
| | | } else if (shuJuZhen <= length && shuJuZhen > 0) { |
| | | sendData(dataBytes[shuJuZhen - 1],ip,port); |
| | | getProgress(shuJuZhen, length); |
| | | } else if (shuJuZhen == length + 1||shuJuZhen == length + 2) { |
| | | sendData(new byte[]{(byte) ymodem.EOT},ip,port); |
| | | getProgress(shuJuZhen, length); |
| | | } else if (shuJuZhen == length + 3) { |
| | | sendData(endBytes,ip,port); |
| | | getProgress(shuJuZhen, length); |
| | | ShowMessage.zidingyi("åçº§å®æ"); |
| | | SendDataFlag=true; |
| | | numSend = 0; |
| | | } |
| | | } |
| | | |
| | | //计ç®å½åå
çè¿åº¦ |
| | | private static void getProgress(double shuJuZhen, int length) { |
| | | int i =(int) Math.round((shuJuZhen / (length +3)) * 100); |
| | | versionUpgradeComp.getJindu_SaoMiaoQi().setValue(i); |
| | | } |
| | | |
| | | //è·åè¦åéçå¸§æ°æ® |
| | | public static void ComSendUpdateFile(String filePath){ |
| | | try { |
| | | File file = new File(filePath); |
| | | if (!file.exists()) { //妿æä»¶ä¸åå¨ |
| | | ShowMessage.zidingyi("æä»¶ä¸åå¨" + filePath); //æåºæä»¶æ¾ä¸å°å¼å¸¸ |
| | | return; |
| | | } |
| | | getFilesDataBytes.setNum(0); |
| | | startBytes = getFilesDataBytes.getStartData(file);//è·åèµ·å§å¸§æ°æ®å
|
| | | dataBytes = getFilesDataBytes.getData(file);//è·åæ°æ®å¸§æ°æ®å
|
| | | endBytes = getFilesDataBytes.getEndData();//è·åç»æå¸§æ°æ®å
|
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | public static byte[] getRecDat() { |
| | | byte[] dat = new byte[0]; |
| | | try { |
| | | dat = null; |
| | | packet = new DatagramPacket(dat,100); |
| | | socket.receive(packet); |
| | | String hostAddress = packet.getAddress().getHostAddress(); |
| | | if (hostAddress.equals(ip)){ |
| | | int lenth=packet.getLength(); |
| | | dat=subBytes(packet.getData(),0, lenth); |
| | | } |
| | | return dat; |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | |
| | | /**å°æ°æ®åç»ç½å
³*/ |
| | | public static void sendData(byte[] data,String ipout,int portout) { |
| | | DatagramPacket packet=null; |
| | | InetAddress address=null; |
| | | //å¦ææ°æ®ç±»åç¸å |
| | | try { |
| | | address=InetAddress.getByName(ipout); |
| | | } catch (UnknownHostException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | packet=new DatagramPacket(data,data.length, address, portout); |
| | | try { |
| | | socket.send(packet); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public static byte[] subBytes(byte[] src, int begin, int count) { |
| | | byte[] bs = new byte[count]; |
| | | System.arraycopy(src, begin, bs, 0, count); |
| | | return bs; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | public static void stopThread() { |
| | | if (socket != null && !socket.isClosed()) { |
| | | socket.close();//å
³ésocket对象 |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package ymodem; |
| | | |
| | | public class ymodem { |
| | | public static final int EOT = 0x04; // ç»æä¼ è¾ |
| | | public static final int ACK = 0x06; // ååº |
| | | public static final int NAK = 0x15; // ä¸ååº |
| | | public static final int CA = 0x18; // ä¼ è¾ç»æ¢ |
| | | public static final int C = 0x43; // è¯·æ±æ°æ®å
|
| | | |
| | | } |