Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Text;
- namespace SerialPortTest
- {
- /* Implementation of GQ-RFC1201 http://www.gqelectronicsllc.com/download/GQ-RFC1201.txt */
- class Program
- {
- static SerialPort port;
- static List<ushort> heartbeats;
- static void Main(string[] args)
- {
- heartbeats = new List<ushort>();
- port = new SerialPort("COM9", 57600, Parity.None, 8, StopBits.One);
- port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
- port.Open();
- port.WriteLine("<GETVER>>"); // get the device version
- port.WriteLine("<HEARTBEAT1>>"); // enable counts per second heartbeat
- while (true)
- {
- System.Threading.Thread.Sleep(10);
- }
- }
- private static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- var buf = new byte[port.BytesToRead];
- port.Read(buf, 0, buf.Length);
- if (buf.Length == 2) // this is most likely a heartbeat
- {
- Array.Reverse(buf);
- var bits = new System.Collections.BitArray(buf);
- bits[14] = false;
- bits[15] = false; // "The highest bit 15 and bit 14 are reserved data bits."
- bits.CopyTo(buf, 0);
- var outint = (ushort)BitConverter.ToUInt16(buf, 0); // the CPS value as unsigned short
- addHeartBeat(outint);
- }
- else Console.WriteLine(Encoding.ASCII.GetString(buf));
- }
- static void addHeartBeat(ushort source) // keep the last 60 values in a list and compute the CPM value by adding them
- {
- heartbeats.Add(source);
- while (heartbeats.Count > 60) heartbeats.RemoveAt(0);
- int cpm = 0;
- foreach (var hb in heartbeats) cpm += hb;
- Console.WriteLine(cpm + " cpm");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement