public void Open()
 {
     Ftd2xx.FT_Open(_portIndex, out _handle);
     Ftd2xx.FT_SetBaudRate(_handle, 19200);
     Ftd2xx.FT_SetDataCharacteristics(_handle, 8, 1, 0);
     Ftd2xx.FT_SetFlowControl(_handle, Ftd2xx.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 0x13);
     Ftd2xx.FT_SetTimeouts(_handle, 1000, 1000);
     Ftd2xx.FT_Purge(_handle, Ftd2xx.FT_PURGE.FT_PURGE_ALL);
 }
        public sealed override void Update()
        {
            while (Ftd2xx.BytesToRead(_handle) >= 285)
            {
                ReadData();
            }

            if (Ftd2xx.BytesToRead(_handle) == 1)
            {
                Ftd2xx.ReadByte(_handle);
            }

            Ftd2xx.Write(_handle, new byte[] { 0x38 });
            _alternativeRequest.BeginInvoke(null, null);
        }
        public TBalancerGroup(ISettings settings)
        {
            uint numDevices;

            try
            {
                if (Ftd2xx.FT_CreateDeviceInfoList(out numDevices) != Ftd2xx.FT_STATUS.FT_OK)
                {
                    _report.AppendLine("Status: FT_CreateDeviceInfoList failed");
                    return;
                }
            }
            catch (DllNotFoundException)
            {
                return;
            }
            catch (ArgumentNullException)
            {
                return;
            }
            catch (EntryPointNotFoundException)
            {
                return;
            }
            catch (BadImageFormatException)
            {
                return;
            }

            Ftd2xx.FT_DEVICE_INFO_NODE[] info = new Ftd2xx.FT_DEVICE_INFO_NODE[numDevices];
            if (Ftd2xx.FT_GetDeviceInfoList(info, ref numDevices) != Ftd2xx.FT_STATUS.FT_OK)
            {
                _report.AppendLine("Status: FT_GetDeviceInfoList failed");
                return;
            }

            // make sure numDevices is not larger than the info array
            if (numDevices > info.Length)
            {
                numDevices = (uint)info.Length;
            }

            for (int i = 0; i < numDevices; i++)
            {
                _report.Append("Device Index: ");
                _report.AppendLine(i.ToString(CultureInfo.InvariantCulture));
                _report.Append("Device Type: ");
                _report.AppendLine(info[i].Type.ToString());

                // the T-Balancer always uses an FT232BM
                if (info[i].Type != Ftd2xx.FT_DEVICE.FT_DEVICE_232BM)
                {
                    _report.AppendLine("Status: Wrong device type");
                    continue;
                }

                Ftd2xx.FT_STATUS status = Ftd2xx.FT_Open(i, out Ftd2xx.FT_HANDLE handle);
                if (status != Ftd2xx.FT_STATUS.FT_OK)
                {
                    _report.AppendLine("Open Status: " + status);
                    continue;
                }

                Ftd2xx.FT_SetBaudRate(handle, 19200);
                Ftd2xx.FT_SetDataCharacteristics(handle, 8, 1, 0);
                Ftd2xx.FT_SetFlowControl(handle, Ftd2xx.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 0x13);
                Ftd2xx.FT_SetTimeouts(handle, 1000, 1000);
                Ftd2xx.FT_Purge(handle, Ftd2xx.FT_PURGE.FT_PURGE_ALL);

                status = Ftd2xx.Write(handle, new byte[] { 0x38 });
                if (status != Ftd2xx.FT_STATUS.FT_OK)
                {
                    _report.AppendLine("Write Status: " + status);
                    Ftd2xx.FT_Close(handle);
                    continue;
                }

                bool isValid         = false;
                byte protocolVersion = 0;

                int j = 0;
                while (Ftd2xx.BytesToRead(handle) == 0 && j < 2)
                {
                    Thread.Sleep(100);
                    j++;
                }

                if (Ftd2xx.BytesToRead(handle) > 0)
                {
                    if (Ftd2xx.ReadByte(handle) == TBalancer.StartFlag)
                    {
                        while (Ftd2xx.BytesToRead(handle) < 284 && j < 5)
                        {
                            Thread.Sleep(100);
                            j++;
                        }

                        int length = Ftd2xx.BytesToRead(handle);
                        if (length >= 284)
                        {
                            byte[] data = new byte[285];
                            data[0] = TBalancer.StartFlag;
                            for (int k = 1; k < data.Length; k++)
                            {
                                data[k] = Ftd2xx.ReadByte(handle);
                            }

                            // check protocol version 2X (protocols seen: 2C, 2A, 28)
                            isValid         = (data[274] & 0xF0) == 0x20;
                            protocolVersion = data[274];
                            if (!isValid)
                            {
                                _report.Append("Status: Wrong Protocol Version: 0x");
                                _report.AppendLine(protocolVersion.ToString("X", CultureInfo.InvariantCulture));
                            }
                        }
                        else
                        {
                            _report.AppendLine("Status: Wrong Message Length: " + length);
                        }
                    }
                    else
                    {
                        _report.AppendLine("Status: Wrong Startflag");
                    }
                }
                else
                {
                    _report.AppendLine("Status: No Response");
                }

                Ftd2xx.FT_Purge(handle, Ftd2xx.FT_PURGE.FT_PURGE_ALL);
                Ftd2xx.FT_Close(handle);

                if (isValid)
                {
                    _report.AppendLine("Status: OK");
                    _hardware.Add(new TBalancer(i, protocolVersion, settings));
                }

                if (i < numDevices - 1)
                {
                    _report.AppendLine();
                }
            }
        }
 public override void Close()
 {
     Ftd2xx.FT_Close(_handle);
     base.Close();
 }
 private void DelayedAlternativeRequest()
 {
     System.Threading.Thread.Sleep(500);
     Ftd2xx.Write(_handle, new byte[] { 0x37 });
 }
        private void ReadData()
        {
            Ftd2xx.Read(_handle, _data);
            if (_data[0] != StartFlag)
            {
                Ftd2xx.FT_Purge(_handle, Ftd2xx.FT_PURGE.FT_PURGE_RX);
                return;
            }

            if (_data[1] == 255 || _data[1] == 88)
            {
                // bigNG

                if (_data[274] != _protocolVersion)
                {
                    return;
                }


                if (_primaryData.Length == 0)
                {
                    _primaryData = new byte[_data.Length];
                }

                _data.CopyTo(_primaryData, 0);

                for (int i = 0; i < _digitalTemperatures.Length; i++)
                {
                    if (_data[238 + i] > 0)
                    {
                        _digitalTemperatures[i].Value = 0.5f * _data[238 + i] + _digitalTemperatures[i].Parameters[0].Value;
                        ActivateSensor(_digitalTemperatures[i]);
                    }
                    else
                    {
                        DeactivateSensor(_digitalTemperatures[i]);
                    }
                }

                for (int i = 0; i < _analogTemperatures.Length; i++)
                {
                    if (_data[260 + i] > 0)
                    {
                        _analogTemperatures[i].Value = 0.5f * _data[260 + i] + _analogTemperatures[i].Parameters[0].Value;
                        ActivateSensor(_analogTemperatures[i]);
                    }
                    else
                    {
                        DeactivateSensor(_analogTemperatures[i]);
                    }
                }

                for (int i = 0; i < _sensorHubTemperatures.Length; i++)
                {
                    if (_data[246 + i] > 0)
                    {
                        _sensorHubTemperatures[i].Value = 0.5f * _data[246 + i] + _sensorHubTemperatures[i].Parameters[0].Value;
                        ActivateSensor(_sensorHubTemperatures[i]);
                    }
                    else
                    {
                        DeactivateSensor(_sensorHubTemperatures[i]);
                    }
                }

                for (int i = 0; i < _sensorHubFlows.Length; i++)
                {
                    if (_data[231 + i] > 0 && _data[234] > 0)
                    {
                        float pulsesPerSecond = (_data[231 + i] * 4.0f) / _data[234];
                        float pulsesPerLiter  = _sensorHubFlows[i].Parameters[0].Value;
                        _sensorHubFlows[i].Value = pulsesPerSecond * 3600 / pulsesPerLiter;
                        ActivateSensor(_sensorHubFlows[i]);
                    }
                    else
                    {
                        DeactivateSensor(_sensorHubFlows[i]);
                    }
                }

                for (int i = 0; i < _fans.Length; i++)
                {
                    float maxRpm = 11.5f * ((_data[149 + 2 * i] << 8) | _data[148 + 2 * i]);

                    if (_fans[i] == null)
                    {
                        _fans[i] = new Sensor("Fan Channel " + i,
                                              i,
                                              SensorType.Fan,
                                              this,
                                              new[]
                        {
                            new ParameterDescription("MaxRPM",
                                                     "Maximum revolutions per minute (RPM) of the fan.",
                                                     maxRpm)
                        },
                                              _settings);
                    }

                    float value;
                    if ((_data[136] & (1 << i)) == 0) // pwm mode
                    {
                        value = 0.02f * _data[137 + i];
                    }
                    else // analog mode
                    {
                        value = 0.01f * _data[141 + i];
                    }

                    _fans[i].Value = _fans[i].Parameters[0].Value * value;
                    ActivateSensor(_fans[i]);

                    _controls[i].Value = 100 * value;
                    ActivateSensor(_controls[i]);
                }
            }
            else if (_data[1] == 253)
            {
                // miniNG #1
                if (_alternativeData.Length == 0)
                {
                    _alternativeData = new byte[_data.Length];
                }

                _data.CopyTo(_alternativeData, 0);

                ReadMiniNg(0);
                if (_data[66] == 253) // miniNG #2
                {
                    ReadMiniNg(1);
                }
            }
        }