汪菘
2 years ago
32 changed files with 47692 additions and 649 deletions
File diff suppressed because it is too large
@ -0,0 +1,18 @@ |
|||
2022-09-24 14:51:58.739 [nl-pool1-thread-2] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 111111111111 |
|||
2022-09-24 14:51:58.739 [nl-pool1-thread-2] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 判断当前有无指令:true |
|||
2022-09-24 14:51:58.848 [nl-pool1-thread-10] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 111111111111 |
|||
2022-09-24 14:51:58.852 [nl-pool1-thread-10] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 判断当前有无指令:true |
|||
2022-09-24 14:51:58.956 [nl-pool1-thread-7] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 111111111111 |
|||
2022-09-24 14:51:58.961 [nl-pool1-thread-7] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 判断当前有无指令:true |
|||
2022-09-24 14:51:59.065 [nl-pool1-thread-3] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 111111111111 |
|||
2022-09-24 14:51:59.065 [nl-pool1-thread-3] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 判断当前有无指令:true |
|||
2022-09-24 14:51:59.175 [nl-pool1-thread-5] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 111111111111 |
|||
2022-09-24 14:51:59.175 [nl-pool1-thread-5] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 判断当前有无指令:true |
|||
2022-09-24 14:51:59.284 [nl-pool1-thread-8] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 111111111111 |
|||
2022-09-24 14:51:59.284 [nl-pool1-thread-8] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 判断当前有无指令:true |
|||
2022-09-24 14:51:59.393 [nl-pool1-thread-10] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 111111111111 |
|||
2022-09-24 14:51:59.393 [nl-pool1-thread-10] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 判断当前有无指令:true |
|||
2022-09-24 14:51:59.502 [nl-pool1-thread-9] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 111111111111 |
|||
2022-09-24 14:51:59.502 [nl-pool1-thread-9] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 判断当前有无指令:true |
|||
2022-09-24 14:51:59.612 [nl-pool1-thread-6] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 111111111111 |
|||
2022-09-24 14:51:59.612 [nl-pool1-thread-6] INFO org.nl.acs.device_driver.xg_agv.XgagvDeviceDriver - 判断当前有无指令:true |
@ -0,0 +1,361 @@ |
|||
package org.nl.acs.device_driver.xg_agv; |
|||
|
|||
import cn.hutool.core.util.ObjectUtil; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.nl.acs.device.device_driver.standard_inspect.ItemDto; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
@Slf4j |
|||
public class ItemProtocol { |
|||
public static String is_charging = "is_charging"; |
|||
public static String stop = "stop"; |
|||
public static String DI0 = "DI0"; |
|||
public static String DI1 = "DI1"; |
|||
public static String DI2 = "DI2"; |
|||
public static String DI3 = "DI3"; |
|||
public static String DI4 = "DI4"; |
|||
public static String DI11 = "DI12"; |
|||
public static String DI12 = "DI12"; |
|||
public static String DO0 = "DO0"; |
|||
public static String DO1 = "DO1"; |
|||
public static String DO2 = "DO2"; |
|||
public static String x = "x"; |
|||
public static String y = "y"; |
|||
public static String angle = "angle"; |
|||
//站点的 id 号, 0 表示无导航站点
|
|||
public static String navigation_address = "navigation_address"; |
|||
//0 = 定位失败, 1 = 定位正确, 2 = 正在重定位, 3 = 定位完成;机器人刚启动后,定位状态为 2,
|
|||
// 当定位状态变为 3 时,可以用 [0x]00003 地址位确认定位正确,确认后定位状态将变为 1
|
|||
public static String positioning_status = "positioning_status"; |
|||
//0 = 无, 1 = 等待执行导航, 2 = 正在执行导航, 3 = 导航暂停, 4 = 到达, 5 = 失败, 6 = 取消, 7 = 超时
|
|||
public static String navigation_status = "navigation_status"; |
|||
//0 = 没有导航, 1 = 自由导航到任意点, 2 = 自由导航到站点, 3 = 路径导航到站点, 7 = 平动转动, 100 = 其他
|
|||
public static String navigation_type = "navigation_type"; |
|||
//范围为 0 ~ 1
|
|||
public static String electricity = "electricity"; |
|||
public static String address = "address"; |
|||
public static String next_address = "next_address"; |
|||
public static String home_relocation = "home_relocation"; |
|||
//确认定位正确
|
|||
public static String locate_correct = "locate_correct"; |
|||
public static String pause_navigation = "pause_navigation"; |
|||
public static String continue_navigation = "continue_navigation"; |
|||
public static String cancle_navigation = "cancle_navigation"; |
|||
//滚筒运行状态
|
|||
public static String drum_run_status = "drum_run_status"; |
|||
|
|||
|
|||
public static String to_home_relocation = "to_home_relocation"; |
|||
//确认定位正确
|
|||
public static String to_locate_correct = "to_locate_correct"; |
|||
public static String to_pause_navigation = "to_pause_navigation"; |
|||
public static String to_continue_navigation = "to_continue_navigation"; |
|||
public static String to_cancle_navigation = "to_cancle_navigation"; |
|||
public static String to_DO0_height = "to_DO0_height"; |
|||
public static String to_DO0_low = "to_DO0_low"; |
|||
public static String to_DO1_height = "to_DO1_height"; |
|||
public static String to_DO1_low = "to_DO1_low"; |
|||
public static String to_DO2_low = "to_DO2_low"; |
|||
public static String to_DO8_low = "to_DO8_low"; |
|||
public static String to_DO9_low = "to_DO9_low"; |
|||
|
|||
public static String to_address = "to_address"; |
|||
public static String to_clear_error = "to_clear_error"; |
|||
//虚拟高电平写
|
|||
public static String to_di_height = "to_di_height"; |
|||
//虚拟低电平写
|
|||
public static String to_di_low = "to_di_low"; |
|||
|
|||
|
|||
public int getIs_charging() { |
|||
return this.getOpcIntegerValue(is_charging); |
|||
} |
|||
|
|||
public int getStop() { |
|||
return this.getOpcIntegerValue(stop); |
|||
} |
|||
|
|||
public int getDI0() { |
|||
return this.getOpcIntegerValue(DI0); |
|||
} |
|||
|
|||
public int getDI1() { |
|||
return this.getOpcIntegerValue(DI1); |
|||
} |
|||
|
|||
public int getDI2() { |
|||
return this.getOpcIntegerValue(DI2); |
|||
} |
|||
|
|||
public int getDI3() { |
|||
return this.getOpcIntegerValue(DI3); |
|||
} |
|||
|
|||
public int getDI4() { |
|||
return this.getOpcIntegerValue(DI4); |
|||
|
|||
} |
|||
|
|||
public int getDI11() { |
|||
return this.getOpcIntegerValue(DI11); |
|||
|
|||
} |
|||
|
|||
public int getDI12() { |
|||
return this.getOpcIntegerValue(DI12); |
|||
|
|||
} |
|||
|
|||
public int getDO0() { |
|||
return this.getOpcIntegerValue(DO0); |
|||
|
|||
} |
|||
|
|||
public int getDO1() { |
|||
return this.getOpcIntegerValue(DO1); |
|||
|
|||
} |
|||
public int getDO2() { |
|||
return this.getOpcIntegerValue(DO2); |
|||
|
|||
} |
|||
|
|||
public Float getX() { |
|||
return this.getOpcFloatValue(x); |
|||
|
|||
} |
|||
|
|||
public Float getY() { |
|||
return this.getOpcFloatValue(y); |
|||
|
|||
} |
|||
|
|||
public Float getAngle() { |
|||
return this.getOpcFloatValue(angle); |
|||
|
|||
} |
|||
|
|||
public short getNavigation_address() { |
|||
return this.getOpcShortValue(navigation_address); |
|||
|
|||
} |
|||
|
|||
public short getPositioning_status() { |
|||
return this.getOpcShortValue(positioning_status); |
|||
} |
|||
|
|||
public short getNavigation_status() { |
|||
return this.getOpcShortValue(navigation_status); |
|||
} |
|||
|
|||
public short getNavigation_type() { |
|||
return this.getOpcShortValue(navigation_type); |
|||
|
|||
} |
|||
|
|||
public int getElectricity() { |
|||
return this.getOpcIntegerValue(electricity); |
|||
|
|||
} |
|||
|
|||
public int getAddress() { |
|||
return this.getOpcIntegerValue(address); |
|||
|
|||
} |
|||
|
|||
public short getNext_address() { |
|||
return this.getOpcShortValue(next_address); |
|||
} |
|||
|
|||
public int getHome_relocation() { |
|||
return this.getOpcIntegerValue(home_relocation); |
|||
|
|||
} |
|||
|
|||
public int getLocate_correct() { |
|||
return this.getOpcIntegerValue(locate_correct); |
|||
|
|||
} |
|||
|
|||
public int getPause_navigation() { |
|||
return this.getOpcIntegerValue(pause_navigation); |
|||
|
|||
} |
|||
|
|||
public int getContinue_navigation() { |
|||
return this.getOpcIntegerValue(continue_navigation); |
|||
|
|||
} |
|||
|
|||
public int getCancle_navigation() { |
|||
return this.getOpcIntegerValue(cancle_navigation); |
|||
} |
|||
|
|||
public short getDrum_run_status() { |
|||
return this.getOpcShortValue(drum_run_status); |
|||
} |
|||
|
|||
public static void setTo_home_relocation(String to_home_relocation) { |
|||
ItemProtocol.to_home_relocation = to_home_relocation; |
|||
} |
|||
|
|||
public static void setTo_locate_correct(String to_locate_correct) { |
|||
ItemProtocol.to_locate_correct = to_locate_correct; |
|||
} |
|||
|
|||
public static void setTo_pause_navigation(String to_pause_navigation) { |
|||
ItemProtocol.to_pause_navigation = to_pause_navigation; |
|||
} |
|||
|
|||
public static void setTo_continue_navigation(String to_continue_navigation) { |
|||
ItemProtocol.to_continue_navigation = to_continue_navigation; |
|||
} |
|||
|
|||
public static void setTo_cancle_navigation(String to_cancle_navigation) { |
|||
ItemProtocol.to_cancle_navigation = to_cancle_navigation; |
|||
} |
|||
|
|||
public static void setTo_DO0_height(String to_DO0_height) { |
|||
ItemProtocol.to_DO0_height = to_DO0_height; |
|||
} |
|||
|
|||
public static void setTo_DO0_low(String to_DO0_low) { |
|||
ItemProtocol.to_DO0_low = to_DO0_low; |
|||
} |
|||
|
|||
public static void setTo_DO1_height(String to_DO1_height) { |
|||
ItemProtocol.to_DO1_height = to_DO1_height; |
|||
} |
|||
|
|||
public static void setTo_DO1_low(String to_DO1_low) { |
|||
ItemProtocol.to_DO1_low = to_DO1_low; |
|||
} |
|||
|
|||
public static void setTo_DO8_low(String to_DO8_low) { |
|||
ItemProtocol.to_DO8_low = to_DO8_low; |
|||
} |
|||
|
|||
public static void setTo_DO9_low(String to_DO9_low) { |
|||
ItemProtocol.to_DO9_low = to_DO9_low; |
|||
} |
|||
|
|||
|
|||
public static void setTo_address(String to_address) { |
|||
ItemProtocol.to_address = to_address; |
|||
} |
|||
|
|||
public static void setTo_clear_error(String to_clear_error) { |
|||
ItemProtocol.to_clear_error = to_clear_error; |
|||
} |
|||
|
|||
public static void setTo_di_height(String to_di_height) { |
|||
ItemProtocol.to_di_height = to_di_height; |
|||
} |
|||
|
|||
public static void setTo_di_low(String to_di_low) { |
|||
ItemProtocol.to_di_low = to_di_low; |
|||
} |
|||
|
|||
private XgagvDeviceDriver driver; |
|||
|
|||
public ItemProtocol(XgagvDeviceDriver driver) { |
|||
this.driver = driver; |
|||
} |
|||
|
|||
public ItemProtocol() { |
|||
|
|||
} |
|||
|
|||
|
|||
public Float getOpcFloatValue(String protocol) { |
|||
Float value = this.driver.getFloatValue(protocol); |
|||
if (ObjectUtil.isEmpty(value)) { |
|||
log.error("读取错误!"); |
|||
} else { |
|||
return value; |
|||
} |
|||
return 0.0f; |
|||
} |
|||
|
|||
public int getOpcIntegerValue(String protocol) { |
|||
Integer value = this.driver.getIntegerValue(protocol); |
|||
if (value == null) { |
|||
log.error("读取错误!"); |
|||
} else { |
|||
return value; |
|||
} |
|||
return 0; |
|||
|
|||
} |
|||
|
|||
public short getOpcShortValue(String protocol) { |
|||
Short value = this.driver.getShortValue(protocol); |
|||
if (value == null) { |
|||
log.error("读取错误!"); |
|||
} else { |
|||
return value; |
|||
} |
|||
return 0; |
|||
|
|||
} |
|||
|
|||
public static List<ItemDto> getReadableItemDtos() { |
|||
ArrayList list = new ArrayList(); |
|||
list.add(new ItemDto(is_charging, "是否充电", "10003")); |
|||
list.add(new ItemDto(DI0, "DI0", "10020")); |
|||
list.add(new ItemDto(DI1, "DI1", "10021")); |
|||
list.add(new ItemDto(DI2, "DI2", "10022")); |
|||
list.add(new ItemDto(DI3, "DI3", "10023")); |
|||
list.add(new ItemDto(DI4, "DI4", "10024")); |
|||
list.add(new ItemDto(DO1, "DO1", "10061")); |
|||
list.add(new ItemDto(DO2, "DO2", "10062")); |
|||
list.add(new ItemDto(DI11, "DI11", "10031")); |
|||
list.add(new ItemDto(DI12, "DI12", "10032")); |
|||
list.add(new ItemDto(x, "x", "30001")); |
|||
list.add(new ItemDto(y, "y", "30003")); |
|||
list.add(new ItemDto(angle, "angle", "30005")); |
|||
list.add(new ItemDto(navigation_address, "当前导航站点", "30007")); |
|||
list.add(new ItemDto(positioning_status, "定位状态", "30008")); |
|||
list.add(new ItemDto(navigation_status, "当前导航状态", "30009")); |
|||
list.add(new ItemDto(navigation_type, "当前导航类型", "30010")); |
|||
list.add(new ItemDto(electricity, "电量", "30013")); |
|||
list.add(new ItemDto(address, "当前所在站点", "30034")); |
|||
list.add(new ItemDto(next_address, "下一个要经过的站点", "30036")); |
|||
list.add(new ItemDto(home_relocation, "在Home点重定位", "00002")); |
|||
list.add(new ItemDto(locate_correct, "确认定位正确", "00003")); |
|||
list.add(new ItemDto(pause_navigation, "暂停导航", "00004")); |
|||
list.add(new ItemDto(continue_navigation, "继续导航", "00005")); |
|||
list.add(new ItemDto(cancle_navigation, "取消导航", "00006")); |
|||
list.add(new ItemDto(drum_run_status, "滚筒运行状态", "30062")); |
|||
list.add(new ItemDto(stop, "车子停止", "10019")); |
|||
|
|||
|
|||
return list; |
|||
} |
|||
|
|||
public static List<ItemDto> getWriteableItemDtos() { |
|||
ArrayList list = new ArrayList(); |
|||
list.add(new ItemDto(to_home_relocation, "to_在Home点重定位", "00002")); |
|||
list.add(new ItemDto(to_locate_correct, "to_确认定位正确", "00003")); |
|||
list.add(new ItemDto(to_pause_navigation, "to_暂停导航", "00004")); |
|||
list.add(new ItemDto(to_continue_navigation, "to_继续导航", "00005")); |
|||
list.add(new ItemDto(to_cancle_navigation, "to_取消导航", "00006")); |
|||
list.add(new ItemDto(to_DO0_height, "to_DO0置为高电平", "00061")); |
|||
list.add(new ItemDto(to_DO0_low, "to_DO0置为低电平", "00020")); |
|||
list.add(new ItemDto(to_DO1_height, "to_DO1置为高电平", "00062")); |
|||
list.add(new ItemDto(to_DO1_low, "to_DO1置为低电平", "00021")); |
|||
list.add(new ItemDto(to_DO2_low, "to_DO2置为低电平", "00022")); |
|||
list.add(new ItemDto(to_DO8_low, "to_DO8置为低电平", "00028")); |
|||
list.add(new ItemDto(to_DO9_low, "to_DO9置为低电平", "00029")); |
|||
list.add(new ItemDto(to_address, "to_address", "40001")); |
|||
list.add(new ItemDto(to_clear_error, "to_clear_error", "40090")); |
|||
list.add(new ItemDto(to_di_height, "虚拟高电平写", "40063")); |
|||
list.add(new ItemDto(to_di_low, "虚拟低电平写", "40064")); |
|||
|
|||
return list; |
|||
} |
|||
|
|||
} |
|||
|
@ -0,0 +1,292 @@ |
|||
package org.nl.acs.device_driver.xg_agv; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.JSONArray; |
|||
|
|||
import java.io.UnsupportedEncodingException; |
|||
import java.util.Arrays; |
|||
import java.io.IOException; |
|||
import java.io.InputStream; |
|||
import java.io.OutputStream; |
|||
import java.io.UnsupportedEncodingException; |
|||
import java.net.ConnectException; |
|||
import java.net.Socket; |
|||
import java.net.UnknownHostException; |
|||
import java.util.List; |
|||
|
|||
public class TCPAGVClient { |
|||
|
|||
private static Socket socket; |
|||
private static OutputStream socketOut; |
|||
private static InputStream socketIn; |
|||
static byte[] requestb ; |
|||
|
|||
public static byte[] TCPRemoteConnectRead(String ipstr,int portstr,byte[] typestr) |
|||
{ |
|||
byte[] bytes = new byte[2048]; |
|||
try |
|||
{ |
|||
requestb=typestr; |
|||
socket = new Socket(ipstr,portstr); |
|||
socket.setSoTimeout(1000); |
|||
socketOut = socket.getOutputStream(); |
|||
|
|||
socketOut.write(requestb); |
|||
socketOut.flush(); |
|||
|
|||
try { |
|||
Thread.sleep(300); |
|||
} catch (InterruptedException e) { |
|||
// TODO Auto-generated catch block
|
|||
e.printStackTrace(); |
|||
} |
|||
//通过shutdownOutput高速服务器已经发送完数据,后续只能接受数据
|
|||
socket.shutdownOutput(); |
|||
|
|||
socketIn = socket.getInputStream(); |
|||
|
|||
int len; |
|||
StringBuilder sb = new StringBuilder(); |
|||
while ((len = socketIn.read(bytes)) != -1) { |
|||
sb.append(new String(bytes, 0, len)); |
|||
} |
|||
String s2=sb.toString().trim(); |
|||
System.out.println(s2); |
|||
|
|||
} |
|||
catch (UnknownHostException e) |
|||
{ |
|||
e.printStackTrace(); |
|||
} |
|||
catch (ConnectException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
catch (IOException e) { |
|||
e.printStackTrace(); |
|||
}finally { |
|||
//循环结束则退出输入流
|
|||
synchronized (socketIn) { |
|||
if (socketOut != null) { |
|||
try |
|||
{ |
|||
socketOut.close(); |
|||
} |
|||
catch (IOException e) |
|||
{ |
|||
// TODO Auto-generated catch block
|
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
if (socketIn != null) { |
|||
try |
|||
{ |
|||
socketIn.close(); |
|||
} |
|||
catch (IOException e) |
|||
{ |
|||
// TODO Auto-generated catch block
|
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
if (socket != null) { |
|||
try |
|||
{ |
|||
socket.close(); |
|||
} |
|||
catch (IOException e) |
|||
{ |
|||
// TODO Auto-generated catch block
|
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
return bytes; |
|||
} |
|||
|
|||
public static byte[] TCPRemoteConnectRead(String ipstr,int portstr,String typestr) |
|||
{ |
|||
byte[] bytes = new byte[2048]; |
|||
try |
|||
{ |
|||
requestb=typestr.getBytes(); |
|||
socket = new Socket(ipstr,portstr); |
|||
socket.setSoTimeout(1000); |
|||
socketOut = socket.getOutputStream(); |
|||
|
|||
socketOut.write(requestb); |
|||
socketOut.flush(); |
|||
|
|||
try { |
|||
Thread.sleep(300); |
|||
} catch (InterruptedException e) { |
|||
// TODO Auto-generated catch block
|
|||
e.printStackTrace(); |
|||
} |
|||
//通过shutdownOutput高速服务器已经发送完数据,后续只能接受数据
|
|||
socket.shutdownOutput(); |
|||
|
|||
socketIn = socket.getInputStream(); |
|||
|
|||
int len; |
|||
StringBuilder sb = new StringBuilder(); |
|||
while ((len = socketIn.read(bytes)) != -1) { |
|||
sb.append(new String(bytes, 0, len)); |
|||
} |
|||
String s2=sb.toString().trim(); |
|||
System.out.println(s2); |
|||
|
|||
} |
|||
catch (UnknownHostException e) |
|||
{ |
|||
e.printStackTrace(); |
|||
} |
|||
catch (ConnectException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
catch (IOException e) { |
|||
e.printStackTrace(); |
|||
}finally { |
|||
//循环结束则退出输入流
|
|||
synchronized (socketIn) { |
|||
if (socketOut != null) { |
|||
try |
|||
{ |
|||
socketOut.close(); |
|||
} |
|||
catch (IOException e) |
|||
{ |
|||
// TODO Auto-generated catch block
|
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
if (socketIn != null) { |
|||
try |
|||
{ |
|||
socketIn.close(); |
|||
} |
|||
catch (IOException e) |
|||
{ |
|||
// TODO Auto-generated catch block
|
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
if (socket != null) { |
|||
try |
|||
{ |
|||
socket.close(); |
|||
} |
|||
catch (IOException e) |
|||
{ |
|||
// TODO Auto-generated catch block
|
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
return bytes; |
|||
} |
|||
|
|||
|
|||
public static int IntToHexHigh(int b) { |
|||
int big = (b & 0xFF00) >> 8; |
|||
return big; |
|||
} |
|||
public static int IntToHexLow(int b) { |
|||
int little = b & 0xFF; |
|||
return little; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* @param num:要获取二进制值的数 |
|||
* @param index:倒数第一位为0,依次类推 |
|||
*/ |
|||
public static int get(int num, int index) |
|||
{ |
|||
return (num & (0x1 << index)) >> index; |
|||
} |
|||
|
|||
/** |
|||
* 16进制转换成为string类型字符串 |
|||
* @param s |
|||
* @return |
|||
*/ |
|||
public static String hexStringToString(String s) { |
|||
if (s == null || s.equals("")) { |
|||
return null; |
|||
} |
|||
s = s.replace(" ", ""); |
|||
byte[] baKeyword = new byte[s.length() / 2]; |
|||
for (int i = 0; i < baKeyword.length; i++) { |
|||
try { |
|||
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16)); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
try { |
|||
// s = new String(baKeyword, "UTF-8");
|
|||
// new String();
|
|||
s = new String(baKeyword, "GBK"); |
|||
new String(); |
|||
} catch (Exception e1) { |
|||
e1.printStackTrace(); |
|||
} |
|||
return s; |
|||
} |
|||
|
|||
public static void test() |
|||
{ |
|||
String str="{\"id\":\"S001\",\"version\":\"v1.1.0\",\"model\":\"S1\"}"; |
|||
byte[] ba; |
|||
try |
|||
{ |
|||
ba = str.getBytes("UTF-8"); |
|||
|
|||
String s_new = new String(ba, "UTF-8"); |
|||
System.out.println("原始字符串:\t" + str); |
|||
System.out.println("byte数组地址:\t" + ba); |
|||
System.out.println("输出byte数组:\t" + Arrays.toString(ba)); |
|||
System.out.println("输出byte数组长度:\t" + ba.length); |
|||
System.out.println("转换为字符串:\t" + s_new); |
|||
} |
|||
catch (UnsupportedEncodingException e) |
|||
{ |
|||
// TODO Auto-generated catch block
|
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
//字符串转化成为16进制字符串
|
|||
public static String strTo16(String s) { |
|||
String str = ""; |
|||
for (int i = 0; i < s.length(); i++) { |
|||
int ch = (int) s.charAt(i); |
|||
String s4 = Integer.toHexString(ch).toUpperCase(); |
|||
str = str + s4; |
|||
} |
|||
return str; |
|||
} |
|||
//16进制字符串转字节数组
|
|||
public static byte[] strToHexToByte(String str) |
|||
{ |
|||
byte[] bytestr; |
|||
try |
|||
{ |
|||
bytestr = str.getBytes("UTF-8"); |
|||
System.out.println("输出byte数组:\t" + Arrays.toString(bytestr)); |
|||
return bytestr; |
|||
} |
|||
catch (UnsupportedEncodingException e) |
|||
{ |
|||
// TODO Auto-generated catch block
|
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
} |
|||
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue