Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. I am a newbie to C#. I am handling a code where my PC is communicating with a robot. The robot is sending a sensor value to the PC. A DAQ device is being used for digitized sensor data. The sensor values are coming using a networkstream and are stored in a rxbuffer. I can get the number of samples. But how to know the signal duration and the sampling frequency  of the incoming rxbuffer. And also how to change the sampling frequency of the DAQ card I apologize may be my question is not clear. TIA for your help


What I have tried:

private int[] rxData = new int[0xFFFFFF]; 
        private int rxLength = 0;

        public object Generate { get; private set; }
        public object SampleClockActiveEdge { get; private set; }
        public object SampleQuantityMode { get; private set; }
        public object samplesPerChannel { get; private set; }

        

        public void socketReceived() 
        {
            
            byte[] rxBuffer = new byte[0xFFFFFF];
            int startLen = 0, dataLen = 0;

            while (isConnected) 
            {
                try
                {
                    
                    startLen = getSeqeunce(START_CODE);
                    Console.WriteLine("startLen:" + startLen);
                    if (startLen != -1)
                    {
                        Console.WriteLine("rx start");
                        this.Invoke(new Function(updateTextBox), "Measuring");
                        readFully(rxBuffer, 4); 
                        dataLen = (rxBuffer[0] << 24) + (rxBuffer[1] << 16) + (rxBuffer[2] << 8) + rxBuffer[3];
                        //rxLength = dataLen / 2;
                        rxLength  = Convert.ToInt32(tbIP1.Text);
                        

                        Console.WriteLine("rxLength:" + rxLength);
                        readFully(rxBuffer, dataLen);

                        this.Invoke(new Function(updateTextBox), "Converting");


                        //AET_RobotRemote.Sampling var = (AET_RobotRemote.Sampling)fs;
                        //var.DoMoreWork();
                        for (int i = 0; i < rxLength; i++)
                        {
                            

                            rxData[i] = (short)((rxBuffer[i * 2]) + rxBuffer[i * 2 + 1] * 256);
                            





                            Console.WriteLine("{0}: {1}", i, rxData[i]);


                            curveSensor.AddPoint(i, rxData[i]);
                        }

                       




                        this.Invoke(new Function(updateTextBox), "Displaying"); 
                        zgSensor.AxisChange();
                        zgSensor.Invalidate();
                        this.Invoke(new Function(updateTextBox), "Measurement completed"); 
                        Console.WriteLine("rx end");
                        TimeSpan duration = new TimeSpan(0, 0, 0, 0, rxLength);
                        Console.WriteLine("{0} minutes , {1} seconds and {2} MilliSeconds", duration.Minutes, duration.Seconds, duration.Milliseconds);
                        // Console.WriteLine("Duration of the Signal:" + sample.print_duration() + " nanoSecond");
                        //long Sampling_frequency = rxLength / sample.print_duration();
                        //Console.WriteLine("Sampling Frequency: " + Sampling_frequency);



                        this.Invoke(new Action(delegate ()
                        {
                            btnStart.Enabled = true;
                            btnSave.Enabled = true;
                            btnCustom.Enabled = true;
                        }));
                        isComplete = true;
                    }
                }
                catch
                {
                }
            }
        }

        private void updateTextBox(string str)
        {
            lbGraphMessage.Text = str;
        }

        private void sendPacket(byte[] data, int size) 
        {
            byte[] buffer = new byte[256]; 
            int checksum = 0, index = 0;

            try
            {
                if (isConnected)
                {
                    buffer[index++] = 0x42;
                    buffer[index++] = 0x4D;
                    buffer[index++] = (byte)((size + 2) >> 8); 
                    buffer[index++] = (byte)((size + 2) & 0xFF);
                    for (int i = 0; i < size; i++)
                    {
                        checksum += data[i];
                        buffer[index++] = data[i];
                    }
                    buffer[index++] = (byte)(checksum >> 8);
                    buffer[index++] = (byte)(checksum & 0xFF);
                    for (int i = 0; i<index; i++)  Console.Write("{0:X} ", buffer[i]);
                    Console.Write("\n");
                    ns.Write(buffer, 0, index);
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }

        private void readFully(byte[] buffer, int size) 
        {
            int offset = 0;
            int readBytes;
            do
            {
                readBytes = ns.Read(buffer, offset, size - offset);
                offset += readBytes;
            } while ((readBytes > 0) && (offset < size));
        }

        private int getSeqeunce(int[] sequence) 
        {
            int seqIndex = 0; 
            int c;
            for (int i = 0; i < 1024; i++) 
            {
                
                
                c = ns.ReadByte();
                
              


                //Console.WriteLine("readbyte:" + c);
                if (c == sequence[seqIndex])
                { 
                    seqIndex++;
                    if (seqIndex == sequence.Length) return i + 1;
                }
                else seqIndex = 0;
            }
            return -1;
        }
Posted
Updated 7-Jun-21 18:18pm
v2
Comments
[no name] 5-Jun-21 19:29pm    
One usually knows or can control the frequency of the sampling device; that way you can anticipate a sample or know when they're getting dropped. "Guessing" the frequency just leads to uncertainty and frustration.
Richard Deeming 7-Jun-21 12:22pm    
You could at the very least format your code block properly. Leaving the whole thing on one line renders it practically unreadable!
Masrur02 8-Jun-21 0:20am    
Hello, Thank you for your comment. I have organized the code in proper block now. Can you please help me to change the sampling rate of that code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900