Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Originally I submitted a question here: SerialPort class for a library SerialPort class for a library

Originally I submitted a question here: SerialPort class for a library

Originally I submitted a question here: SerialPort class for a library

added 482 characters in body
Source Link
using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace SerialPortSharp
{
 public sealed class SerialPortSharpSerialPortConn : IDisposable
 {
 private readonly SerialPort serialPort;
 private readonly string returnToken;
 private readonly string hookOpen;
 private readonly string hookClose;
 private bool disposed;
 public SerialPortSharpSerialPortConn(
 string comPort = "Com1",
 int baud = 9600,
 Parity parity = Parity.None,
 int dataBits = 8,
 StopBits stopBits = StopBits.One,
 string returnToken = ">""> ",
 string hookOpen = "REMOTE""",
 string hookClose = "LOCAL"""
 )
 {
 this.serialPort = new SerialPort(comPort, baud, parity, dataBits, stopBits)
 {
 NewLine = returnToken,
 ReadTimeout = 1000,
 RtsEnable = true,
 DtrEnable = true
 };
 this.returnToken = returnToken;
 if (hookOpen == "")
 this.hookOpen = null;
 else
 this.hookOpen = hookOpen;
 if (hookClose == "")
 this.hookClose = null;
 else
  this.hookClose = hookClose;
 }
 public bool OpenConnection()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Open();
 this.serialPort.DiscardInBuffer();
 bool hooked = false;
 if (hookOpen != ""null)
 {
 hooked = this.Hook();
 }
 else
 {
 hooked = true;
 }
 if (hooked)
 {
 return true;
 }
 else
 {
 return false;
 }
 }
 catch
 {
 return false;
 }
 }
 public bool CloseConnection()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 bool unhooked = false;
 if (hookClose != ""null)
 {
 unhooked = this.UnHook();
 }
 else
 {
 unhooked = true;
 }
 if (unhooked)
 {
 Thread.Sleep(100);
 this.serialPort.ReadLine();
 this.serialPort.DiscardInBuffer();
 this.serialPort.Close();
 this.serialPort.Dispose();
 return true;
 }
 else
 {
 return false;
 }
 }
 catch
 {
 return false;
 }
 }
 public void Dispose()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot dispose of a disposed object.");
 }
 var closed = this.CloseConnection();
 if (closed)
 {
 this.disposed = true;
 }
 else
 {
 throw new Exception("Error! Could not close port!");
 }
 }
 private bool Hook()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(hookOpen + "\r");
 Thread.Sleep(100);
 this.serialPort.DiscardInBuffer();
 return true;
 }
 catch
 {
 return false;
 }
 }
 private bool UnHook()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(hookClose + "\r");
 Thread.Sleep(100);
 this.serialPort.DiscardInBuffer();
 return true;
 }
 catch
 {
 return false;
 }
 }
 private string HookTest(string serialCommand)
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(serialCommand + "\r");
 Thread.Sleep(100);
 bool loop = true;
 string output = "";
 while (loop)
 {
 output += this.serialPort.ReadExisting();
 if (output.EndsWith(this.returnToken + " /r/n"))
 {
 break;
 }
 }
 return output;
 }
 catch (TimeoutException e)
 {
 throw new Exception("Connection failed. TimedRead timed out.");
 }
 }
 public string WriteConnection(string serialCommand, bool isSafe, string= serialCommandfalse)
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 if (isSafe)
 {
 var output = this.HookTest(serialCommand);
 return output;
 }
 else
 {
 try
 {
 this.serialPort.Write(serialCommand + "\r");
 Thread.Sleep(100);
 return this.serialPort.ReadExisting();
 }
 catch (Exception e)
 {
 throw new Exception("Connection failed. Timed out.");
 }
 }
 }
 }
}

EDIT 2: I've changed the code about lately. I finally freed up a device for testing purposes and it works. I tested 400+ queries and only gotten several errors related to a device quirk I had to work out in the commands I was submitting. I'd love to hear any further suggestions.

using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace SerialPortSharp
{
 public sealed class SerialPortSharp : IDisposable
 {
 private readonly SerialPort serialPort;
 private readonly string returnToken;
 private readonly string hookOpen;
 private readonly string hookClose;
 private bool disposed;
 public SerialPortSharp(
 string comPort = "Com1",
 int baud = 9600,
 Parity parity = Parity.None,
 int dataBits = 8,
 StopBits stopBits = StopBits.One,
 string returnToken = ">",
 string hookOpen = "REMOTE",
 string hookClose = "LOCAL"
 )
 {
 this.serialPort = new SerialPort(comPort, baud, parity, dataBits, stopBits)
 {
 NewLine = returnToken,
 ReadTimeout = 1000,
 RtsEnable = true,
 DtrEnable = true
 };
 this.returnToken = returnToken;
 this.hookOpen = hookOpen;
 this.hookClose = hookClose;
 }
 public bool OpenConnection()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Open();
 this.serialPort.DiscardInBuffer();
 bool hooked = false;
 if (hookOpen != "")
 {
 hooked = this.Hook();
 }
 else
 {
 hooked = true;
 }
 if (hooked)
 {
 return true;
 }
 else
 {
 return false;
 }
 }
 catch
 {
 return false;
 }
 }
 public bool CloseConnection()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 bool unhooked = false;
 if (hookClose != "")
 {
 unhooked = this.UnHook();
 }
 else
 {
 unhooked = true;
 }
 if (unhooked)
 {
 Thread.Sleep(100);
 this.serialPort.ReadLine();
 this.serialPort.DiscardInBuffer();
 this.serialPort.Close();
 this.serialPort.Dispose();
 return true;
 }
 else
 {
 return false;
 }
 }
 catch
 {
 return false;
 }
 }
 public void Dispose()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot dispose of a disposed object.");
 }
 var closed = this.CloseConnection();
 if (closed)
 {
 this.disposed = true;
 }
 else
 {
 throw new Exception("Error! Could not close port!");
 }
 }
 private bool Hook()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(hookOpen);
 Thread.Sleep(100);
 this.serialPort.DiscardInBuffer();
 return true;
 }
 catch
 {
 return false;
 }
 }
 private bool UnHook()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(hookClose);
 Thread.Sleep(100);
 this.serialPort.DiscardInBuffer();
 return true;
 }
 catch
 {
 return false;
 }
 }
 private string HookTest(string serialCommand)
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(serialCommand);
 Thread.Sleep(100);
 bool loop = true;
 string output = "";
 while (loop)
 {
 output += this.serialPort.ReadExisting();
 if (output.EndsWith(this.returnToken + " /r/n"))
 {
 break;
 }
 }
 return output;
 }
 catch (TimeoutException e)
 {
 throw new Exception("Connection failed. Timed out.");
 }
 }
 public string WriteConnection(bool isSafe, string serialCommand)
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 if (isSafe)
 {
 var output = this.HookTest(serialCommand);
 return output;
 }
 else
 {
 try
 {
 this.serialPort.Write(serialCommand);
 Thread.Sleep(100);
 return this.serialPort.ReadExisting();
 }
 catch (Exception e)
 {
 throw new Exception("Connection failed. Timed out.");
 }
 }
 }
 }
}
using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace SerialPortSharp
{
 public sealed class SerialPortConn : IDisposable
 {
 private readonly SerialPort serialPort;
 private readonly string returnToken;
 private readonly string hookOpen;
 private readonly string hookClose;
 private bool disposed;
 public SerialPortConn(
 string comPort = "Com1",
 int baud = 9600,
 Parity parity = Parity.None,
 int dataBits = 8,
 StopBits stopBits = StopBits.One,
 string returnToken = "> ",
 string hookOpen = "",
 string hookClose = ""
 )
 {
 this.serialPort = new SerialPort(comPort, baud, parity, dataBits, stopBits)
 {
 ReadTimeout = 1000,
 RtsEnable = true,
 DtrEnable = true
 };
 this.returnToken = returnToken;
 if (hookOpen == "")
 this.hookOpen = null;
 else
 this.hookOpen = hookOpen;
 if (hookClose == "")
 this.hookClose = null;
 else
  this.hookClose = hookClose;
 }
 public bool OpenConnection()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Open();
 this.serialPort.DiscardInBuffer();
 bool hooked = false;
 if (hookOpen != null)
 {
 hooked = this.Hook();
 }
 else
 {
 hooked = true;
 }
 if (hooked)
 {
 return true;
 }
 else
 {
 return false;
 }
 }
 catch
 {
 return false;
 }
 }
 public bool CloseConnection()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 bool unhooked = false;
 if (hookClose != null)
 {
 unhooked = this.UnHook();
 }
 else
 {
 unhooked = true;
 }
 if (unhooked)
 {
 Thread.Sleep(100);
 this.serialPort.ReadLine();
 this.serialPort.DiscardInBuffer();
 this.serialPort.Close();
 this.serialPort.Dispose();
 return true;
 }
 else
 {
 return false;
 }
 }
 catch
 {
 return false;
 }
 }
 public void Dispose()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot dispose of a disposed object.");
 }
 var closed = this.CloseConnection();
 if (closed)
 {
 this.disposed = true;
 }
 else
 {
 throw new Exception("Error! Could not close port!");
 }
 }
 private bool Hook()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(hookOpen + "\r");
 Thread.Sleep(100);
 this.serialPort.DiscardInBuffer();
 return true;
 }
 catch
 {
 return false;
 }
 }
 private bool UnHook()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(hookClose + "\r");
 Thread.Sleep(100);
 this.serialPort.DiscardInBuffer();
 return true;
 }
 catch
 {
 return false;
 }
 }
 private string HookTest(string serialCommand)
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(serialCommand + "\r");
 Thread.Sleep(100);
 bool loop = true;
 string output = "";
 while (loop)
 {
 output += this.serialPort.ReadExisting();
 if (output.EndsWith(this.returnToken))
 {
 break;
 }
 }
 return output;
 }
 catch (TimeoutException e)
 {
 throw new Exception("Connection failed. Read timed out.");
 }
 }
 public string WriteConnection(string serialCommand, bool isSafe = false)
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 if (isSafe)
 {
 var output = this.HookTest(serialCommand);
 return output;
 }
 else
 {
 try
 {
 this.serialPort.Write(serialCommand + "\r");
 Thread.Sleep(100);
 return this.serialPort.ReadExisting();
 }
 catch (Exception e)
 {
 throw new Exception("Connection failed. Timed out.");
 }
 }
 }
 }
}

EDIT 2: I've changed the code about lately. I finally freed up a device for testing purposes and it works. I tested 400+ queries and only gotten several errors related to a device quirk I had to work out in the commands I was submitting. I'd love to hear any further suggestions.

Tweeted twitter.com/#!/StackCodeReview/status/380485573099937792
added 879 characters in body
Source Link
using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace SerialPortSharp
{
 public sealed class SerialPortSharp : IDisposable
 {
 private readonly SerialPort serialPort;
 private readonly string returnToken;
 private readonly string hookOpen;
 private readonly string hookClose;
 private bool disposed;
 public SerialPortSharp(
 string comPort = "Com1",
 int baud = 9600,
 Parity parity = Parity.None,
 int dataBits = 8,
 StopBits stopBits = StopBits.One,
 string returnToken = ">",
 string hookOpen = "REMOTE",
 string hookClose = "LOCAL"
 )
 {
 this.serialPort = new SerialPort(comPort, baud, parity, dataBits, stopBits)
 {
 NewLine = returnToken,
 ReadTimeout = 1000,
 RtsEnable = true,
 DtrEnable = true
 };
 this.returnToken = returnToken;
 this.hookOpen = hookOpen;
 this.hookClose = hookClose;
 }
 public bool OpenConnection()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Open();
 this.serialPort.DiscardInBuffer();
 bool hooked = false;
 if (hookOpen != "")
 {
 hooked = this.Hook();
 }
 else
 {
 hooked = true;
 }
 if (hooked)
 {
 return true;
 }
 else
 {
 return false;
 }
 }
 catch
 {
 return false;
 }
 }
 public bool CloseConnection()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 bool unhooked = false;
 if (hookClose != "")
 {
 unhooked = this.UnHook();
 }
 else
 {
 unhooked = true;
 }
 if (unhooked)
 {
 Thread.Sleep(100);
 this.serialPort.ReadLine();
 this.serialPort.DiscardInBuffer();
 this.serialPort.Close();
 this.serialPort.Dispose();
 return true;
 }
 else
 {
 return false;
 }
 }
 catch
 {
 return false;
 }
 }
 public void Dispose()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot dispose of a disposed object.");
 }
 var closed = this.CloseConnection();
 if (closed)
 {
 this.disposed = true;
 }
 else
 {
 throw new Exception("Error! Could not close port!");
 }
 }
 private bool Hook()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(hookOpen);
 Thread.Sleep(100);
 this.serialPort.DiscardInBuffer();
 return true;
 }
 catch
 {
 return false;
 }
 }
 private bool UnHook()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(hookClose);
 Thread.Sleep(100);
 this.serialPort.DiscardInBuffer();
 return true;
 }
 catch
 {
 return false;
 }
 }
 private string HookTest(string serialCommand)
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(serialCommand);
 Thread.Sleep(100);
 bool loop = true;
 string output = "";
 while (loop)
 {
 output += this.serialPort.ReadExisting();
 if (output.EndsWith(this.returnToken + " /r/n"))
 {
 break;
 }
 }
 return output;
 }
 catch (TimeoutException e)
 {
 throw new Exception("Connection failed. Read timedTimed out.");
 }
 }
 public string WriteConnection(bool isSafe, string serialCommand)
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 if (isSafe)
 {
 var output = this.HookTest(serialCommand);
 return output;
 }
 else
 {
 try
 {
 this.serialPort.Write(serialCommand);
 Thread.Sleep(100);
 return this.serialPort.ReadExisting();
 }
 catch (Exception e)
 {
 throw new Exception("Connection failed. Read timedTimed out.");
 }
 }
 }
 }
}
using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace SerialPortSharp
{
 public sealed class SerialPortSharp : IDisposable
 {
 private readonly SerialPort serialPort;
 private readonly string returnToken;
 private readonly string hookOpen;
 private readonly string hookClose;
 private bool disposed;
 public SerialPortSharp(
 string comPort = "Com1",
 int baud = 9600,
 Parity parity = Parity.None,
 int dataBits = 8,
 StopBits stopBits = StopBits.One,
 string returnToken = ">",
 string hookOpen = "REMOTE",
 string hookClose = "LOCAL"
 )
 {
 this.serialPort = new SerialPort(comPort, baud, parity, dataBits, stopBits)
 {
 NewLine = returnToken,
 ReadTimeout = 1000,
 RtsEnable = true,
 DtrEnable = true
 };
 this.returnToken = returnToken;
 this.hookOpen = hookOpen;
 this.hookClose = hookClose;
 }
 public bool OpenConnection()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Open();
 this.serialPort.DiscardInBuffer();
 bool hooked = false;
 if (hookOpen != "")
 {
 hooked = this.Hook();
 }
 else
 {
 hooked = true;
 }
 if (hooked)
 {
 return true;
 }
 else
 {
 return false;
 }
 }
 catch
 {
 return false;
 }
 }
 public bool CloseConnection()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 bool unhooked = false;
 if (hookClose != "")
 {
 unhooked = this.UnHook();
 }
 else
 {
 unhooked = true;
 }
 if (unhooked)
 {
 Thread.Sleep(100);
 this.serialPort.ReadLine();
 this.serialPort.DiscardInBuffer();
 this.serialPort.Close();
 this.serialPort.Dispose();
 return true;
 }
 else
 {
 return false;
 }
 }
 catch
 {
 return false;
 }
 }
 public void Dispose()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot dispose of a disposed object.");
 }
 var closed = this.CloseConnection();
 if (closed)
 {
 this.disposed = true;
 }
 else
 {
 throw new Exception("Error! Could not close port!");
 }
 }
 private bool Hook()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(hookOpen);
 Thread.Sleep(100);
 this.serialPort.DiscardInBuffer();
 return true;
 }
 catch
 {
 return false;
 }
 }
 private bool UnHook()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(hookClose);
 Thread.Sleep(100);
 this.serialPort.DiscardInBuffer();
 return true;
 }
 catch
 {
 return false;
 }
 }
 private string HookTest(string serialCommand)
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(serialCommand);
 Thread.Sleep(100);
 bool loop = true;
 string output = "";
 while (loop)
 {
 output += this.serialPort.ReadExisting();
 if (output.EndsWith(this.returnToken + " /r/n"))
 {
 break;
 }
 }
 return output;
 }
 catch (TimeoutException e)
 {
 throw new Exception("Connection failed. Read timed out.");
 }
 }
 public string WriteConnection(bool isSafe, string serialCommand)
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 if (isSafe)
 {
 var output = this.HookTest(serialCommand);
 return output;
 }
 else
 {
 try
 {
 this.serialPort.Write(serialCommand);
 Thread.Sleep(100);
 return this.serialPort.ReadExisting();
 }
 catch (Exception e)
 {
 throw new Exception("Connection failed. Read timed out.");
 }
 }
 }
 }
}
using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace SerialPortSharp
{
 public sealed class SerialPortSharp : IDisposable
 {
 private readonly SerialPort serialPort;
 private readonly string returnToken;
 private readonly string hookOpen;
 private readonly string hookClose;
 private bool disposed;
 public SerialPortSharp(
 string comPort = "Com1",
 int baud = 9600,
 Parity parity = Parity.None,
 int dataBits = 8,
 StopBits stopBits = StopBits.One,
 string returnToken = ">",
 string hookOpen = "REMOTE",
 string hookClose = "LOCAL"
 )
 {
 this.serialPort = new SerialPort(comPort, baud, parity, dataBits, stopBits)
 {
 NewLine = returnToken,
 ReadTimeout = 1000,
 RtsEnable = true,
 DtrEnable = true
 };
 this.returnToken = returnToken;
 this.hookOpen = hookOpen;
 this.hookClose = hookClose;
 }
 public bool OpenConnection()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Open();
 this.serialPort.DiscardInBuffer();
 bool hooked = false;
 if (hookOpen != "")
 {
 hooked = this.Hook();
 }
 else
 {
 hooked = true;
 }
 if (hooked)
 {
 return true;
 }
 else
 {
 return false;
 }
 }
 catch
 {
 return false;
 }
 }
 public bool CloseConnection()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 bool unhooked = false;
 if (hookClose != "")
 {
 unhooked = this.UnHook();
 }
 else
 {
 unhooked = true;
 }
 if (unhooked)
 {
 Thread.Sleep(100);
 this.serialPort.ReadLine();
 this.serialPort.DiscardInBuffer();
 this.serialPort.Close();
 this.serialPort.Dispose();
 return true;
 }
 else
 {
 return false;
 }
 }
 catch
 {
 return false;
 }
 }
 public void Dispose()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot dispose of a disposed object.");
 }
 var closed = this.CloseConnection();
 if (closed)
 {
 this.disposed = true;
 }
 else
 {
 throw new Exception("Error! Could not close port!");
 }
 }
 private bool Hook()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(hookOpen);
 Thread.Sleep(100);
 this.serialPort.DiscardInBuffer();
 return true;
 }
 catch
 {
 return false;
 }
 }
 private bool UnHook()
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(hookClose);
 Thread.Sleep(100);
 this.serialPort.DiscardInBuffer();
 return true;
 }
 catch
 {
 return false;
 }
 }
 private string HookTest(string serialCommand)
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 try
 {
 this.serialPort.Write(serialCommand);
 Thread.Sleep(100);
 bool loop = true;
 string output = "";
 while (loop)
 {
 output += this.serialPort.ReadExisting();
 if (output.EndsWith(this.returnToken + " /r/n"))
 {
 break;
 }
 }
 return output;
 }
 catch (TimeoutException e)
 {
 throw new Exception("Connection failed. Timed out.");
 }
 }
 public string WriteConnection(bool isSafe, string serialCommand)
 {
 if (this.disposed)
 {
 throw new ObjectDisposedException(this.GetType().Name, "Cannot use a disposed object.");
 }
 if (isSafe)
 {
 var output = this.HookTest(serialCommand);
 return output;
 }
 else
 {
 try
 {
 this.serialPort.Write(serialCommand);
 Thread.Sleep(100);
 return this.serialPort.ReadExisting();
 }
 catch (Exception e)
 {
 throw new Exception("Connection failed. Timed out.");
 }
 }
 }
 }
}
added 879 characters in body
Source Link
Loading
deleted 12 characters in body
Source Link
Loading
edited tags
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
added 136 characters in body
Source Link
Loading
Source Link
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /