views:

91

answers:

3

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!

+2  A: 

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.

chris.w.mclean
Thanks for advice Chris, your suggestion seems like a good possible solution. I will need to do a little research on refactoring though... I have never really had to use it and this is my first major Winforms app. I generally work on ASP and ASP.Net stuff.
dc
+1  A: 

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.

Nick R
Thank you Nick! Looks like it would be easy to implement once I figure out where everything needs to go. I like the idea about the dummy bar code scanner; that would save me a lot of testing headaches.I’m not sure if I would be able to use my port class though, since it is handling the actual serial port manipulation as well as the read/write functions for the data.
dc
You wouldn’t happen to know of a simple application example that utilizes your suggestion? I apologize for my ignorance, as I mentioned to Chris, I’m more of a Web developer and this is the first time I have had to do this type of applications.Thanks again Nick!
dc
I don't have a simple example to help you with, but others might. As for using your port class, it should be possible. The thing is this interface hides all the complexity. You could even execute a separate application to do something, communicate with it using stdin/stdout and just return barcode events from the interface.
Nick R
A: 

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.

Nick R
I was realy hoping to avoid creating 2 different apps. I think I may look into the refactor option and see what I can do with that.Thanks Nick.
dc