Commit 1c1b5932 by 谢海旭

添加 Ocpp16Helper.strToBcd 方法

1 parent 06bcbf71
...@@ -22,6 +22,7 @@ import cn.com.glsx.charging.util.PluralCache; ...@@ -22,6 +22,7 @@ import cn.com.glsx.charging.util.PluralCache;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
@Slf4j @Slf4j
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
...@@ -176,6 +177,35 @@ public class Ocpp16Helper { ...@@ -176,6 +177,35 @@ public class Ocpp16Helper {
} }
/** /**
* 字符串转单个bcd字节
*
* @param str
* @return
*/
public static byte strToBcd(String str) {
if (!StringUtils.hasLength(str)) {
throw new IllegalArgumentException("str cannot be empty");
}
int len = str.length();
if (len > 2) {
throw new IllegalArgumentException("Invalid str");
}
if (len == 1) {
str = "0" + str; // 补齐偶数位
}
char highChar = str.charAt(0);
char lowChar = str.charAt(1);
int high = Character.digit(highChar, 10);
int low = Character.digit(lowChar, 10);
return (byte) ((high << 4) | low);
}
/**
* 字符串 转ascii字节数组 * 字符串 转ascii字节数组
* 不足指定长度,默认后面补零 * 不足指定长度,默认后面补零
* *
......
package cn.com.glsx.charging.gbt.ocpp16; package cn.com.glsx.charging.gbt.ocpp16;
import cn.com.glsx.charging.modules.gbt.ocpp16.ClientMain; import cn.com.glsx.charging.modules.gbt.ocpp16.ClientMain;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper; import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client.TcpClient; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client.TcpClient;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.HeartbeatReqData; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.HeartbeatReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.LoginReqData; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.LoginReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.HeartbeatReq; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.HeartbeatReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.LoginReq; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.LoginReq;
/** /**
* 模拟充电桩端,连接云快充平台的测试服务器 * 模拟充电桩端,连接云快充平台的测试服务器
* 实现登录,心跳,交互 * 实现登录,心跳,交互
* *
* @author kome * @author kome
* @date 2023年11月6日 上午9:49:10 * @date 2023年11月6日 上午9:49:10
*/ */
public class ClientSimulateTest { public class ClientSimulateTest {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Ocpp16Helper.scan(); Ocpp16Helper.scan();
String host = "192.168.3.122"; String host = "192.168.3.122";
int port = 8888; int port = 8888;
TcpClient client = ClientMain.getInstance(host, port); TcpClient client = ClientMain.getInstance(host, port);
Thread.sleep(3000); Thread.sleep(3000);
LoginReq req = new LoginReq(); LoginReq req = new LoginReq();
req.setData(new LoginReqData()); req.setData(new LoginReqData());
req.getData().setPileId(Ocpp16Helper.strToBcd("55031412782305", req.getData().getPileId())); req.getData().setPileId(Ocpp16Helper.strToBcd("55031412782305", req.getData().getPileId()));
req.getData().setPointType((byte) 0); req.getData().setPointType((byte) 0);
req.getData().setGunCount((byte) 2); req.getData().setGunCount((byte) 2);
req.getData().setSoftVersion(Ocpp16Helper.strToBcd("v4.1.50", req.getData().getSoftVersion())); req.getData().setProtocolVersion((byte) 10);
req.getData().setProtocolVersion((byte) 10); req.getData().setSoftVersion(Ocpp16Helper.strToAscii("v4.1.50", req.getData().getSoftVersion()));
req.getData().setNetwork((byte) 0x03);
client.sendSync(req); req.getData().setSim(Ocpp16Helper.strToBcd("01010101010101010101", req.getData().getSim()));
req.getData().setOperator((byte) 0x04);
// 定期心跳
while (true) { client.sendSync(req);
HeartbeatReq heartbeat = new HeartbeatReq();
HeartbeatReqData hdata = new HeartbeatReqData(); // 定期心跳
hdata.setPileId(req.getData().getPileId()); while (true) {
hdata.setStatus((byte) 0); HeartbeatReqData hdata = new HeartbeatReqData();
heartbeat.setData(hdata); hdata.setPileId(req.getData().getPileId());
hdata.setGunNo(Ocpp16Helper.strToBcd("2"));
client.sendSync(heartbeat); hdata.setStatus((byte) 0);
Thread.sleep(10000); HeartbeatReq heartbeat = new HeartbeatReq();
} heartbeat.setData(hdata);
}
client.sendSync(heartbeat);
}
Thread.sleep(10000);
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!