Exemple #1
0
 public void AppendWaitForValueRising(byte channel, UInt16 latency, UInt16 targetValue)
 {
     bl.Add(9);
     bl.Add(channel);
     bl.AddRange(CustomConvertorClass.ConvertIntTo2Bytes(latency));
     bl.AddRange(CustomConvertorClass.ConvertIntTo2Bytes(targetValue));
 }
Exemple #2
0
        public void AppendWaitForValueRising(byte channel, UInt16 latency, float targetValue)
        {
            // targt value is given in mV
            if (targetValue < 0)
            {
                targetValue = 0;
            }

            if (targetValue > 3300)
            {
                targetValue = 3300 - delta;
            }

            /*
             * maxValue = 32767
             *
             *
             * signed int offsetCorrectedValue = (signed int)rawData - SDOffset;
             * signed int offsetCorrectedValue = (signed int)rawData;
             * Now gain correction
             * float offsetAndGainValue = (float)offsetCorrectedValue * SDGain;
             * ADC is 15bits plus sign bit which are 32767 different values
             * currentValue/maxPossibleValue = x/3300  in mV
             * float fValue = offsetAndGainValue*3300/32768;
             *
             * offsetAndGainValue = fValue * 32768 / 3300
             */
            // Now convert to uint16
            UInt16 uValue = (UInt16)(targetValue * 32768 / 3300);

            bl.Add(9);
            bl.Add(channel);
            bl.AddRange(CustomConvertorClass.ConvertIntTo2Bytes(latency));
            bl.AddRange(CustomConvertorClass.ConvertIntTo2Bytes(uValue));
        }
Exemple #3
0
 public void AppendReadFromAddress(int address, int numOfBytes)
 {
     bl.Add(6);
     // Append address
     byte[] bAddress = CustomConvertorClass.ConvertIntTo4Bytes(address);
     bl.AddRange(bAddress);
     // Get and append length
     byte[] bLen = CustomConvertorClass.ConvertIntTo2Bytes(numOfBytes);
     bl.AddRange(bLen);
 }
Exemple #4
0
        /// <summary>
        /// Calculate checksum, and append it at the end of message
        /// </summary>
        /// <returns></returns>
        public byte[] GetFinalCommandList()
        {
            var num = bl.Count;

            // First 2 bytes are skipped but checksum should be counted so overall length is unchanged, +2 - 2 = 0
            bl.InsertRange(2, CustomConvertorClass.ConvertIntTo2Bytes(num));

            UInt16 csum = ChecksumClass.CalculateChecksum(bl.ToArray());

            bl.AddRange(CustomConvertorClass.ConvertIntTo2BytesBE(csum));

            return(bl.ToArray());
        }
Exemple #5
0
 /// <summary>
 ///  Data recorder task
 /// </summary>
 /// <param name="channel"> CH0 = 0 , CH1 = 1, CH0 & CH1 = 2</param>
 /// <param name="opMode"> 1- continuous , 2 - target points </param>
 /// <param name="prescaler"> (prescaler - 1) * 10ms</param>
 /// <param name="targetPoints"> How much points should be recorder (not used in continuous mode)</param>
 public void AppendDataRecorderTask(byte channel, byte opMode, UInt32 prescaler, UInt32 targetPoints, DateTime time)
 {
     bl.Add(4);
     bl.Add(channel);
     bl.Add(opMode);
     bl.AddRange(CustomConvertorClass.ConvertIntTo4Bytes(prescaler));
     bl.AddRange(CustomConvertorClass.ConvertIntTo4Bytes(targetPoints));
     // Now append transformed time
     bl.Add((byte)(time.Year - 2000));
     bl.Add((byte)(time.Month));
     bl.Add((byte)(time.Day));
     bl.Add((byte)(time.Hour));
     bl.Add((byte)(time.Minute));
     bl.Add((byte)(time.Second));
 }
        /// <summary>
        /// Remove checksum from data packet, and updates data length accordingly
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static byte[] RemoveChecksumFromMessage(byte[] msg)
        {
            // Copy whole array except last two bytes
            byte[] newMsg = new byte[msg.Length - 2];

            for (int i = 0; i < newMsg.Length; i++)
            {
                newMsg[i] = msg[i];
            }

            // Remove checksum count from message header
            int len = newMsg.Length - 2; // number of data bytes

            byte[] temp = CustomConvertorClass.ConvertIntTo2Bytes(len);
            newMsg[0] = temp[1];
            newMsg[1] = temp[0];

            return(newMsg);
        }
Exemple #7
0
        public void SetCutoffValueCH1(float targetValue)
        {
            // targt value is given in mV

            if (targetValue > 3300)
            {
                targetValue = 3300 - delta;
            }
            else if (targetValue < 0)
            {
                targetValue = 0;
            }
            targetValue = targetValue * 32768 / 3300;
            // Now convert to uint16
            UInt16 uValue = (UInt16)(targetValue);

            bl.Add(31);
            bl.AddRange(CustomConvertorClass.ConvertIntTo2Bytes(uValue));
        }
Exemple #8
0
        public void AppendSetCriticalLow(float targetValue, byte channel)
        {
            // targt value is given in mV
            if (targetValue > 3300)
            {
                targetValue = 3300 - delta;
            }
            else if (targetValue < 0)
            {
                targetValue = 0;
            }

            // Now convert to uint16
            UInt16 uValue = (UInt16)(targetValue * 32768 / 3300);


            bl.Add(11);
            bl.AddRange(CustomConvertorClass.ConvertIntTo2Bytes(uValue));
            bl.Add(channel);
        }
Exemple #9
0
        public void AppendWaitForValueFalling(byte channel, UInt16 latency, float targetValue)
        {
            // targt value is given in mV
            if (targetValue < 0)
            {
                targetValue = 0;
            }

            if (targetValue > 3300)
            {
                targetValue = 3300 - delta;
            }

            // Now convert to uint16
            UInt16 uValue = (UInt16)(targetValue * 32768 / 3300);

            bl.Add(10);
            bl.Add(channel);
            bl.AddRange(CustomConvertorClass.ConvertIntTo2Bytes(latency));
            bl.AddRange(CustomConvertorClass.ConvertIntTo2Bytes(uValue));
        }
Exemple #10
0
        /// <summary>
        /// Collects all raw data from serial port and store them in internal buffer until whole message is received (data need to be sent in custom protocol)
        /// </summary>
        /// <param name="data">Received data fragment</param>
        /// <returns>Result</returns>
        public UARTResult CollectData(byte[] data)
        {
            switch (state)
            {
            case 0:
                // First time, try to parse data len that will be received
                fragmentedData.AddRange(data);
                if (fragmentedData.Count > 2)
                {
                    // We can extract data length
                    dataLen = CustomConvertorClass.Decode2BytesToInt(data, 0);
                    // Check if we received all data at once
                    if (dataLen + 2 <= fragmentedData.Count)
                    {
                        // Everything arrived at once
                        state = 0;     //DEBUG
                        bData = fragmentedData.ToArray();
                        return(UARTResult.Done);
                    }

                    state = 2;
                }
                // We received just first char, we need one more to form dataLen
                state = 1;
                return(UARTResult.WaitMoreData);

            case 1:
                // We received enough data to infer total length, check if we received all data too
                fragmentedData.AddRange(data);
                dataLen = CustomConvertorClass.Decode2BytesToInt(fragmentedData.ToArray(), 0);
                // Check if we received all data at once
                if (dataLen + 2 <= fragmentedData.Count)
                {
                    // Everything arrived at once
                    state = 0;
                    bData = fragmentedData.ToArray();
                    return(UARTResult.Done);;
                }
                state = 2;
                return(UARTResult.WaitMoreData);

            case 2:
                fragmentedData.AddRange(data);
                if (dataLen + 2 == fragmentedData.Count)
                {
                    // Everything arrived at once
                    state = 0;
                    bData = fragmentedData.ToArray();
                    return(UARTResult.Done);
                }
                else if (fragmentedData.Count > dataLen + 2)
                {
                    throw new Exception("MORE DATA RECEIVED!!!");
                }
                return(UARTResult.WaitMoreData);

            default:
                break;
            }
            return(UARTResult.Error);
        }
Exemple #11
0
 public void AppendWaitForMs(UInt32 ms)
 {
     bl.Add(3);
     bl.AddRange(CustomConvertorClass.ConvertIntTo4Bytes(ms));
 }
Exemple #12
0
 public void SetCutoffValueCH1(ushort uValue)
 {
     bl.Add(31);
     bl.AddRange(CustomConvertorClass.ConvertIntTo2Bytes(uValue));
 }
Exemple #13
0
 public void AppendSetCriticalHigh(UInt16 targetValue, byte channel)
 {
     bl.Add(12);
     bl.AddRange(CustomConvertorClass.ConvertIntTo2Bytes(targetValue));
     bl.Add(channel);
 }