我试图将int转换为字节,找不到适合我的示例
我需要转换成字节或字节在TCP ip发送
final server = await ServerSocket.bind(ip,port);
try{
server.listen((Socket socket) {
int a = 10; // line feed
int b = 36; // $
int c = 122; // z
const sec = Duration(seconds:1);
Timer.periodic(sec,(Timer t) {
socket.wite('data : $a,$b,$c');
});
} on SocketExeption catch(ex) {
print(ex.message);
}
和我收到
|d|a|t|a| |:|1|0|,|3|6|,|1|2|2|
如何接收
|d|a|t|a| |:|LF|,|$|,|z| or |d|a|t|a| |:|0x0A|,|$|,|z|
我和PLC一起工作,他喜欢绝对索引中的字节
这是可能的吗?
如果使用
int a = 0;
for(;;){
socket.write(String.fromCharCode(a));
a++;
if(a>=256)a=0;
}
接收TCP ip
byte0 =00.01....4F........7D.7E.7F.C2.C2....C2.C2..C2.C2.C3.C3.C3..C3.C3.00.01...
byte1 =00.00....00........00.00.00.80.81....84.85..BE.BF.80.81.82..BE.BF.00.00...
在任何情况下,我都可以使用字符串但对于某些协议,它是巨大的数据,最小乘x4如果使用3个字符和一个分隔符
我尝试在windows应用程序上使用RTC与flutter
为最后一个项目创建一个
javaServer<-low side code bit/byte->plc
和
flutter<-json->javaServer
我觉得很丑
work thanks LEPSCH
try {
client.listen((Socket socket) {
debugPrint('New TCP client ${socket.address.address}:${socket.port} connected.');
List<int> bytes = List.empty(growable: true);
for (var i = 0; i < 255; i++) {
bytes.add(i);
}
Timer.periodic(const Duration(seconds: 1), (Timer t) {
for (var i = 0; i < 255; i++) {
socket.add([bytes[i]]);
}
});
});
} on SocketException catch (ex) {
debugPrint(ex.message);
Timer.periodic(Duration(seconds: 1), (Timer t) {
startServer();
});
}
不应该使用socket.write
来写字符,而应该使用add
来写字节列表。
在您的示例中,所有字节从0到255,您需要做如下操作:
socket.write('data : ');
String separator = '';
for (int a = 0; a < 256; a++) {
if (separator != null) socket.write(separator);
socket.add([a]);
separator = ',';
}
有了上面的内容,你将在客户端得到如下所示:
00000000: 6461 7461 203a 2000 2c01 2c02 2c03 2c04 data : .,.,.,.,.
00000010: 2c05 2c06 2c07 2c08 2c09 2c0a 2c0b 2c0c ,.,.,.,.,.,.,.,.
00000020: 2c0d 2c0e 2c0f 2c10 2c11 2c12 2c13 2c14 ,.,.,.,.,.,.,.,.
00000030: 2c15 2c16 2c17 2c18 2c19 2c1a 2c1b 2c1c ,.,.,.,.,.,.,.,.
00000040: 2c1d 2c1e 2c1f 2c20 2c21 2c22 2c23 2c24 ,.,.,., ,!,",#,$
00000050: 2c25 2c26 2c27 2c28 2c29 2c2a 2c2b 2c2c ,%,&,',(,),*,+,,
00000060: 2c2d 2c2e 2c2f 2c30 2c31 2c32 2c33 2c34 ,-,.,/,0,1,2,3,4
00000070: 2c35 2c36 2c37 2c38 2c39 2c3a 2c3b 2c3c ,5,6,7,8,9,:,;,<
00000080: 2c3d 2c3e 2c3f 2c40 2c41 2c42 2c43 2c44 ,=,>,?,@,A,B,C,D
00000090: 2c45 2c46 2c47 2c48 2c49 2c4a 2c4b 2c4c ,E,F,G,H,I,J,K,L
000000a0: 2c4d 2c4e 2c4f 2c50 2c51 2c52 2c53 2c54 ,M,N,O,P,Q,R,S,T
000000b0: 2c55 2c56 2c57 2c58 2c59 2c5a 2c5b 2c5c ,U,V,W,X,Y,Z,[,
000000c0: 2c5d 2c5e 2c5f 2c60 2c61 2c62 2c63 2c64 ,],^,_,`,a,b,c,d
000000d0: 2c65 2c66 2c67 2c68 2c69 2c6a 2c6b 2c6c ,e,f,g,h,i,j,k,l
000000e0: 2c6d 2c6e 2c6f 2c70 2c71 2c72 2c73 2c74 ,m,n,o,p,q,r,s,t
000000f0: 2c75 2c76 2c77 2c78 2c79 2c7a 2c7b 2c7c ,u,v,w,x,y,z,{,|
00000100: 2c7d 2c7e 2c7f 2c80 2c81 2c82 2c83 2c84 ,},~,.,.,.,.,.,.
00000110: 2c85 2c86 2c87 2c88 2c89 2c8a 2c8b 2c8c ,.,.,.,.,.,.,.,.
00000120: 2c8d 2c8e 2c8f 2c90 2c91 2c92 2c93 2c94 ,.,.,.,.,.,.,.,.
00000130: 2c95 2c96 2c97 2c98 2c99 2c9a 2c9b 2c9c ,.,.,.,.,.,.,.,.
00000140: 2c9d 2c9e 2c9f 2ca0 2ca1 2ca2 2ca3 2ca4 ,.,.,.,.,.,.,.,.
00000150: 2ca5 2ca6 2ca7 2ca8 2ca9 2caa 2cab 2cac ,.,.,.,.,.,.,.,.
00000160: 2cad 2cae 2caf 2cb0 2cb1 2cb2 2cb3 2cb4 ,.,.,.,.,.,.,.,.
00000170: 2cb5 2cb6 2cb7 2cb8 2cb9 2cba 2cbb 2cbc ,.,.,.,.,.,.,.,.
00000180: 2cbd 2cbe 2cbf 2cc0 2cc1 2cc2 2cc3 2cc4 ,.,.,.,.,.,.,.,.
00000190: 2cc5 2cc6 2cc7 2cc8 2cc9 2cca 2ccb 2ccc ,.,.,.,.,.,.,.,.
000001a0: 2ccd 2cce 2ccf 2cd0 2cd1 2cd2 2cd3 2cd4 ,.,.,.,.,.,.,.,.
000001b0: 2cd5 2cd6 2cd7 2cd8 2cd9 2cda 2cdb 2cdc ,.,.,.,.,.,.,.,.
000001c0: 2cdd 2cde 2cdf 2ce0 2ce1 2ce2 2ce3 2ce4 ,.,.,.,.,.,.,.,.
000001d0: 2ce5 2ce6 2ce7 2ce8 2ce9 2cea 2ceb 2cec ,.,.,.,.,.,.,.,.
000001e0: 2ced 2cee 2cef 2cf0 2cf1 2cf2 2cf3 2cf4 ,.,.,.,.,.,.,.,.
000001f0: 2cf5 2cf6 2cf7 2cf8 2cf9 2cfa 2cfb 2cfc ,.,.,.,.,.,.,.,.
00000200: 2cfd 2cfe 2cff ,.,.,.