简体   繁体   中英

how to send cpcl commands with flutter_blue package in flutter

i try send cpcl commands with flutter_blue package(this works if i send only string ) to my zebra zq220 printer.

At first i write in this mode and it works if i input only string with nospace:

void printBluetooth() async {
 if (textController1.text == "") {
   return;
 }
List<String> lst = [];

 lst.add("hello"); 
 lst.add("world");
 lst.add("FORM");
 lst.add("PRINT");
 
 for (int i = 0; i < lst.length; ++i) {
  await  sendByte(lst[i]);
 }
 
}   



Future<void> sendByte(String scmd) async {
 print("sendBytes");
 // List<int> bytes = Uint8List.fromList(List.from(scmd.codeUnits));
  //List<int> bytes = gbk.encode(scmd);

 List<BluetoothService> services = await connectDevice!.discoverServices();
 for (BluetoothService service in services) {
   var characteristics = service.characteristics;
   for (BluetoothCharacteristic c in characteristics)  {
     c.write(uft8.encode(scmd));
   }
   
 }
}

    

But if i try to use my cpcl commands like this(which contains space):

    List<String> lst = [];
  lst.add("! 0 200 200 380 1 "); //initialize
  lst.add("! 0 200 200 380 ${stampaQuantitaController.text} "); //initialize
  lst.add("SETBOLD 1"); ////
  lst.add("TEXT 7 1 100 0 HAHAHAH");
  if (descrizioneProdotto.trim().length > 32) {
    lst.add("TEXT 7 1 100 40 ${descrizioneProdotto.substring(32, 32)}");
  }
  lst.add("SETBOLD 0"); //

  if (prezzoOfferta == "") {
    //'lst.Add("SETMAG 2 2");
    lst.add("CENTER"); 
    lst.add("SETMAG 1 1"); 
    lst.add("TEXT 4 2 0 80 € ${prezzoVendita.replaceAll(".", ",")}");
    lst.add("SETMAG 0 0"); 
    lst.add("LEFT"); 
  } else {
    lst.add("SETMAG 1 1"); 
    lst.add("TEXT 4 0 110 80 ${prezzoOfferta.replaceAll(".", ",")}");
    lst.add("LINE 100 100 220 100 1");
    lst.add( "TEXT 4 0 100 125 - ${(double.parse(prezzoOfferta) - double.parse(prezzoVendita)) / double.parse(prezzoOfferta) * 100} %");
    lst.add("SETMAG 1 2"); //
    lst.add("SETBOLD 2"); //
    lst.add("SETSP 6"); //
    lst.add("TEXT 4 0 300 70 € ${prezzoVendita.replaceAll(".", ",")}");
    lst.add("SETSP 0"); //
    lst.add("SETBOLD 0"); //
    lst.add("SETMAG 0 0"); //
    lst.add("TEXT 7 0 280 155 FINE: $pezzoOffertaFine ");
  }
  lst.add("BARCODE-TEXT 7 0 5");
  lst.add("BARCODE EAN13 1 1 50 130 180 ${textController1.text}");
  lst.add("TEXT 7 0 350 180 $codArticolo");
  lst.add("TEXT 7 0 350 200 $fornitore");
  lst.add("TEXT 7 0 350 220 ${DateFormat("yyyy-MM-dd").format(DateTime.now())}");
  lst.add("FORM");
  lst.add("PRINT");


   for (int i = 0; i < lst.length; ++i) {
     await  sendByte(lst[i]);
    }




Future<void> sendByte(String scmd) async {
    print("sendBytes");
    // List<int> bytes = Uint8List.fromList(List.from(scmd.codeUnits));
     //List<int> bytes = gbk.encode(scmd);
   
    List<BluetoothService> services = await connectDevice!.discoverServices();
    for (BluetoothService service in services) {
      var characteristics = service.characteristics;
      for (BluetoothCharacteristic c in characteristics)  {
        c.write(latin1.encode(scmd));
        // or i use  c.write(gbk.encode(scmd));
       
      }
      
    }
  }







it tell me that

#0      BluetoothCharacteristic.write.<anonymous closure> (package:flutter_blue/src/bluetooth_characteristic.dart:139:15)
#1      _rootRunUnary (dart:async/zone.dart:1434:47)
#2      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
<asynchronous suspension>

but i can see on the display of zebra zq220 printer thant it has received anyway something from my app.

Thanks very very much in advance,and best regards

I finally find the solution

Future<void> sendByte(String scmd) async {
print("sendBytes");
// List<int> bytes = Uint8List.fromList(List.from(scmd.codeUnits));
 //List<int> bytes = gbk.encode(scmd);

List<BluetoothService> services = await connectDevice!.discoverServices();
for (BluetoothService service in services) {
  var characteristics = service.characteristics;
  for (BluetoothCharacteristic c in characteristics)  {
    c.write(utf8.encode(scmd));
    // or i use  c.write(gbk_bytes.encode(scmd)); 
   
  }
}}

and

  List<String> lst = [];
 lst.add("! 0 200 200 380 1 "); //initialize
 lst.add("! 0 200 200 380 ${stampaQuantitaController.text} "); //initialize
 lst.add("SETBOLD 1"); ////
 lst.add("TEXT 7 1 100 0 HAHAHAH");
 if (descrizioneProdotto.trim().length > 32) {
   lst.add("TEXT 7 1 100 40 ${descrizioneProdotto.substring(32, 32)}");
 }
 lst.add("SETBOLD 0"); //

 if (prezzoOfferta == "") {
   //'lst.Add("SETMAG 2 2");
   lst.add("CENTER"); 
   lst.add("SETMAG 1 1"); 
   lst.add("TEXT 4 2 0 80 € ${prezzoVendita.replaceAll(".", ",")}");
   lst.add("SETMAG 0 0"); 
   lst.add("LEFT"); 
 } else {
   lst.add("SETMAG 1 1"); 
   lst.add("TEXT 4 0 110 80 ${prezzoOfferta.replaceAll(".", ",")}");
   lst.add("LINE 100 100 220 100 1");
   lst.add( "TEXT 4 0 100 125 - ${(double.parse(prezzoOfferta) - double.parse(prezzoVendita)) / double.parse(prezzoOfferta) * 100} %");
   lst.add("SETMAG 1 2"); //
   lst.add("SETBOLD 2"); //
   lst.add("SETSP 6"); //
   lst.add("TEXT 4 0 300 70 € ${prezzoVendita.replaceAll(".", ",")}");
   lst.add("SETSP 0"); //
   lst.add("SETBOLD 0"); //
   lst.add("SETMAG 0 0"); //
   lst.add("TEXT 7 0 280 155 FINE: $pezzoOffertaFine ");
 }
 lst.add("BARCODE-TEXT 7 0 5");
 lst.add("BARCODE EAN13 1 1 50 130 180 ${textController1.text}");
 lst.add("TEXT 7 0 350 180 $codArticolo");
 lst.add("TEXT 7 0 350 200 $fornitore");
 lst.add("TEXT 7 0 350 220 ${DateFormat("yyyy-MM-dd").format(DateTime.now())}");
 lst.add("FORM");
 lst.add("PRINT");

//mistake is here,at first i sended byte with loop .But now i use a string to storage list values and put them in one time all in bytes and send them
String ss="";
  for (int i = 0; i < lst.length; ++i) {
    ss+="${lst[i]}\r\n";
   }
await  sendByte(ss);


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM