Beispiel #1
0
        public void run()
        {
            while (run_th)
            {
                if (comms_q.Count > 0)
                {
                    messg = comms_q.Dequeue();
                    switch (messg.GetRequest())
                    {
                    case Message.MsgRequest.FIND_DEVICES:
                        clearListOfDevices();
                        find_devices(921600, 1, 0);      // 921600
                        break;

                    case Message.MsgRequest.CONNECT_DEVICE:
                        connect_device(messg.GetMessage());
                        break;
                    }
                }
                if (connectedDevice != null && connectedDevice.isPortError())
                {
                    commState = CommsStates.ERROR;
                    disconnect_device();
                }
                Thread.Sleep(10);
            }
        }
Beispiel #2
0
 public void disconnect_device()
 {
     connectedDevice.close_gen();
     connectedDevice.close_logAnlys();
     connectedDevice.close_counter();
     connectedDevice.close_syncPwm_gen();
     connectedDevice.close_scope();
     connectedDevice.close_volt();
     connectedDevice.close_source();
     connectedDevice.close_port();
     this.commState = CommsStates.DISCONNECTED;
 }
Beispiel #3
0
        public string[] get_dev_names()
        {
            string[] result = new string[devices.Count()];
            newDevices = false;
            int i = 0;

            foreach (Device d in devices)
            {
                result[i] = d.get_port() + ": " + d.get_name();
                i++;
            }
            this.commState = CommsStates.DEVICES_READ; //access from different thread (can cause some issue:( )
            return(result);
        }
Beispiel #4
0
        public void connect_device(string port)
        {
            this.commState = CommsStates.CONNECTING;
            foreach (Device d in devices)
            {
                if (port.Equals(d.get_port()))
                {
                    this.connectedDevice = d;
                    break;
                }
            }

            if (connectedDevice.open_port())
            {
                this.commState = CommsStates.CONNECTED;
            }
            else {
                this.commState = CommsStates.ERROR;
            }
        }
Beispiel #5
0
        public void connect_device(string port)
        {
            this.commState = CommsStates.CONNECTING;
            foreach (Device d in devices)
            {
                if (port.Equals(d.get_port()))
                {
                    this.connectedDevice = d;
                    break;
                }
            }

            if (connectedDevice.open_port())
            {
                this.commState = CommsStates.CONNECTED;
            }
            else
            {
                this.commState = CommsStates.ERROR;
            }
        }
Beispiel #6
0
        // nalezne vsechna pripojena zarizeni a da je do listu
        public void find_devices(int speed, double mull, double offset)
        {
            this.commState = CommsStates.FINDING;

            numberOfPorts = 0;

            if (!connected)
            {
                serialPort = new SerialPort();
                serialPort.ReadBufferSize = 128 * 1024;
                serialPort.BaudRate       = speed;

                string[] devList       = SerialPort.GetPortNames();
                string[] uniqueDevList = devList.Distinct().ToArray();

                foreach (string s in uniqueDevList)
                {
                    numberOfPorts++;
                }

                int counter = 0;
                foreach (string serial in uniqueDevList)
                {
                    counter++;
                    progress = (int)((double)counter * 100 / numberOfPorts * mull + offset * 100);
                    try
                    {
                        Thread.Yield();
                        serialPort.PortName     = serial;
                        serialPort.ReadTimeout  = 5000;
                        serialPort.WriteTimeout = 1000;
                        serialPort.Open();
                        serialPort.Write(Commands.RESET_DEVICE + ";");
                        Thread.Sleep(50);
                        serialPort.DiscardInBuffer();
                        serialPort.Write(Commands.IDNRequest + ";");
                        Thread.Sleep(200);

                        char[] msg    = new char[256];
                        int    toRead = serialPort.BytesToRead;

                        serialPort.Read(msg, 0, toRead);
                        string msgInput   = new string(msg, 0, 4);
                        string deviceName = new string(msg, 4, toRead - 8);

                        Thread.Yield();
                        if (msgInput.Equals(Commands.SYSTEM) && deviceName.Length > 8)
                        {
                            bool   inList    = false;
                            Device newDevice = new Device(serialPort.PortName, deviceName, serialPort.BaudRate);
                            foreach (var item in devices)
                            {
                                if (item.Equals(newDevice))
                                {
                                    inList = true;
                                }
                            }
                            if (!inList)
                            {
                                devices.Add(new Device(serialPort.PortName, deviceName, serialPort.BaudRate));
                            }
                        }
                        serialPort.Close();
                        serialPort.Dispose();
                    }
                    catch (Exception ex)
                    {
                        if (serialPort.IsOpen)
                        {
                            serialPort.Close();
                        }
                        Console.WriteLine(ex);
                    }
                }
                if (devices.Count > 0)
                {
                    newDevices     = true;
                    this.commState = CommsStates.FOUND_DEVS;
                }
                else
                {
                    this.commState = CommsStates.NO_DEV_FOUND;
                }
            }
        }
Beispiel #7
0
        // nalezne vsechna pripojena zarizeni a da je do listu
        public void find_devices()
        {
            this.commState = CommsStates.FINDING;
            devices.Clear();

            numberOfPorts = 0;

            if (!connected)
            {
                serialPort = new SerialPort();
                serialPort.ReadBufferSize = 128 * 1024;
                serialPort.BaudRate = 115200;

                foreach (string s in SerialPort.GetPortNames())
                {
                    numberOfPorts++;
                }

                int counter = 0;
                foreach (string serial in SerialPort.GetPortNames())
                {
                    counter++;
                    progress = (counter * 100) / numberOfPorts;
                    try
                    {
                        Thread.Yield();
                        serialPort.PortName = serial;
                        serialPort.ReadTimeout = 5000;
                        serialPort.WriteTimeout = 1000;
                        serialPort.Open();
                        serialPort.Write(Commands.IDNRequest+";");
                        Thread.Sleep(250);

                        char[] msg = new char[256];
                        int toRead = serialPort.BytesToRead;

                        serialPort.Read(msg, 0, toRead);
                        string msgInput = new string(msg, 0, 4);
                        string deviceName = new string(msg, 4, toRead - 4);

                        Thread.Yield();
                        if (msgInput.Equals(Commands.ACKNOWLEDGE))
                        {
                            devices.Add(new Device(serialPort.PortName, deviceName, serialPort.BaudRate));
                        }
                        serialPort.Close();
                        serialPort.Dispose();
                    }
                    catch (Exception ex)
                    {
                        if (serialPort.IsOpen)
                        {
                            serialPort.Close();
                        }
                        Console.WriteLine(ex);
                    }
                }
                if (devices.Count > 0)
                {
                    newDevices = true;
                    this.commState = CommsStates.FOUND_DEVS;
                }
                else {
                    this.commState = CommsStates.NO_DEV_FOUND;
                }

            }
        }
Beispiel #8
0
 public void disconnect_device()
 {
     connectedDevice.close_scope();
     connectedDevice.close_port();
     this.commState = CommsStates.DISCONNECTED;
 }
Beispiel #9
0
 public string[] get_dev_names()
 {
     string[] result = new string[devices.Count()];
     newDevices = false;
     int i = 0;
     foreach (Device d in devices)
     {
         result[i] = d.get_port()+":"+d.get_name();
         i++;
     }
     this.commState = CommsStates.DEVICES_READ; //access from different thread (can cause some issue:( )
     return result;
 }