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?
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.
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.
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.
Re: Multithreading for multiple serial ports
oh RS485, familiar name... are you using modBus protocol?
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
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
Re: Multithreading for multiple serial ports
Don't revive code from many years ago, as nobody cares. Start a new thread