public void connect_device(string port) { foreach (Device d in devices) { if (port.Equals(d.get_port())) { this.connectedDevice=d; d.open_port(); break; } } }
// nalezne vsechna pripojena zarizeni a da je do listu public void find_devices() { devices.Clear(); numberOfPorts = 0; if (!connected) { serialPort = new SerialPort(); serialPort.ReadBufferSize = 128*1024; serialPort.BaudRate = 115200; this.connectingInProgress = true; this.error = 0; foreach (string s in SerialPort.GetPortNames()) { numberOfPorts++; } int counter = 0; foreach (string s in SerialPort.GetPortNames()) { counter++; progress = (counter * 100) / numberOfPorts; try { Thread.Yield(); serialPort.PortName = s; serialPort.Open(); serialPort.Write(Defines.IDNRequest); Thread.Sleep(250); char[] msg = new char[1024]; int toRead = serialPort.BytesToRead; serialPort.Read(msg, 0, toRead); string msgInput = ""; string procesor = ""; string deviceName = ""; string version = ""; int i=0; if (toRead > 4) { for (i = 0; i < 4; i++) { msgInput = msgInput + msg[i]; } } Thread.Yield(); string msgCompare = Defines.IDN; if (msgInput.Contains(msgCompare)) { if (toRead > 10) { while (msg[i] != ' ') { procesor = procesor + msg[i]; i++; } i++; while (msg[i] != 'V') { deviceName = deviceName + msg[i]; i++; } for (int j = 0; j < 4; j++) { version = version + msg[i]; i++; } } string portname = serialPort.PortName; int baud = serialPort.BaudRate; serialPort.Close(); serialPort.Dispose(); Device tmp = new Device(portname, deviceName, procesor, version,baud); devices.Add(tmp); } else { serialPort.Close(); } } catch (Exception ex) { if (serialPort.IsOpen) { serialPort.Close(); } Console.WriteLine(ex); } } newDevices = true; this.connectingInProgress = false; } }
public void disconnect_device() { this.connectedDevice.close_port(); this.connectedDevice = null; connected = false; }