2024年2月27日 星期二

用 dart 語法, 針對 Uint8List 資料流(stream): 內容是 vt100 字串, 僅把 ESC[ ... m 移除並轉換成 utf8 字串

Uint8List remain = Uint8List(0);
String utf8Log = "";
void stream2utf8(Uint8List data) {
  final findLF = data.lastIndexWhere((e) => e == 0xa);// find position of the last \n
  final endLF = findLF + 1; // to include LF
  final len = remain.length + (findLF < 0 ? data.length : endLF);
  final resemble = Uint8List(len);
  if (remain.isNotEmpty) resemble.setAll(0, remain);// copy remain to front of resemble
  resemble.setRange(remain.length, len, data); // append sublist of data to resemble
  if (findLF < 0) { // keep in memory
    remain = resemble;
  } else { // line feed found, time to flush out
    remain = Uint8List.sublistView(data, endLF);
    Uint8List afterESC = Uint8List.sublistView(resemble);
    while (afterESC.isNotEmpty) { // until empty
      final findESC = afterESC.indexOf(0x1b); // find ESC position
      utf8Log += utf8.decode(Uint8List.sublistView(afterESC, 0,
        findESC < 0 ? afterESC.length : findESC
      ));
      if (findESC < 0) break;
      var lenESC = 1; // sequence ^[ ... m seek
      if (afterESC[findESC + lenESC] == 0x5b) { // found sequence start '['
        do {
          lenESC ++;  // one byte advance
          if (afterESC[findESC + lenESC] == 0x6d) { // found end of character 'm'
            lenESC ++;// one byte advance
            break;
          }
        } while (findESC + lenESC < afterESC.length);
        if (findESC + lenESC >= afterESC.length) { // todo: other ^[ sequence not yet implement!
          afterESC[findESC] = 0x5E; // change ESC from 0x1b to '^' to prevent infinit loop
          continue;// backoff to show this sequence
        }
      } else { // todo: other ^ sequence not yet implement!
        afterESC[findESC] = 0x5E; // change ESC from 0x1b to '^' to prevent infinit loop
        continue;// backoff to show this sequence
      }
      afterESC = Uint8List.sublistView(afterESC, findESC + lenESC);// ok,  go ahead
    }
    setState((){}); // update screen if necessary
}

2024年2月19日 星期一

簡單的 flutter app 用來測試 usb serial for android

在 Android 系統上要使用 usb serial port 可以透過 usb-serial-for-android , 它是用 JAVA 語言寫的, 上官網打包先將它下載回來:  https://github.com/mik3y/usb-serial-for-android, 將它解壓縮

    unzip  usb-serial-for-android-master.zip

1. 打開終端機用 flutter 命令產生一個新專案(例如 usbadc):  

    flutter create usbadc

2. 將上述 usb-serial-for-android 解開的檔案, 只要把目錄 usbSerialForAndroid/src/main/java/com 整個驅動程式的目錄複製到新專案:


2024年2月8日 星期四

linux 上解決核心模組 ch34x.ko 掛載 /dev/ttyUSB0 時被強制斷線的問題

 參考討論文章: https://unix.stackexchange.com/questions/670636/unable-to-use-usb-dongle-based-on-usb-serial-converter-chip

只要有 root 權限執行以下腳本就能解決, 一勞永逸辦法是將它放在 /etc/rc.local 裡面, 一開機就執行

        # ...

    for f in /usr/lib/udev/rules.d/*brltty*.rules; do
        sudo ln -s /dev/null "/etc/udev/rules.d/$(basename "$f")"
    done
    sudo udevadm control --reload-rules

     

簡單 c 程式碼, 根據五行八卦相生相剋推斷吉凶

#include "stdio.h" // 五行: //               木2 //      水1           火3 //         金0     土4 // // 先天八卦 vs 五行 //                    ...