Results 1 to 8 of 8

Thread: SerialPort Change Configuration in runtime

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    171

    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?

  2. #2
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    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

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    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)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    171

    Re: SerialPort Change Configuration in runtime

    Quote Originally Posted by pourkascheff View Post
    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.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    171

    Re: SerialPort Change Configuration in runtime

    Quote Originally Posted by passel View Post
    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.

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: SerialPort Change Configuration in runtime

    Have you tried setting parity to none and databits to 8?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    171

    Re: SerialPort Change Configuration in runtime

    Quote Originally Posted by dbasnett View Post
    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
  •  



Click Here to Expand Forum to Full Width