-
Mar 23rd, 2023, 01:40 AM
#1
Thread Starter
Addicted Member
SerialPort Change Configuration in runtime
Hello,
I need to use RS232 with an old device that use different configuration for TX and RX:
TX:
9600 bps, Parity.Even, data bit 8
RX
9600 bps, Parity.None, data bit 7
I've tried to switch them in runtime, without close the port, but It not work very well, sometime TX command are not received from device, and may time the buffer RX buffer is corrupted. So I tried to
Port.Close before the configuration switch. Command sended(TX) work fine, but I have iusse in RX: I close DataReceived event to retrive data and parse it and then call the TX routine to answer, when I close the port to switch to TX it exit from the RX event or the TX routine (if called from the event).
My switch routines is similar to:
SerialPort1.Close;
'Here Change Config.
SerialPort1.Open
Which is the best way to go? Any advice?
-
Mar 23rd, 2023, 11:35 AM
#2
Hyperactive Member
Re: SerialPort Change Configuration in runtime
I don't memorize DB9 COM port scientific documentation but I'm totally sure you are not able to change something when a port is open. You are not even able to send/listen a port using by another application (But there are serial port sniffers/spiers which can listen asynchronously).
- About dataloss and corrupted buffer, Can you provide your core code?
- Or even what you have tried so far?
- What are you trying to perform (In case there's a better/easier way.)
- Is there an exception/error occur?
I found following thread by "dbasnett" very useful I came up with similar approach in my every SP projects now on. 
https://www.vbforums.com/showthread....=1#post5568506
-
Mar 23rd, 2023, 12:43 PM
#3
Re: SerialPort Change Configuration in runtime
The best friend of any programmer is a search engine 
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Mar 23rd, 2023, 03:30 PM
#4
Thread Starter
Addicted Member
Re: SerialPort Change Configuration in runtime
 Originally Posted by pourkascheff
I don't memorize DB9 COM port scientific documentation but I'm totally sure you are not able to change something when a port is open. You are not even able to send/listen a port using by another application (But there are serial port sniffers/spiers which can listen asynchronously).
- About dataloss and corrupted buffer, Can you provide your core code?
- Or even what you have tried so far?
- What are you trying to perform (In case there's a better/easier way.)
- Is there an exception/error occur?
I found following thread by "dbasnett" very useful I came up with similar approach in my every SP projects now on.
https://www.vbforums.com/showthread....=1#post5568506
I'm looking at the code you linked; My code is similar, but I used only function and Subs, none Tasks, since I'm not familiar with it.
Last edited by hannibal smith; Mar 24th, 2023 at 03:50 AM.
-
Mar 23rd, 2023, 03:48 PM
#5
Re: SerialPort Change Configuration in runtime
I don't believe that dbasnett's code or tutorial would cover having to have different configurations for Tx and Rx simultaneously, or quickly switching the port between a transmit configuration and a receive configuration for every transmit and receive.
That seems like an odd requirement and I can't find any sites that mention such a requirement.
It seems to me if this is an actual requirement for some piece of hardware, I would consider creating a custom cable that uses two serial connections on one end that would split the transmit related pins on one connector, and the receive related pins on another connector, and then you would have to open two com ports, and use one (the appropriate one) for transmitting and one for receiving. That way you can set up each for the number of bits and parity that each direction require.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Mar 24th, 2023, 03:56 AM
#6
Thread Starter
Addicted Member
Re: SerialPort Change Configuration in runtime
 Originally Posted by passel
I don't believe that dbasnett's code or tutorial would cover having to have different configurations for Tx and Rx simultaneously, or quickly switching the port between a transmit configuration and a receive configuration for every transmit and receive.
That seems like an odd requirement and I can't find any sites that mention such a requirement.
It seems to me if this is an actual requirement for some piece of hardware, I would consider creating a custom cable that uses two serial connections on one end that would split the transmit related pins on one connector, and the receive related pins on another connector, and then you would have to open two com ports, and use one (the appropriate one) for transmitting and one for receiving. That way you can set up each for the number of bits and parity that each direction require.
You are right it not cover it.
I can't build a custom cable since is not db9 connection, but a FT232 into the device board.
I don't need to switch fast, only switch, data that coming from device stilll be sended every 5 sec if PC does not respond with aother command.
I tryed to close the port by calling a sub from datareceived event, but when port is closed, the evnet is killed, so it not execute the code to reopen the port with new config.
I think the way to go is the one from the dbasnett. It is not easy for me, since I'm struggling to undestand async programmation.
-
Mar 24th, 2023, 08:18 AM
#7
Re: SerialPort Change Configuration in runtime
Have you tried setting parity to none and databits to 8?
-
Mar 24th, 2023, 10:27 AM
#8
Thread Starter
Addicted Member
Re: SerialPort Change Configuration in runtime
 Originally Posted by dbasnett
Have you tried setting parity to none and databits to 8?
Of Course.
It not work, I get wrong chars.
What I already tried:ù
change config without close and reopen the port: I get some garbage in rx and sometime device did not get the command. As workaround I putted a delay of 0.5 sec after config change and clean the buffer. It work but is a very dirty way.
I close, change config and reopen:not work because I've close the port inside a method called from datareceived event, and closing the port it exit from it without complete the work.
I'm going to try to add che cloese/config change/reopen inside the dataproces task of this code:
https://www.vbforums.com/showthread....=1#post5568506
after checked and parse the data received.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|