Reputation: 167
I’m looking for some advice for an application I currently developing. I’ll try to keep this as brief as possible so if additional info is need just let me know.
I’m developing an Winforms based event tracking system using VB.Net (VS 2008 Pro). The application collects data via serial bar code scanners and stores this data within a MS SQL Express db located on the same Windows XP Pro workstation that the application is installed on. When the application receives the formatted string of data it checks the db table and sends a reply back to the device indicating whether the data is a duplicate. I have written a PortManager class to handle the serial based devices and this functionality is working well.
I have now been asked to incorporate a new type of collection device into the system. The new device is running embedded Linux and uses TCP\IP (POE) rather than serial communication. The device manufacture has assured me he can provide a string from the devices integrated scanner in the same format as my serial based devices.
If possible, I would like to utilize the current application and just add the ability to use the TCP\IP devices to it. I was thinking it might be possible to just add a new class (similar to my Port Manager class) to handle the TCP\IP based devices.
I’m really at a loss as to the best way to approach this. I’m just not that 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 to a Winforms app via TCP\IP would be greatly appreciated.
Thank you for any help you might be able to provide!
Upvotes: 2
Views: 198
Reputation: 18295
There is another alternative if you think it is too hard to refactor the port class. You could create a separate application that does the serial port manipulation and implement a TCP/IP server in it that looks very similar to the TCP/IP based device.
You application then changes to use TCP/IP to talk to the scanner. It doesn't matter whether the scanner is your application talking over the serial port, or an external scanner directly communicating 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 have to create two applications and make sure the TCP/IP to serial application starts up if you need to, but it would work.
Upvotes: 0
Reputation: 18295
Create a simple wrapper object for the input. It sounds like the devices you have produce messages or events irrespective of how the events get to your system.
A simple interface would be something like
public interface IBarcodeScanner
{
IEnumerable<BarcodeEvent> ReadEvents();
}
You could then use this in a separate thread in your application as follows.
var barcodeScanner = OpenConnectedScanner();
foreach (var barcodeEvent in barcodeScanner)
{
ProcessEvent(barcodeEvent);
}
All you need to do then is create two types of IBarcodeScanner. A serial port version and a TCP/IP version. As an added bonus, you could create a dummy barcode scanner to make it easy to test your code.
Upvotes: 1
Reputation: 1821
The cleanest way I can think of to do this would be to refactor your code to accept a stream as it's input. Then you could change your port manager to write to a stream it had been given by your app. TCP/IP comm in .net is handled most easily w/ a Network stream, so now you've got a clean interface, that later on could be expanded even more to include flat file streams, memory streams from other apps/plugins.
Upvotes: 2