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 = Convert.ToInt32(tbIP1.Text);
Console.WriteLine("rxLength:" + rxLength);
readFully(rxBuffer, dataLen);
this.Invoke(new Function(updateTextBox), "Converting");
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);
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)
{
}
}
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();
if (c == sequence[seqIndex])
{
seqIndex++;
if (seqIndex == sequence.Length) return i + 1;
}
else seqIndex = 0;
}
return -1;
}