CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2012

    Reading and Writing serial ports simultaneously using threads

    I am using the boost thread library but I am a new user. I have written some code but I am still at the planning stage of my program.

    I first set up two serial port connections using:
    port1 = open(portName1, O_RDWR | O_NOCTTY | O_NONBLOCK);
    this->port = port1;
    port2 = open(portName2, O_RDWR | O_NOCTTY | O_NONBLOCK);

    Then I produce two threads. They share port2 a global variable.

    Thread A reads from serial port 1 continuously and prints what it receives.
    Thread B reads from serial port 2. When thread B has a full packet received it writes a counter to serial port 1 and then increments a counter.
    The device attached to serial port 1 writes what it receives so that thread A receives what thread B sends.

    To read, thread A uses:
    n = read(this->port, serialBuffer, ASERIAL_BUFFER_SIZE);

    To read, thread B uses:
    n = read(port2, serialBuffer, ASERIAL_BUFFER_SIZE);
    To write, thread B uses:
    n = write(port1, &(this->counter),2);
    this->counter = this->counter + 1;
    I want thread B to write preferably without having to wait for thread A to stop reading. Is this possible? I want to know if I need to use a mutex.


  2. #2
    Join Date
    Feb 2005
    Madrid (Spain)

    Re: Reading and Writing serial ports simultaneously using threads

    You can use Mutex, Semaphore or you can use MSMQ to write data with thread 2 and check the queue with thread 1.

    Best regards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

By using this site, you agree to the Privacy Policy