Serial Communication
I have a Program that uses serial port communication to talk to some hardware. No problem. Then I added two GSM Modens to the picture. In VB I opened the COM port to the modem with one protocol (8 data, none, 1) and when the communication between modems was open and running I closed the port and reopened it with a new protocol (7 data, even parity, 2 stop) to my hardware. The hardware is standardized to this speed several years ago so I can not change this and most modems do not support 7, e , 2 protocol.
I want to expand the support of modems and many of them break contact when the port is closed. I'm not using hardware 开发者_如何学Chandshaking.
So here is my question. How can I best change between protocols with out closing the serial port?
- Does anybody have another idea how this can be done without changing any hardware settings?
- Make a virtual serial port? (how?)
- Ideas?
To truly set all options of your serial connection you should use the Win32 API communication functions. As far as i know you don't need to close and re-open a port to get these settings active. The will work right after setting them (like in this example).
You just have to take care, if you want to change just one or two settings, that you have to prefill you structure with the corresponding GetX function, make your changes and call the SetX function.
Maybe in contrast to the sentence before, but don't rely on default settings on your first SetX call. I've seen a lot of programs just altering the popular settings (e.g baudrate, stopp bits, etc.) but not the rare ones (like XoffChar, etc). If you just do that and another program will change these settings you start wondering why your program is suddenly not able to communicate anymore with your serial device, because you use the settings which another program set to the serial port.
And before i forget, if you like to use Win32 API functions in C# take a look at PInvoke.net
精彩评论