Skip to content

Instantly share code, notes, and snippets.

@glyphx
Created September 26, 2017 10:12
Show Gist options
  • Save glyphx/fd55e8ea0000b9c960d9e8d794f9a07f to your computer and use it in GitHub Desktop.
Save glyphx/fd55e8ea0000b9c960d9e8d794f9a07f to your computer and use it in GitHub Desktop.
312 am
using System;
using Random = System.Random;
using System.Threading;
using System.IO.Ports;
using System.Diagnostics;
public class Hand_CMD_Test
{
private static SerialPort handPortL;
private static SerialPort handPortR;
public static Random rnd = new Random();
static Stopwatch stopWatch = new Stopwatch();
public static int Main(string[] args)
{
stopWatch.Start();
TimeSpan ts = stopWatch.Elapsed;
int i = 1;
Init(); //Initialize COMPORTS Baud 115200 COM255 = L COM256 = R
while (true)
{
Thread.Sleep(500);
WriteToHands();
ts = stopWatch.Elapsed;
PrintTime(ts);
Console.WriteLine("We have been through the Main() function " + i + " times.");
Console.WriteLine("=================================================================");
i++;
}
return 0; //Should never execute as currently implemented.
}// Main
private static void Init() //can cause uncaught exception in system.dll if comports are not open
{ // another good reason to use device id's
/*============================================================================================\
| *Preconditions: Serial Ports are addressed to 255==LHand and 256==Rhand in A4 Fast CSV mode |
| *PostConditions: Leaves hands in 500,500,500,500 position with COM PORTS in BAUD 115200 |
\============================================================================================*/
handPortL = new SerialPort("COM255", 115200);
handPortR = new SerialPort("COM256", 115200);
if (!handPortL.IsOpen)
{
try
{
handPortL.Open();
handPortL.BaudRate = 115200;
handPortL.WriteLine("H2");
ClearBuffers();
var i = 1;
Console.WriteLine("End of handPortL init(), which executed a total of " + i + " times.");
i++;
}
catch (Exception e)
{
Console.WriteLine("Could not open comport for hands." + e);
}
}
if (!handPortR.IsOpen)
{
try
{
handPortR.Open();
handPortR.BaudRate = 115200;
handPortR.WriteLine("H1");
ClearBuffers();
var i = 1;
Console.WriteLine("End of handPortR init(), which executed a total of" + i + " times.");
i++;
}
catch (Exception e)
{
Console.WriteLine("Could not open comport for hands" + e);
}
}
try
{
Console.WriteLine("Init()'s WriteLine() Try Block Begin");
handPortL.Write("500,500,500,500\n");
handPortR.Write("500,500,500,500\n");
ClearBuffers();
var i = 1;
Console.WriteLine("Wrote to L and R in Init() " + i + " Times");
i++;
}
catch (Exception e)
{
Console.WriteLine("Caught error in Init()'s WriteLine Try Block" + e);
}
}// Init
private static int WriteToHands() //Precondition: Hands are initialized / COM Ports == Open
/*======================================================================================\
| * Precondition: Hands are init to baudrt 112500 / COM Ports Open LH=COM255 RH=COM256 |
| * Postcondition: Hands are written a random value from 100-800, buffers cleared after |
\======================================================================================*/
{
string temp = (rnd.Next(100, 700)).ToString() + "," + (rnd.Next(100, 700)).ToString() + ","
+ (rnd.Next(100, 700)).ToString() + "," + (rnd.Next(100, 700)).ToString();
Console.WriteLine(temp);
handPortL.Write(temp + "\n"); //changed to write()
handPortR.Write(temp + "\n");
ClearBuffers();
return 0;
} // WriteToHands
private static int ClearBuffers()
{
/*======================================================================================\
| * Clears serial buffers for both hands in try catch blocks. |
| * PostConditions: returns 1 for error in L, -1 for R, otherwise 0 |
\======================================================================================*/
try
{
handPortL.DiscardOutBuffer();
handPortL.DiscardInBuffer();
}
catch (Exception e)
{
Console.WriteLine("Error in ClearBuffers() L");
return 1; //if one fails they all will fail, should write this seperate -- lazy.
}
try
{
handPortR.DiscardOutBuffer();
handPortR.DiscardInBuffer();
}
catch (Exception e)
{
Console.WriteLine("Error in ClearBuffers() R");
return -1;
}
return 0;
}// ClearBuffers
public static string PrintTime(TimeSpan ts)
{
/*====================================================================================\
| * Preconditions - Needs a non-null TimeSpan value |
| * Postconditions - prints a formatted string to stdout, and returns the same string.|
\====================================================================================*/
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
return elapsedTime;
}// FormatTime
} //End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment