Ejemplo n.º 1
0
        private void buttonGetMeasurements_Click(object sender, EventArgs e)
        {
#warning Update to use CommandFormerClass
            uartReceiver.Reset();
            processFunction = ProcessGetMeasurementInfo;
            // Send command to get measurement header
            List <byte> sb = new List <byte>();

            sb.Add(startSeq);
            sb.Add(deviceAddr);
            // Append message length
            sb.Add(0);
            sb.Add(9);
            // Append command
            sb.Add(commandRead);

            // Get and append address
            byte[] bAddress = CustomConvertorClass.ConvertIntTo4Bytes(MEASURE_INFO_BASE);
            sb.AddRange(bAddress);
            // Get and append length
            byte[] bLen = CustomConvertorClass.ConvertIntTo2Bytes(MEASURE_INFO_LEN);
            sb.AddRange(bLen);

            // Debug checksum test
            //sb.Add(0x1);
            //sb.Add(0x2);

            // Now calculate and append checksum
            UInt16 csum  = ChecksumClass.CalculateChecksum(sb.ToArray());
            byte[] bcsum = CustomConvertorClass.ConvertIntTo2BytesBE(csum);
            sb.AddRange(bcsum); // 2B


            serial.Write(sb.ToArray(), 0, sb.Count);
        }
Ejemplo n.º 2
0
        private void buttonRead_Click(object sender, EventArgs e)
        {
#warning Update to use CommandFormerClass
            uartReceiver.Reset();
            processFunction = DebugRead;

            List <byte> sb = new List <byte>();

            sb.Add(startSeq);
            sb.Add(deviceAddr);
            // Append message length, include shecksum
            sb.Add(0);
            sb.Add(9);
            // Append command
            sb.Add(commandRead); // 1B

            // Get and append address
            int    temp     = Convert.ToInt32(this.textBoxAddress.Text);
            byte[] bAddress = CustomConvertorClass.ConvertIntTo4Bytes(temp);
            sb.AddRange(bAddress); // 4B
            // Get and append length
            temp = Convert.ToInt32(this.textBoxLen.Text);
            byte[] bLen = CustomConvertorClass.ConvertIntTo2Bytes(temp);
            sb.AddRange(bLen); // 2B

            // Now calculate and append checksum
            UInt16 csum  = ChecksumClass.CalculateChecksum(sb.ToArray());
            byte[] bcsum = CustomConvertorClass.ConvertIntTo2BytesBE(csum);
            sb.AddRange(bcsum); // 2B

            serial.Write(sb.ToArray(), 0, sb.Count);
        }
Ejemplo n.º 3
0
        public void AppendSetCriticalHigh(float targetValue, byte channel)
        {
            const float delta = 100f;

            // targt value is given in mV
            if (targetValue < 0)
            {
                targetValue = 0 + delta;
            }

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

            // First apply gain
            targetValue /= FormDataPresenter.GAIN;
            // Now convert to uint16
            UInt16 uValue = (UInt16)(targetValue * 32768 / 3300);


            bl.Add(12);
            bl.AddRange(CustomConvertorClass.ConvertIntTo2Bytes(uValue));
            bl.Add(channel);
        }
Ejemplo n.º 4
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));
 }
Ejemplo n.º 5
0
        public void AppendWaitForValueFalling(byte channel, UInt16 latency, float targetValue)
        {
            const float delta = 100f;

            // targt value is given in mV
            if (targetValue < 0)
            {
                targetValue = 0 + delta;
            }

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

            // First apply gain
            targetValue /= FormDataPresenter.GAIN;
            // 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));
        }
Ejemplo n.º 6
0
        public byte[] GetFinalCommandList()
        {
            var num = bl.Count;

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

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

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

            return(bl.ToArray());
        }
Ejemplo n.º 7
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));
 }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        private void ReadAndDisplayData(int index)
        {
            MeasurementHeaderClass header;

            try
            {
                header = headers[index];
            }
            catch (Exception)
            {
                MessageBox.Show("Header not present");
                return;
            }


            uartReceiver.Reset();
            processFunction = DebugRead;
#warning Update to use CommandFormerClass
            List <byte> sb = new List <byte>();

            sb.Add(startSeq);
            sb.Add(deviceAddr);
            // Append message length, include checksum
            sb.Add(0);
            sb.Add(9);
            // Append command
            sb.Add(commandRead); //1

            // Append address
            byte[] bAddress = CustomConvertorClass.ConvertIntTo4Bytes(header.dataAddress); //5
            sb.AddRange(bAddress);
            // Get and append length
            byte[] bLen = CustomConvertorClass.ConvertIntTo2Bytes(header.numOfPoints * 2); //7      1 measure is 2 bytes
            sb.AddRange(bLen);

            // Now calculate and append checksum
            UInt16 csum  = ChecksumClass.CalculateChecksum(sb.ToArray());
            byte[] bcsum = CustomConvertorClass.ConvertIntTo2BytesBE(csum);
            sb.AddRange(bcsum); // 2B

            serial.Write(sb.ToArray(), 0, sb.Count);
        }
Ejemplo n.º 10
0
        private void buttonErase_Click(object sender, EventArgs e)
        {
            // Send command to erase measurement header
            List <byte> sb = new List <byte>();

            sb.Add(startSeq);
            sb.Add(deviceAddr);
            // Append message length, and include checksum
            sb.Add(0);
            sb.Add(3);
            // Append command
            sb.Add(commandErase);

            // Now calculate and append checksum
            UInt16 csum = ChecksumClass.CalculateChecksum(sb.ToArray());

            byte[] bcsum = CustomConvertorClass.ConvertIntTo2BytesBE(csum);
            sb.AddRange(bcsum); // 2B

            serial.Write(sb.ToArray(), 0, sb.Count);
        }
Ejemplo n.º 11
0
        public void AppendWaitForValueRising(byte channel, UInt16 latency, float targetValue)
        {
            const float delta = 100f;

            // targt value is given in mV
            if (targetValue < 0)
            {
                targetValue = 0 + delta;
            }

            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
             */
            // First apply gain
            targetValue /= FormDataPresenter.GAIN;
            // 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));
        }
        public bool CollectData(byte[] data)
        {
            switch (state)
            {
            case 0:
                // First time
                fragmentedData.AddRange(data);
                if (fragmentedData.Count > 2)
                {
                    // We can extract data length
                    //dataLen = CustomConvertorClass.ConvertRawStringToInt(fragmentedData.Substring(0, 2)); // OLD WAY
                    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 = ConvertStringToByte(fragmentedData); // OLD WAY
                        bData = fragmentedData.ToArray();
                        //Console.WriteLine("case 0 uart: Received " + bData.Length);
                        return(true);
                    }

                    state = 2;
                }
                else
                {
                    // We received just first char, we need one more to form dataLen
                    state = 1;
                }
                break;

            case 1:
                fragmentedData.AddRange(data);
                //dataLen = CustomConvertorClass.ConvertRawStringToInt(fragmentedData.Substring(0, 2)); // OLD WAY
                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;     //DEBUG
                    //bData = ConvertStringToByte(fragmentedData); // OLD WAY
                    bData = fragmentedData.ToArray();
                    //Console.WriteLine("case 1 uart: Received " + bData.Length);
                    foreach (var item in bData)
                    {
                        Console.WriteLine(item.ToString());
                    }
                    return(true);
                }
                state = 2;
                break;

            case 2:
                fragmentedData.AddRange(data);
                if (dataLen + 2 <= fragmentedData.Count)
                {
                    // Everything arrived at once
                    state = 0;     //DEBUG
                    bData = fragmentedData.ToArray();
                    //Console.WriteLine("case 2 uart: Received " + bData.Length);
                    if (bData.Length > 200)
                    {
                        Console.WriteLine("");
                    }
                    return(true);
                }
                break;

            default:
                break;
            }
            return(false);

            //if (totalDataReceived == 0)
            //{
            //    fragmentedData = data;
            //}
        }
Ejemplo n.º 13
0
        private void FetchHeaders(byte[] b)
        {
            // Check checksum and remove from message
            ChecksumClass cs = new ChecksumClass();

            if (cs.VerifyChecksumFromReceivedMessage(b))
            {
                Console.WriteLine("CHECKSUM MATCH");
            }
            else
            {
                Console.WriteLine("CHECKSUM DONT MATCH");
                return;
            }

            var newArr = UARTHelperClass.RemoveChecksumFromMessage(b);


            ByteArrayDecoderClass decoder = new ByteArrayDecoderClass(newArr);

            decoder.Get2BytesAsInt(); // Remove first 2 values (number of data transmited)

            int[] timestamp     = decoder.Get6BytesAsTimestamp();
            int   prescaler     = decoder.Get4BytesAsInt();
            int   numOfPoints   = decoder.Get2BytesAsInt();
            int   operatingMode = decoder.Get1ByteAsInt();
            int   channelMode   = decoder.Get1ByteAsInt();

            // Fill header with remaining data
            headers[currentHeader].timestamp     = timestamp;
            headers[currentHeader].prescaler     = prescaler;
            headers[currentHeader].numOfPoints   = numOfPoints;
            headers[currentHeader].operatingMode = operatingMode;
            headers[currentHeader].channel       = channelMode;
            headers[currentHeader].ready         = true;
            // Fill labels
            FillLabelGroup(currentHeader, headers[currentHeader].headerAddress, timestamp, prescaler, numOfPoints, operatingMode, channelMode);

            uartReceiver.Reset();

            currentHeader++;
            if (currentHeader == headers.Count) // Last time in here, reset everything
            {
                processFunction = Dummy;
                return;
            }
#warning Update to use CommandFormerClass
            // Send command to get measurement header
            List <byte> sb = new List <byte>();

            sb.Add(startSeq);
            sb.Add(deviceAddr);
            // Append message length and include checksum
            sb.Add(0);
            sb.Add(9);
            // Append command
            sb.Add(commandRead);

            // Get and append address
            byte[] bAddress = CustomConvertorClass.ConvertIntTo4Bytes(headers[currentHeader].headerAddress);
            sb.AddRange(bAddress);
            // Get and append length
            byte[] bLen = CustomConvertorClass.ConvertIntTo2Bytes(HEADER_LENGTH);
            sb.AddRange(bLen);
            Thread.Sleep(50);
            //Console.WriteLine("FetchHeaders -- sending " + sb.Count);

            // Now calculate and append checksum
            UInt16 csum  = ChecksumClass.CalculateChecksum(sb.ToArray());
            byte[] bcsum = CustomConvertorClass.ConvertIntTo2BytesBE(csum);
            sb.AddRange(bcsum); // 2B

            serial.Write(sb.ToArray(), 0, sb.Count);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Measurement info is received, decode data and send requests to read measurement headers
        /// </summary>
        /// <param name="b"></param>
        private void ProcessGetMeasurementInfo(byte[] b)
        {
            // Check checksum and remove from message
            ChecksumClass cs = new ChecksumClass();

            if (cs.VerifyChecksumFromReceivedMessage(b))
            {
                Console.WriteLine("CHECKSUM MATCH");
            }
            else
            {
                Console.WriteLine("CHECKSUM DONT MATCH");
                return;
            }

            var newArr = UARTHelperClass.RemoveChecksumFromMessage(b);


            ByteArrayDecoderClass decoder = new ByteArrayDecoderClass(newArr);

            decoder.Get2BytesAsInt(); // Remove first 2 values (number of data transmited)
            EraseLabels();
            // Get number of measurements and their header starting addresses
            int numOfMeasurements = decoder.Get2BytesAsInt();

            Console.WriteLine("Number of measurements: " + numOfMeasurements);
            if (numOfMeasurements == 0)
            {
                // if no measurements leave it as it is
                processFunction = Dummy;
                return;
            }
            headers = new List <MeasurementHeaderClass>(); // Reset headers list
            for (int i = 0; i < numOfMeasurements; i++)
            {
                headers.Add(new MeasurementHeaderClass(decoder.Get4BytesAsInt()));
            }

            // Reset UART receiver and set new method for data processing
            uartReceiver.Reset();
            processFunction = FetchHeaders;
            currentHeader   = 0; // Reset header index


            // Bootstrap
#warning Update to use CommandFormerClass
            // Send command to get measurement header
            List <byte> sb = new List <byte>();

            sb.Add(startSeq);
            sb.Add(deviceAddr);
            // Append message length
            sb.Add(0);
            sb.Add(9);
            // Append command
            sb.Add(commandRead);

            // Get and append address
            byte[] bAddress = CustomConvertorClass.ConvertIntTo4Bytes(headers[0].headerAddress);
            sb.AddRange(bAddress);
            // Get and append length
            byte[] bLen = CustomConvertorClass.ConvertIntTo2Bytes(HEADER_LENGTH);
            sb.AddRange(bLen);
            //Thread.Sleep(200);
            //Console.WriteLine("ProcessGetMeasurementInfo --sending " + sb.Count);

            // Now calculate and append checksum
            UInt16 csum  = ChecksumClass.CalculateChecksum(sb.ToArray());
            byte[] bcsum = CustomConvertorClass.ConvertIntTo2BytesBE(csum);
            sb.AddRange(bcsum); // 2B

            serial.Write(sb.ToArray(), 0, sb.Count);
        }
Ejemplo n.º 15
0
 public void AppendWaitForMs(UInt32 ms)
 {
     bl.Add(3);
     bl.AddRange(CustomConvertorClass.ConvertIntTo4Bytes(ms));
 }
Ejemplo n.º 16
0
 public void AppendSetCriticalHigh(UInt16 targetValue, byte channel)
 {
     bl.Add(12);
     bl.AddRange(CustomConvertorClass.ConvertIntTo2Bytes(targetValue));
     bl.Add(channel);
 }