private bool TryOpenUsb(string fileName) { try { _UsbApi = new UsbApi(fileName); ExecuteCommand(ECommand.位); //Query(); return(true); } catch { return(false); } }
public Usb() { foreach (var item in new string[] { "AP", "DB", "RS", "FC7", "FC4", "FC8" }) { var data = new byte[item.Length + 7]; data[0] = 0x02; //stx data[1] = 0x30; //addr H data[2] = 0x30; //addr L data[3] = 0x00; //len H data[4] = (byte)item.Length; //len L int index = 5; foreach (var c in item) { data[index++] = (byte)c; } data[index++] = 0x03; //etx data[index] = 0x02; //校验码 for (int i = 1; i < data.Length - 1; i++) { data[index] ^= data[i]; } _CommandData.Add(data); } var fileName = Settings.Default.UsbFileName; if (!string.IsNullOrEmpty(fileName) && TryOpenUsb(fileName)) { return; } foreach (var fn in UsbApi.GetUsbFileNames()) { if (TryOpenUsb(fn)) { Settings.Default.UsbFileName = fn; Settings.Default.Save(); return; } } _UsbApi = null; }