Multithreading for multiple serial ports
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Multithreading for multiple serial ports

  1. #1
    Join Date
    Dec 2011
    Posts
    2

    Multithreading for multiple serial ports

    I have a PC with 16 RS485 serial ports attached (using 2 MOXA PCI to serial port cards).
    I need to download the contents of a file (with binary data) to the devices attached to all 16 serial ports simultaneously.

    I've created and launched 16 BackgroundWorkers to manage the download for each port. Each BackgroundWorker runs a method that opens a specified COM port. I use a timer with each serial port to manage reception timeout handling. I use the StopWatch class to generate dealys where required. Each BackgroundWorker also opens a FileStream with reach only access and fileshare settings on the one file to be used for downloading to all 16 ports.

    There are some bizarre things happening here! The timer used with the SerialPorts doesn't appear to operate, received data is not generating received data events and there are spontaneous closures of some of the Filestreams!

    Is my approach to managing multiple downloads a valid one to use? Can anyone suggest either why I'm seeing the problems described and how to solve them, or a better method to performing the simultaneous download to multiple ports?

  2. #2
    Join Date
    Dec 2011
    Posts
    2

    Re: Multithreading for multiple serial ports

    Problem solved.
    The System.Timers.Timer used with the serial port was declared as a static. Once removed, the issue went away.

    I found it was also necessary to call System.Threading.ThreadPool.SetMinThreads to set a suitable number of idle threads to be available to perform the downloads efficiently.

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: Multithreading for multiple serial ports

    Glad it worked out. I don't know that I've heard of someone using so many serial ports. I am curious: how do you address them all? Do you really just get COM1 through COM16? Or is it something else?

    Good luck in the rest of your project; sounds neat.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,845

    Re: Multithreading for multiple serial ports

    I have had to use as many as 8 before each had the next sequencial com number, Seems like they started at 5 on the digi board we used but it has been a long time so memory is foggy now. In my case the system was monitoring 8 phone lines for incoming calls to receive sales orders from people in the field.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Dec 2011
    Posts
    61

    Re: Multithreading for multiple serial ports

    oh RS485, familiar name... are you using modBus protocol?

  6. #6
    Join Date
    May 2012
    Posts
    1

    Re: Multithreading for multiple serial ports

    Hi oz_peter,
    Could you plz share your code as am looking for similar kinda apps multiport serial communication.Thx Sam

  7. #7
    Join Date
    Jul 2014
    Posts
    1

    Re: Multithreading for multiple serial ports

    Hi oz_peter, can you please share your solution, I'm try to handle 8 comport receives.

    Thanks in advance

  8. #8
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,990

    Re: Multithreading for multiple serial ports

    Don't revive code from many years ago, as nobody cares. Start a new thread
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Tags for this Thread

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

This is a CodeGuru survey question.


Featured


HTML5 Development Center