HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

SerialPort class for a library

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
classforlibraryserialport

Problem

I've been working a lot recently with SerialPort in C# and I've come up with the following class as part of a class library I'm working on for another program. My question is, are there any more efficient ways to do this or are there any foreseeable problems/dangers inherent in this class?

```
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text;
using System.IO.Ports;
using System.IO;

namespace MedSerialPort
{
public class SerialPortConnection
{
///
///
/// This is written on the fly and it's a little hacky. Forgive me.
/// Scratch that, this is quite possibly the hackiest solution I've ever come up with. I actually almost feel guilty.
/// ">" is an end of line token on the machine I most commonly work with. I have serialPort.ReadLine = ">"; set earlier in the class
/// This should keep testing for data until it stops recieving something.
/// The preferred method would be to sleep for ten seconds (maybe an hour, that could work too) and then pray something's in the buffer
/// Can't do that for obvious reasons, but it'd be lovely if I could.
/// Serial ports are an absolute pain to work with. I'm sorry for anyone else that gets stuck working on this class library...
/// Forgive all the unused "e"s I plan to log them later.
///
///
///

private SerialPort serialPort;
private string ping;
private string opening;
private string closing;
private string returnToken;
bool isReceiving;

public SerialPortConnection(string comPort = "Com1", int baud = 9600, System.IO.Ports.Parity parity = System.IO.Ports.Parity.None, int dataBits = 8, System.IO.Ports.StopBits stopBits = System.IO.Ports.StopBits.One, string ping = "*IDN?", string opening = "REMOTE", string closing = "LOCAL", string returnToken = ">")
{
this.ping = ping;

Solution

Character based serial communications are classically difficult to program in theory.  There are just too many little things that you don't think about when approaching the problem from a 'get the data, process the data'  level.  E.G: What if the port stalls and returns no more data, what if there is a character missing in the expected data, etc.  In many implementations these conditions will cause your code to completely lock up and go into an infinite loop.  Although the SerialPort class of the .NET framework tries to present a clean interface, underneath this abstraction are still all the realities of serial communications.

You really need a reference implementation to even begin determining the quality parameters and pros and cons of your code ( if your concept even works at all ).   Some things can be designed at a high level.  Serial communications is not one of things.  Get a machine, any machine that can meet the essential basic needs to create a WORKING proof of concept and go from there.

What you probably want to aim for generally is something like the OSI protocol stack or TCP/IP stack.  These stacks show how real robust MESSAGING system should be implemented.  They include such essential features as error correction, retry aborted sessions, etc, etc.  

Although you may think these example stacks are overkill, you will find that implementing communications is not as simple as it seems, and these messaging stacks have developed their complex nature in response to this challenging problem.

Context

StackExchange Code Review Q#29807, answer score: 12

Revisions (0)

No revisions yet.