jcurl / RJCP.DLL.SerialPortStream

SerialPortStream is an independent implementation of System.IO.Ports.SerialPort and SerialStream for better reliability and maintainability. Default branch is 2.x and now has support for Mono with help of a C library.
Microsoft Public License
628 stars 197 forks source link

Interface of SerialPortStream #41

Closed tmatthey closed 6 years ago

tmatthey commented 6 years ago

Have there been any efforts or thoughts to add an interface for SerialPortStream? This would simplify the testing without having actual hardware connected to the computer and using just some fake implementations to return a stream of bytes.

jcurl commented 6 years ago

Thoughts have been given since version 2.0 to provide other internal implementations through the INativeSerial interface. It might be possible to make that interface public to allow external implementations (such as for testing). Certainly one wouldn't need an ISerialPortStream interface as there is much extra functionality there not specifically related to a serial port.

You're more than welcome to discuss your thoughts in this direction, and if a suitable design is found, possibly making a pull request for it.

tmatthey commented 6 years ago

Cool! My 2Cents based on http://www.barrycornelius.com/papers/quality/slides/b-slides033.html for ISerialPortStream was to use a fake returning some byte stream for my RS232 project on Win & Linux. This would allow very easy & rapid testing to read bytes from serial port. Giving an additionally base fake implementation, which you can inherit from you can just override what you need for your tests, would be very handy. I'm aware the SerialPortStream has much more functionality there not specifically related to a serial port.

In case you would go via INativeSerial you would need Castle Windsor or any other IoC to inject your fake serial stream / fake INativeSerial implementation.

You may look at https://github.com/tmatthey/SerialPortStream; I'm taken a spike to extract the interface. Additionally, I'm trying to get an unified solution file and project files for net40, net45 and .netcore1.5 for VS 2017.

And kudos for providing this nice serial port pkg.

jcurl commented 6 years ago

Thanks for your post! For testing, I install com2com as a software library. The sources are open, but there are a few signed versions out there for those without kernel signing certificates.

tmatthey commented 6 years ago

Thanxs, I’ll try out com2com. I finally was able to unify the targets into one sln / csproj and pushed to the forked repo. I was able to run the tests, but only with ReSharper installed.

I’m not sure about the difference when creating new instance of SerialPortStrem. The default does not do an open, but with given parameters (port, baud, etc) it does anopen according to the doc? The code seems to tell me something different, no open. Did I miss something out?

jcurl commented 6 years ago

When you instantiate the SerialPortStream, it should not open, until you explicitly say so, as there's two ways to open (Open() and OpenDirect()). You might want to change some other properties before opening. This prevents any issues with external devices that might be confused if parameters change after it's opened.

tmatthey commented 6 years ago

Thanxs. I figured out how I can make an interface with the properties I need:

public class SerialPortStream : RJCP.IO.Ports.SerialPortStream, ISerialPortStream { }

public interface ISerialPortStream { int BaudRate { get; set; } int DataBits { get; set; } Parity Parity { get; set; } StopBits StopBits { get; set; } int ReadBufferSize { get; set; } int WriteBufferSize { get; set; } int ReadTimeout { get; set; } int WriteTimeout { get; set; } }

tmatthey commented 5 years ago

Thanks for adding an interface.