Reading and Writing serial ports simultaneously using threads
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Reading and Writing serial ports simultaneously using threads

  1. #1
    Join Date
    Mar 2012
    Posts
    1

    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:
    Code:
    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:
    Code:
    n = read(this->port, serialBuffer, ASERIAL_BUFFER_SIZE);

    To read, thread B uses:
    Code:
    n = read(port2, serialBuffer, ASERIAL_BUFFER_SIZE);
    To write, thread B uses:
    Code:
    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.

    Thanks

  2. #2
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center