java - 将较长的 getTime() 分成 4 条短裤,实际上并不适用 Modbus
问题描述
我是一个初学者,我想将一个长的 gettime 分成 4 个用于 Modbus 协议的短裤,但它并不总是有效。我不知道为什么(可能是因为选角)。我希望有人可以帮助我:)
提前感谢那些可以启发我的人!
public void SetUp() {
long time = new Date().getTime(); // time in ms since epoch
System.out.println(time);
int high32 = (int)(time >> 32);
int low32 = (int)time;
long l = (((long)high32) << 32) | (low32 & 0xffffffffL);
short low16_1 = (short) high32;
short high16_1= (short) (high32 >> 16);
int complete = low16_1 | (high16_1 << 16);
short low16_2 = (short) low32;
short high16_2= (short) (low32 >> 16);
int complete2 = low16_2 | (high16_2 << 16);
long d = (((long)complete) << 32) | (complete2 & 0xffffffffL);
System.out.println(l);
System.out.println(d);
Date e = new Date (time);
System.out.println(e);
Date g = new Date (d);
System.out.println(g);
}
显示示例:
1530430114623
1530430114623
1530430114623
Sun Jul 01 09:28:34 CEST 2018
Sun Jul 01 09:28:34 CEST 2018
1530431375214
1530431375214
1533303293806
Sun Jul 01 09:49:35 CEST 2018
Fri Aug 03 15:34:53 CEST 2018
解决方案
只需使用工会
这是如何从 1 long(32 位)转换为 4 uint8(8 位)
union convertor{
long input;
uint8_t output[4];
}
convertor entry;
entry.input=new Date().getTime()
//the value will be split into 4 unit8_t of entry.output
你可以用 unit16_t 做类似的事情,在这里你必须使用 2 unit16_t 而不是 4。
然后,您可以将这些短裤组合在一起,以获取原始时间值
for(int i=0;i<4;i++)
entry.output[i]=data[i];
//get the original value from entry.intput as long
推荐阅读
- python-3.x - 使用 MongoDB 一次更新具有不同值的不同文档中的相同字段
- azure - Azure:分发给单个订阅的所有订阅者的服务总线
- javascript - Add a $ symbol next to number
- mysql - MySQL 与 PostgreSQL 上的分片
- sql - 为什么我不断收到 INSERT 错误:目标多于表达式
- python-3.x - MaybeEncodingError:发送结果时出错:'
' - r - 如何使用军用时间来计算时间间隔
- python-3.x - 关于订单 Set List
- python - 如何在页面刷新后等待之前通过 Selenium 和 Python 呈现的元素
- dns - GCP 请求无法转发到对等网络,因为对等项目禁用了 DNS 对等互连