Home > Net >  flutter convert int to byte (flutter with PLC, RTC)
flutter convert int to byte (flutter with PLC, RTC)

Time:06-18

i'm try to convert int to byte and cant find an example working fo me

i need convert int in byte or bytes to sent in 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);
}

and i recive

|d|a|t|a| |:|1|0|,|3|6|,|1|2|2|

how i can recive

|d|a|t|a| |:|LF|,|$|,|z| or |d|a|t|a| |:|0x0A|,|$|,|z|

i work with plc and he like bytes in absolute indexing

it's possible?

if use

int a = 0;
for(;;){
  socket.write(String.fromCharCode(a));
  a  ;
  if(a>=256)a=0;
}

recive over 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...

In any case i can use strings but for some protocols it's a huge of data, minimun moltiply x4 if use 3 char and one separator

i try use RTC with flutter on windows app

for the last project i create a

javaServer<-low side code bit/byte->plc 

and

flutter<-json->javaServer

i think is ugly

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();
    });
  }

CodePudding user response:

Instead of using socket.write to write the character you should use the add to write a list of bytes.

From your example with all bytes from 0 to 255 you need to do something like below:

socket.write('data : ');
String separator = '';
for (int a = 0; a < 256; a  ) {
  if (separator != null) socket.write(separator);
  socket.add([a]);
  separator = ',';
}

With this above you're going to get something like below on the client-side:

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                           ,.,.,.
  • Related