tags:

views:

708

answers:

5

I'm trying to test my code that reads from a USB port (COM25 when the device is connected) that is created when a device is connected to my computer and to a boat. I cannot power the USB device when not on the boat so testing is difficult. Can someone let me know how to simulate a COM port and write data to it so my test program is able to connect to that simulated COM port and read that data?

I'm reading this from a Java program but the simulation doesn't need to be in Java or any specific language. Just a program that will simulate the COM port and allow me to connect to it. I downloaded a COM port emulator from AGG Software and it appears that it's writing to what I deem COM25 but I'm not able to connect to it from my Java test.

Thanks.

+2  A: 

The general answer for this kind of problem is to wrap the code that talks to the COM port in a class that implements an interface. If you do this as a Facade (pattern) then you can also make the COM methods you call sensible from your end.

The interface can then be mocked or faked for the test. (There is a great article on test objects, but I haven't been able to find it yet.) One advantage here is that you can create a fake version that throws exceptions or otherwise does things that are possible for the port to do but hard to get it to do in practice.

Kathy Van Stone
+1  A: 

Where I work, we solved a similar issue by having our emulator not spoof a COM port at all. Here's how you can do it:

  • Define an interface for talking with your COM port, something like IUsbCommService
  • Implement your real COM-communcation service, using the standard Java Comm API
  • For your emulator, simply kick of a thread that spits out the same sort of data you can expect from your USB device at regular intervals.
  • Use your IOC framework of choice (e.g., Spring) to wire up either the emulator or the real service.
  • As long as you hide your implementation logic appropriately, and as long as you code to your interface, your service-consumer code won't care whether it's talking to the real USB device or to the emulator.

For example:

import yourpackage.InaccessibleDeviceException;
import yourpackage.NoDataAvailableException;

public interface IUsbProviderService {

    public void initDevice() throws InaccessibleDeviceException;

    public UsbData getUsbData() 
        throws InaccessibleDeviceException, NoDataAvailableException;
}

// The real service
import javax.comm.SerialPort; //....and the rest of the java comm API 

public class UsbService implements IUsbProviderService {
.
.
.
}

// The emulator
public class UsbServiceEmulator implements IUsbProviderService {
    private Thread listenerThread;
    private static final Long WAITTIMEMS = 10L;
    private String usbData;

    public UsbServiceEmulator(long maxWaitTime) throws InaccessibleDeviceException{
        initialize();
        boolean success = false;
        long slept = 0;

        while (!success && slept < maxWaitTime) {
            Thread.sleep(WAITTIMEMS);
            slept += WAITTIMEMS;

        }
    }

    private void initialize() throws InaccessibleDeviceException{
        listenerThread = new Thread();
        listenerThread.start();
     }

     private class UsbRunner implements Runnable {
        private String[] lines = {"Data line 1", "Data line 2", "Data line 3"};
        public void run() {
            int line = 0;
            while(true) {

                serialEvent(lines[line]);

                if(line == 3) {
                    line = 0;
                } else {
                    line++;
                }

                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    //handle the error
                }
            }

        private void serialEvent(String line) {
            if(/*you have detected you have enough data */) {
                synchronized(this) {
                    usbData = parser.getUsbData();
                }
            } 


     }
}

Hope this helps!

rtperson
A: 

I recommend fabulatech's virtual modem. Get it at http://www.virtual-modem.com

You might also want to get a COM port monitor for your tests - You can find it at http://www.serial-port-monitor.com

Good luck with the boat! :)

Danra
Does the virtual modem have a programmer-accessible API? Because I'm not seeing one from the info provided. As for monitoring the serial port, the product looks good. But if you just need a console for the input, HyperTerminal will work just as well.
rtperson
A: 

Thanks to all the answers so far! Here's what I ended up doing as a result of recommendations from someone at work.

  1. Downloaded the COM Port Data Emulator (CPDE) from AGG Software
  2. Downloaded the Virtual Serial Port Driver (VSPD) from Eltima Software

(I just randomly picked a free data emulator and virtual serial port package. There are plenty of alternatives out there)

  1. Using VSPD, created virtual serial ports 24 and 25 and connected them via a virtual null modem cable. This effectively creates a write port at 24 and a read port at 25.

  2. Ran the CPDE, connected to 24 and started writing my test data.

  3. Ran my test program, connected to 25 and was able to read the test data from it

homebrew2k9
A: 

I use com0com and it works great for what I need.

Tim