Click here to Skip to main content
15,880,651 members
Home / Discussions / C#
   

C#

 
SuggestionMessage Removed Pin
10-Feb-18 9:12
Markhoernchen10-Feb-18 9:12 
GeneralMessage Removed Pin
10-Feb-18 9:59
mveRichard MacCutchan10-Feb-18 9:59 
AnswerMessage Removed Pin
10-Feb-18 11:28
Markhoernchen10-Feb-18 11:28 
QuestionError with Primary key in Sql database Pin
Member 1367168610-Feb-18 7:32
Member 1367168610-Feb-18 7:32 
AnswerRe: Error with Primary key in Sql database Pin
Wendelius10-Feb-18 8:27
mentorWendelius10-Feb-18 8:27 
QuestionSplit a String of numbers in textfield Pin
auting829-Feb-18 11:06
auting829-Feb-18 11:06 
AnswerRe: Split a String of numbers in textfield Pin
Luc Pattyn9-Feb-18 16:22
sitebuilderLuc Pattyn9-Feb-18 16:22 
GeneralRe: Split a String of numbers in textfield Pin
auting8210-Feb-18 8:03
auting8210-Feb-18 8:03 
Hi, thanks for the advice.I am new to programming and as you can see don't have too much experience.
I apologize if my questions seem "stupid".
Back to this problem, I managed to "solve" the problem but I don't think the solution is good even though it works when I run the program.
The instructor of this class I am taking told us to use for loops to solve this problem of getting sensor values and ID numbers of the sensors. I did not do that.
I get completely lost when I need to create an array for sensor values and match it with sensor IDs.

Another thing I am struggling with is how to compare dates. I have a time stamp that indicates clock-time for every sample. A sample time is 5,seconds which means I have to disable Sample button for 5,seconds after clicking it. I used Date.Time.Now and tried to compare dates, I basically have to times one that is current and other that is 5,9 seconds ahead., but since both clocks are running I am not able to apply if/else statement to compare them.

Would you please take a look at this code and let me know if there is any nicer way of solving this preferably with loops.

C#
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DAQ_Simulator
{
    public partial class Form1 : Form
    {
        int counter;
        private DateTime datetime;
        private DateTime datetime2;
        //double next_samplingtime += DateTime.Now;
        //int maxSid = 10; //Number of sensors 7analog and 3 digital
        int maxAI = 7; 
        int maxDI=3;
        int maxSid =11;
        
        // Create an array of 10 sensor objects
        Sensor[] sObj = new Sensor[11];

        public Form1()
        {
            InitializeComponent();

            for (counter = 0; counter < maxSid; counter++)
            {
                sObj[counter] = new Sensor(counter);
            }
        }
        private void displaySensorData(object sender, EventArgs e)
        {
        }
        private void groupSampl_Enter(object sender, EventArgs e)
        {

        }
        private void textSampling_TextChanged(object sender, EventArgs e)
        {
        }
        private void btnSampling_Click(object sender, EventArgs e)
        {
            timer1.Interval = 5900; // sampling time specified in miliseconds
            timer1.Start();
            btnSampling.Enabled = false;
            sampling(); // sample the signals

            datetime2 = DateTime.Now.AddSeconds(5).AddMilliseconds(900);
            string time2 = datetime2.Hour + ":" + datetime2.Minute + ":" + datetime2.Second + "." + datetime2.Millisecond;
            textSampling.Text =time2;
            //  System.TimeSpan diff1 = datetime2.Subtract(datetime);        
        }

    private void groupLogg_Enter(object sender, EventArgs e)
    {
    }
    private void txtLogging_TextChanged(object sender, EventArgs e)
    {
    }
    private void labelLoggingText_Click(object sender, EventArgs e)
    {
    }
    private void btnLogging_Click(object sender, EventArgs e)
    {
    }
    private void labelSensorValues_Click(object sender, EventArgs e)
    {
    }
    private void textSensorValues_TextChanged(object sender, EventArgs e)
    {
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
         btnSampling.Enabled = true;
         timer1.Stop();
    }
    private void sampling()
    {
          
            datetime = DateTime.Now;
           
            string time = datetime.Hour + ":" + datetime.Minute + ":" + datetime.Second + "." + datetime.Millisecond;
            
            // Get the object values as a string

            double sensVal1 = sObj[1].GetAnalogValue();
            double sensVal2 = sObj[2].GetAnalogValue();
            double sensVal3 = sObj[3].GetAnalogValue();
            double sensVal4 = sObj[4].GetAnalogValue();
            double sensVal5 = sObj[5].GetAnalogValue();
            double sensVal6 = sObj[6].GetAnalogValue();
            double sensVal7 = sObj[7].GetAnalogValue();
            
            int sensVal8 = sObj[8].GetDigitalValue();
            int sensVal9 = sObj[9].GetDigitalValue();
            int sensVal10 = sObj[10].GetDigitalValue();

            //Analog values converted to strings and stored in variables 
            String analogSens1 = sensVal1.ToString("F3");
            String analogSens2 = sensVal2.ToString("F3");
            String analogSens3= sensVal3.ToString("F3");
            String analogSens4 = sensVal4.ToString("F3");
            String analogSens5 = sensVal5.ToString("F3");
            String analogSens6 = sensVal6.ToString("F3");
            String analogSens7 = sensVal7.ToString("F3");

            //Digital values converted to strings and stored in variables
            String digSens8 = sensVal8.ToString("F0");
            String digSens9 = sensVal9.ToString("F0");
            String digSens10 = sensVal10.ToString("F0");

            textSensorValues.Text += ("  " + time + "  " + analogSens1 + "  " + analogSens2+ "  " + analogSens3 + "  " + analogSens4+
                "  "+ analogSens5+"  "+analogSens6+"  "+ analogSens7+"  "+digSens8+"  "+digSens9+"  "+digSens10+"\r\n");

            /*
                        for (int id = 0; id < maxAI; id++)
                       {
                            double y = sObj[id].GetAnalogValue();
                            if (id==0)
                            {
                                textSensorValues.Text += time+ " ," + sTxt + "V,";

                            }
                        }

                        for (counter = 0; counter < maxDI; counter++)
                        {

                           // sTxt1 += sObj[3].GetDigitalValue().ToString();
                        }

                */
           
            }
        
    }
}


Sensor class:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAQ_Simulator
{
    public class Sensor
    {
        Random ranDigVal;
        double AnalogVal;
        int DigVal;
        int sId;
        Random rSensVal;

        public Sensor(int id)
        {
            sId = id;
            rSensVal = new Random(id);
            ranDigVal = new Random(id);
            AnalogVal = 0.0F;
        }
        
        public double GetAnalogValue(double minAnalogVolt=0.00F,double maxAnalogVolt=1.00F)
        {
            if(minAnalogVolt <= AnalogVal && AnalogVal<= maxAnalogVolt)

                 AnalogVal = rSensVal.NextDouble();
                 return AnalogVal;         
       }
        public int GetDigitalValue(int digMin = 0, int digMax = 1)
        {

            DigVal = ranDigVal.Next(0, 2);
            return DigVal;
        }
        public int GetSensId()
        {
            return sId;
        }      
    }
}


modified 10-Feb-18 14:09pm.

GeneralRe: Split a String of numbers in textfield Pin
Luc Pattyn10-Feb-18 8:41
sitebuilderLuc Pattyn10-Feb-18 8:41 
AnswerRe: Split a String of numbers in textfield Pin
Richard MacCutchan9-Feb-18 21:58
mveRichard MacCutchan9-Feb-18 21:58 
Questionmodeling n-variable polynomial where n isn't known until runtime Pin
Alexander Kindel9-Feb-18 10:06
Alexander Kindel9-Feb-18 10:06 
AnswerRe: modeling n-variable polynomial where n isn't known until runtime Pin
Alexander Kindel10-Feb-18 4:03
Alexander Kindel10-Feb-18 4:03 
QuestionI do not know English very well, so my question may be incomprehensible .I wanted to create a program that should determine the frequency of spreading it. Pin
Bek Boltayev9-Feb-18 9:48
Bek Boltayev9-Feb-18 9:48 
AnswerRe: I do not know English very well, so my question may be incomprehensible .I wanted to create a program that should determine the frequency of spreading it. Pin
Pete O'Hanlon9-Feb-18 10:10
mvePete O'Hanlon9-Feb-18 10:10 
QuestionHow can I create a program that shows information on DVB-T2 USB and shows it in a noutbook ??? Pin
Bek Boltayev9-Feb-18 9:42
Bek Boltayev9-Feb-18 9:42 
AnswerRe: How can I create a program that shows information on DVB-T2 USB and shows it in a noutbook ??? Pin
Dave Kreskowiak9-Feb-18 13:27
mveDave Kreskowiak9-Feb-18 13:27 
QuestionMultiple sliders or trackbars Pin
Member 136704429-Feb-18 8:07
Member 136704429-Feb-18 8:07 
QuestionRe: Multiple sliders or trackbars Pin
Maciej Los9-Feb-18 9:03
mveMaciej Los9-Feb-18 9:03 
AnswerRe: Multiple sliders or trackbars Pin
ZurdoDev9-Feb-18 9:10
professionalZurdoDev9-Feb-18 9:10 
AnswerRe: Multiple sliders or trackbars Pin
BillWoodruff10-Feb-18 23:23
professionalBillWoodruff10-Feb-18 23:23 
GeneralRe: Multiple sliders or trackbars Pin
Member 1367044211-Feb-18 12:59
Member 1367044211-Feb-18 12:59 
GeneralRe: Multiple sliders or trackbars Pin
BillWoodruff11-Feb-18 16:56
professionalBillWoodruff11-Feb-18 16:56 
GeneralRe: Multiple sliders or trackbars Pin
Member 1367028512-Feb-18 0:54
Member 1367028512-Feb-18 0:54 
QuestionHow to add a discrete transfer function in C# code? Pin
Member 136703089-Feb-18 5:21
Member 136703089-Feb-18 5:21 
AnswerRe: How to add a discrete transfer function in C# code? Pin
OriginalGriff9-Feb-18 5:27
mveOriginalGriff9-Feb-18 5:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.