Advertisement
Guest User

GMC-300E Geiger counter data logging in C#

a guest
Jun 1st, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO.Ports;
  5. using System.Text;
  6.  
  7. namespace SerialPortTest
  8. {
  9.     /* Implementation of GQ-RFC1201 http://www.gqelectronicsllc.com/download/GQ-RFC1201.txt */
  10.     class Program
  11.     {
  12.         static SerialPort port;
  13.         static List<ushort> heartbeats;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             heartbeats = new List<ushort>();
  18.  
  19.             port = new SerialPort("COM9", 57600, Parity.None, 8, StopBits.One);
  20.  
  21.             port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
  22.  
  23.             port.Open();
  24.  
  25.             port.WriteLine("<GETVER>>"); // get the device version
  26.            
  27.             port.WriteLine("<HEARTBEAT1>>"); // enable counts per second heartbeat
  28.  
  29.             while (true)
  30.             {
  31.                 System.Threading.Thread.Sleep(10);
  32.             }
  33.         }
  34.  
  35.  
  36.  
  37.         private static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
  38.         {
  39.             var buf = new byte[port.BytesToRead];
  40.  
  41.             port.Read(buf, 0, buf.Length);
  42.  
  43.             if (buf.Length == 2) // this is most likely a heartbeat
  44.             {
  45.                 Array.Reverse(buf);
  46.                 var bits = new System.Collections.BitArray(buf);
  47.                 bits[14] = false;
  48.                 bits[15] = false; // "The highest bit 15 and bit 14 are reserved data bits."
  49.  
  50.                 bits.CopyTo(buf, 0);
  51.                 var outint = (ushort)BitConverter.ToUInt16(buf, 0); // the CPS value as unsigned short
  52.                 addHeartBeat(outint);
  53.             }
  54.             else Console.WriteLine(Encoding.ASCII.GetString(buf));
  55.         }
  56.  
  57.  
  58.  
  59.         static void addHeartBeat(ushort source) // keep the last 60 values in a list and compute the CPM value by adding them
  60.         {
  61.             heartbeats.Add(source);
  62.  
  63.             while (heartbeats.Count > 60) heartbeats.RemoveAt(0);
  64.  
  65.             int cpm = 0;
  66.             foreach (var hb in heartbeats) cpm += hb;
  67.  
  68.             Console.WriteLine(cpm + " cpm");
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement