Application design guidelines
I'm looking for some guidance for an app I'm currently developing. I try to keep this as short as possible, so if you need more information, just let me know.
I'm developing a Winforms based event tracking system using VB.Net (VS 2008 Pro). The application collects data through serial barcode scanners and stores this data in an MS SQL Express database located on the same Windows XP Pro workstation where the application is installed. When the application receives a formatted string of data, it checks the db table and sends a response back to the device indicating if the data is duplicate. I wrote a PortManager class to handle serial devices and this functionality works well.
Now I was asked to include a new type of data collector in the system. The new device uses embedded Linux and uses TCP \ IP (POE) rather than serial communication. The device manufacturing assured me that it can provide a string from the built-in device scanner in the same format as my serial devices.
If possible, I would like to use the current application and just add the ability to use TCP \ IP devices with it. I thought it might be possible to just add a new class (similar to my Port Manager class) to handle TCP \ IP based devices.
I'm really at a loss as to the best way to approach this. I'm just not familiar with collecting data from a Winforms application using a TCP \ IP based device.
Any advice on the best approach and / or sample code for communicating with a Winforms application over TCP \ IP is greatly appreciated.
Thanks for any help you could provide!
source to share
The cleanest way I can think of is to refactor your code to accept a stream as you enter it. Then you can change the port manager to write to the stream that was provided by your application. TCP / IP communication in .net is handled most easily with network flow, so you now have a clean interface that could later be extended even further to include flat file streams, memory streams from other applications / plugins.
source to share
Create a simple input wrapper. This is similar to devices that you create messages or events, regardless of how events enter your system.
A simple interface would be something like
public interface IBarcodeScanner
{
IEnumerable<BarcodeEvent> ReadEvents();
}
Then you can use this in a separate thread in your application like this.
var barcodeScanner = OpenConnectedScanner();
foreach (var barcodeEvent in barcodeScanner)
{
ProcessEvent(barcodeEvent);
}
All you have to do is create two types of IBarcodeScanner. Serial port version and TCP / IP version. As an added bonus, you can create a barcode scanner to make it easier to verify your code.
source to share
There is another alternative if you think it is too difficult to refactor the port class. You can create a separate application that does serial port processing and implement a TCP / IP server in it that is very similar to a TCP / IP based device.
Then your application will change to use TCP / IP to talk to the scanner. It doesn't matter if your application is scanning a conversation over a serial port or an external scanner directly connected using TCP / IP.
Again, this gives you the ability to create a dummy scanner by implementing a simple stub application.
This is not an ideal approach as you need to create two applications and make sure TCP / IP for the serial application starts up if you need to, but it will work.
source to share