分类 无用之物 下的文章

黑历史?白历史!(笑

开机splash上有彩条的时候使用
需要等到设备完全进系统后再执行
C#写的首个练手项目

/* NB30 AtCmdXSoftUpdate v0.1
   (C)Yuu 2020
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO.Ports;

namespace AtCmdXSoftUpdate {
  class Program {
    static void Main(string[] args) {
        Console.WriteLine("Nb30 AtCmdXSoftUpdate v0.1");
      SerialPort serialPort = new SerialPort();
      serialPort.PortName = "COM30";
      serialPort.BaudRate = 115200;
      serialPort.DataBits = 8;
      serialPort.Parity = Parity.None;
      serialPort.StopBits = StopBits.One;
      serialPort.Handshake = Handshake.None;
      serialPort.ReadTimeout = 500;
      serialPort.WriteTimeout = 500;
      serialPort.Open();

      serialPort.WriteLine("AT+XSOFTUPDATE");
      Thread.Sleep(1000);
      string response = serialPort.ReadExisting();
      if (true == response.Contains("OK")) {
        Console.WriteLine("OK.Please wait device reboot!");
      } else {
        Console.WriteLine("ERROR");
      } 
      serialPort.Close();
    }
  }
}

日系机圈子里的,看到这个标题就应该明白这是干什么用的了吧?
说实话写的很烂,能凑合用的水平,也不知道那么多依赖的更新之后,还能不能用。
稍稍做了一些小修补,由于在国外也没有机器测试,还是希望有人可以来测一下。
这个小东西是很久以前的商用品了,现在也觉得没啥价值,故直接公开
想法及思路来自tewilove
依赖PyUSB

# NB30 SHDIAG USB Protocol v0.2
# (C)Yuu 2019
# Modified History:
# Nov.02-2022:v0.2:Normal Improvement

import sys
import usb.core
import usb.util
import random

NB30_USB_VID = 0x04dd
NB30_USB_PID = 0x933a


def shdiag_xchar(c):
    xft = [0, 2, 4, 6, 1, 3, 5, 7]
    r = 0
    for i in range(8):
        r |= (((c >> i) & 1) << xft[i])
    return r


def shdiag_encode_data(in_data):
    ip = 0
    op = 1
    out_data = [0] * (len(in_data) * 2)
    v = (random.randint(0, 0x7f) + 0xf)
    x = shdiag_xchar(v)
    out_data[0] = v
    while ip < len(in_data):
        if in_data[ip] == '\r' or in_data[ip] == '\n':
            out_data[op] = in_data[ip]
            op += 1
            if in_data[ip] == '\n' and ip + 1 < len(in_data):
                x = shdiag_xchar(v)
                out_data[op] = v
                op += 1
        else:
            out_data[op] = (in_data[ip] ^ (x & 0x7f)) + 0xf
            x = (x >> 1) | ((x & 1) << 7)
            op += 1
        ip += 1
    return out_data[:op]


def shdiag_decode_data(in_data):
    ip = 1
    op = 0
    out_data = [0] * (len(in_data) - 1)
    x = in_data[0]
    x = shdiag_xchar(x)
    while ip < len(in_data):
        if in_data[ip] == '\r' or in_data[ip] == '\n':
            out_data[op] = in_data[ip]
            op += 1
            if in_data[ip] == '\n' and ip + 1 < len(in_data):
                x = in_data[ip + 1]
                x = shdiag_xchar(x)
                ip += 1
        else:
            out_data[op] = (in_data[ip] - 0xf) ^ (x & 0x7f)
            x = (x >> 1) | ((x & 1) << 7)
            op += 1
        ip += 1
    return out_data[:op]


def nb30port_open():
    dev = usb.core.find(idVendor=NB30_USB_VID, idProduct=NB30_USB_PID)
    if dev is None:
        return None
    dev.set_configuration()
    cfg = dev.get_active_configuration()
    intf = cfg[(1, 1)]
    ep_in = usb.util.find_descriptor(intf, custom_match=lambda e: usb.util.endpoint_direction(
        e.bEndpointAddress) == usb.util.ENDPOINT_IN)
    ep_out = usb.util.find_descriptor(intf, custom_match=lambda e: usb.util.endpoint_direction(
        e.bEndpointAddress) == usb.util.ENDPOINT_OUT)
    return dev, ep_in, ep_out


def nb30port_close(dev):
    usb.util.dispose_resources(dev)


def nb30port_read(ep_in):
    return ep_in.read(ep_in.wMaxPacketSize, 1000)


def nb30port_write(ep_out, data):
    return ep_out.write(data, 1000)


def main():
    print("YuuSHDIAG v0.2")
    print("Usage: python shdiag.py <SHDIAG_COMMAND>")
    if len(sys.argv) != 2:
        return 1
    while True:
        dev, ep_in, ep_out = nb30port_open()
        if dev is not None:
            break
    res = nb30port_read(ep_in)
    if len(res) > 0:
        res_decode = shdiag_decode_data(res)
        print(''.join(map(chr, res_decode)))
    req = '{}\r\n'.format(sys.argv[1])
    req_encode = shdiag_encode_data(req)
    if 0 < len(req_encode) == nb30port_write(ep_out, req_encode):
        res = nb30port_read(ep_in)
        if len(res) > 0:
            res_decode = shdiag_decode_data(res)
            print(''.join(map(chr, res_decode)))
    nb30port_close(dev)


if __name__ == '__main__':
    main()