Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Communication layer
- // Handles sending commands on other ports, in addition to what is shown here
- public class Communication {
- public delegate void delData(List<byte> data);
- private EventDrivenTCPClient dataClient;
- private int pendingDataCnt;
- private List<byte> pendingDataRcv = new List<byte>();
- public event delData OnDataRcvd;
- public void Connect() {
- dataClient = new EventDrivenTCPClient(address, (int)CommonSettings.Default.DataPort, false);
- dataClient.DataEncoding = encoding;
- dataClient.DataReceived += dataClient_DataReceived;
- dataClient.ConnectionStatusChanged += dataClient_ConnectionStatusChanged;
- dataClient.Connect();
- }
- void dataClient_DataReceived(EventDrivenTCPClient sender, object data) {
- ReceivePortBytes("Data", 4, false, LogNextData || outputDataConsole, ref pendingDataCnt, ref pendingDataRcv, (byte[])data, RcvData);
- }
- // Called after receiving a complete packet
- void RcvData(List<byte> data) {
- if (OnDataRcvd != null) {
- OnDataRcvd(data);
- }
- }
- // Store received bytes and determine if a complete packet has been received
- // Reused for multiple sockets
- private bool ReceivePortBytes(string portName, int lengthMult, bool usesUID, bool logHex, ref int pendingCnt, ref List<byte> pendingRcv, byte[] data, delMessage callback = null){
- bool gotFullPacket = false;
- pendingRcv.AddRange(data);
- CheckPendingPort(portName, lengthMult, ref pendingCnt, ref pendingRcv);
- // Loop through any complete packets
- while (pendingCnt > 0 && pendingRcv.Count >= 4 + pendingCnt) {
- gotFullPacket = true;
- List<byte> msg = pendingRcv.GetRange(0, pendingCnt + 4);
- pendingRcv.RemoveRange(0, pendingCnt + 4);
- pendingCnt = 0;
- if (callback != null)
- {
- callback(msg);
- }
- CheckPendingPort(portName, lengthMult, ref pendingCnt, ref pendingRcv);
- }
- return gotFullPacket;
- }
- // Determine # of bytes we are expecting on this socket
- private void CheckPendingPort(string portName, int lengthMult, ref int pendingCnt, ref List<byte> pendingRcv) {
- if (pendingCnt == 0 && pendingRcv.Count >= 4) {
- byte[] cnt = FixEndian(pendingRcv.GetRange(0, 4).ToArray());
- pendingCnt = BitConverter.ToInt32(cnt, 0) * lengthMult;
- Console.WriteLine("Expecting " + pendingCnt + " bytes on " + portName + " port");
- }
- }
- private byte[] FixEndian(byte[] arr) {
- if (BitConverter.IsLittleEndian)
- Array.Reverse(arr);
- return arr;
- }
- }
- // Form that handles capturing data
- public class DataStorage {
- List<RawData> param1 = new List<RawData>(); // Count = #channels
- List<RawData> param2 = new List<RawData>();
- List<RawData> param3 = new List<RawData>();
- int _storedEvents = 0;
- int storedEvents {
- get { return _storedEvents; }
- set {
- _storedEvents = value;
- if (InvokeRequired) {
- BeginInvoke((MethodInvoker)delegate
- {
- // Update a label
- L_StoredEvents.Text = "Stored Events: " + _storedEvents;
- });
- } else {
- L_StoredEvents.Text = "Stored Events: " + _storedEvents;
- }
- }
- }
- int numEvents = 0;
- // Hooked up to Communication.OnDataRcvd
- public void AddData(List<byte> data){
- int count = BitConverter.ToInt32(FixEndian(data.GetRange(0, 4).ToArray()), 0);
- int idx = 1;
- try {
- while (idx < count) {
- float timestamp = BitConverter.ToSingle(FixEndian(data.GetRange(idx++ * 4, 4).ToArray()), 0);
- timestamps.Add(timestamp);
- for (int i = 0; i < channels; i++) {
- if (useParam1) {
- float p1 = BitConverter.ToSingle(FixEndian(data.GetRange(idx++ * 4, 4).ToArray()), 0);
- param1[i].data.Add(p1);
- }
- if (useParam2) {
- float p2 = BitConverter.ToSingle(FixEndian(data.GetRange(idx++ * 4, 4).ToArray()), 0);
- param2[i].data.Add(p1);
- }
- if (useParam3) {
- float p3 = BitConverter.ToSingle(FixEndian(data.GetRange(idx++ * 4, 4).ToArray()), 0);
- param3[i].data.Add(p3);
- }
- }
- storedEvents++;
- numEvents++;
- }
- if (limitedCapture) {
- BeginInvoke((MethodInvoker)delegate {
- // Update ProgressBar
- PB_CaptureProgress.Value = (numEvents < PB_CaptureProgress.Maximum ? numEvents : PB_CaptureProgress.Maximum);
- });
- if (numEvents >= Settings.Default.NSamplesToCapture) {
- StopCapture();
- MessageBox.Show("Finished capturing " + numEvents + " events", "Finished Capturing", MessageBoxButtons.OK);
- }
- }
- }
- catch (ArgumentException e) {
- terminal.AppendError("Error parsing data");
- Console.WriteLine(e.Message + "\n" + e.StackTrace);
- }
- catch (OutOfMemoryException) {
- // After switching to ChunkedList instead of List<float> in RawData, I get OOM exceptions in other areas
- terminal.AppendError("Memory limit exceeded, please save and clear data");
- }
- }
- }
- public class RawData {
- public string name;
- public ChunkedList data;
- }
- //ChunkedList from Aloraman
- public class ChunkedList {
- private readonly List<float[]> _chunks = new List<float[]>();
- private const int ChunkSize = 8000;
- private int _count = 0;
- public int Count {
- get { return _count; }
- }
- public void Add(float item) {
- int chunk = _count / ChunkSize;
- int ind = _count % ChunkSize;
- if (ind == 0)
- {
- _chunks.Add(new float[ChunkSize]);
- }
- _chunks[chunk][ind] = item;
- _count++;
- }
- public float this[int index] {
- get {
- if (index < 0 || index >= _count) throw new IndexOutOfRangeException();
- int chunk = index / ChunkSize;
- int ind = index % ChunkSize;
- return _chunks[chunk][ind];
- }
- set {
- if (index < 0 || index >= _count) throw new IndexOutOfRangeException();
- int chunk = index / ChunkSize;
- int ind = index % ChunkSize;
- _chunks[chunk][ind] = value;
- }
- }
- public void Clear() {
- _chunks.Clear();
- _count = 0;
- // anything else i need to do to properly clear?
- }
- }
- // EventDrivenTCPClient from https://www.daniweb.com/software-development/csharp/code/422291/user-friendly-asynchronous-event-driven-tcp-client with this modification
- public class EventDrivenTCPClient {
- private void cbDataReceived(IAsyncResult result) {
- var sock = result.AsyncState as Socket;
- if (sock == null)
- throw new InvalidOperationException("Invalid IASyncResult - Could not interpret as a socket");
- SocketError err = new SocketError();
- try {
- int bytes = sock.EndReceive(result, out err);
- if (bytes == 0 || err != SocketError.Success) {
- lock (SyncLock) {
- tmrReceiveTimeout.Start();
- return;
- }
- } else {
- lock (SyncLock) {
- tmrReceiveTimeout.Stop();
- }
- }
- if (DataReceived != null) {
- // DataReceived.BeginInvoke(this, _encode.GetString(dataBuffer, 0, bytes), new AsyncCallback(cbDataRecievedCallbackComplete), this);
- byte[] rcv = new byte[bytes];
- Array.Copy(dataBuffer, rcv, bytes);
- DataReceived.BeginInvoke(this, rcv, new AsyncCallback(cbDataRecievedCallbackComplete), this);
- }
- } catch (ObjectDisposedException) {
- Console.WriteLine("TCP Client closed");
- }
- }
- }
- // Form to display the data, not used during this test
- public class DataDisplay {
- // Hooked up to Communication.OnDataRcvd
- public void AddData(List<byte> data) { // false
- if (!Settings.Default.UseHistogram)
- return;
- }
- // Parse data and add into individual circular buffer ( float[10000] )
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement