2
votes

I have a C# program that reads from two serial ports at the same time. The serial port device is a Prolific USD to 4 serial ports adapter and I plug the two hardware on separate ports of the adapter. The problem is when I read from each port one at a time, everything works fine but when I try to read from both ports at the same time, one of the port is not responding. To troubleshoot the problem, I started two instances of the application and was able to read from the two ports at a time (one from each instance of the application). Does anyone know how to read from two separate serial ports in one application at the same time? Thank you.

Adding some codes:

Port 1:

// button to start or stop reading from port 1. Because the hardware requires me to write to it before reading the response, the writing is done in the timer

    private void buttonPort1_Click(object sender, EventArgs e)
    {
        if (buttonPort1.Text == "Start Recording")
        {
            if (!port1.IsOpen)
            {
                port1.Open();
            }
            timerPort1.Start();                
            buttonPort1.Text = "Stop Recording";
        }
        else
        {
            timerPort1.Stop();
            buttonPort1.Text = "Start Recording";
        }
    }

// Write "D" to the hardware each time to receive back the response

    private void timerPort1_Tick(object sender, EventArgs e)
    {               
            port1.Write("D");
    }


void port1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            string result = port1.ReadLine();
            oneParamDelegate dg = PHandCondResult;  // send back the result to the main thread
            this.Invoke(dg, result);
        }
        catch
        {
        }

    }

Port 2

The code for the second port is similar to the above really, the difference being different port, datareceived event and timer.

I'll try the multiple thread options suggested by Grant Thomas: I didn't try this before because I thought serial ports are already working on separate threads: the datareceived event doesn't block the main thread and you can't access controls created on the main thread but I'll still give it a go using background worker and revert back later. Thank you all for the quick response.

2
Could you add some code?Alex Filipovici
Are you using multiple threads or just a single one?Eric Smekens
Do you implement the dataReceived event this way: msdn.microsoft.com/en-us/library/… ? For me it is unclear what spPH is.Eric Smekens
@EricSmekens, spPH is port1. I renamed it to make it a bit easier for people to read. My port1 is actually reading a pH meter. I have now corrected the error. Thanks.falopsy
Okay, than you could indeed try threading if it solves your problem.Eric Smekens

2 Answers

5
votes

You're going to need to do some reading, specifically on Threading.

If you have, say, some code that looks like this:

ReadDataFromSomePort();
ReadDataFromSomeOtherPort();

Then the first will execute synchronously (blocking) and then the latter. This happens on the same thread, the main application thread. When you want to do asynchronous things, including just doing one thing while keeping a UI interactive/responsive, then you need to delegate work to other threads.

So, you end up with something like this:

var thread1 = new Thread(ReadDataFromSomePort);
var thread2 = new Thread(ReadDataFromSomeOtherPort);

thread1.Start();
thread2.Start();

There's more to it than this, rest assured, so I recommend some research on the concept before proceeding.

MSDN has a tutorial/programming reference for threading that should get you started.

1
votes

Creating two different objects of SerialPort and different DataReceived events for both should work.