none
Opening a serial port in vb RRS feed

  • Question

  • Hi

    I am trying to talk to an Arduino from a vb app on my PC. I have a simple bit of code sitting inside a Try...Catch structure that looks like this

    ...

                    EMPort.Open()
                    EMPort.ReadTimeout = 500
                    EMPort.Write("?")
                    Response = EMPort.ReadLine()

    ...

    where EMPort is the name I gave to the Toolkit widget I dropped into my Design (COM3 at 115200 baud (defined in the Properties pane) via CH340 USB/RS232 adapter).

    The problem I have is:

    1. It runs exactly as it should inside VS2017 while debugging.

    2. When I publish it and run it, it still runs fine, until I reset the port (by unplugging and re-plugging the USB cable).  Then, on the first pass, it gives me a Receive TimeOut Exception.  My code is such that this error takes me back and I can try again - and on the second (and subsequent) pass (changing nothing) it works fine.

    3. I f I reset the port, then reload VS2017 (without opening any projects, just looking at the home page) it runs fine on the first pass and subsequently.

    4. I have no trouble under any conditions in talking from puTTy to the Arduino via COM3.

    3 and 4 lead me to suspect that I am missing something with opening the port correctly in vb.

    Does anyone have any ideas?

    Wednesday, March 21, 2018 1:44 PM

Answers

  • In a nutshell, when I first start up from cold, I get the timeout  error, but second time through, and after that, it all works fine.

    That would be an issue with the device, or the particular installation.  For instance, does the device return an error code for an invalid command?  In that case the port failure might be due to a partial transmission from the device.  Or, does the installation execute some code at startup that tests the serial ports, for instance to look for a mouse?  That could put a port in an invalid state.   Serial port usage is not a precise art - handling error states is par for the course.

    • Marked as answer by Romeo986 Tuesday, March 27, 2018 12:01 AM
    Thursday, March 22, 2018 9:59 PM

All replies

  • Hi Romeo986,

    Welcome to the MSDN forum.

    It seems your issue is about the VB.NET development and since our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum for a more professional support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Thursday, March 22, 2018 2:17 AM
  • 3 and 4 lead me to suspect that I am missing something with opening the port correctly in vb.

    I would recommend monitoring the port status to detect an unplugging.  Then you can clear any pending operations and close and re-open the port from code when you detect the reconnect.  

    https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.errorreceived(v=vs.110).aspx
    https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.pinchanged(v=vs.110).aspx


    • Edited by Acamar Thursday, March 22, 2018 5:09 AM
    Thursday, March 22, 2018 5:08 AM
  • Thanks, Sara - Sorry about that.

    Newbie :-(

    Roman

    Thursday, March 22, 2018 5:23 AM
  • Acamar,

    Thanks, but that's not actually the issue... In a nutshell, when I first start up from cold, I get the timeout  error, but second time through, and after that, it all works fine. So - how do I reliably open this port up first time in my vb program, without having to run it twice? (Incidentally, I tried code that opened - closed - and reopened the port, but that didn't help),

    Of course, that work-around is quite simple, and I am using it as I write this - but I am one of those funny guys who likes to understand what's happening, rather than just having something that "kinda" works:-)

    Roman

    Thursday, March 22, 2018 5:30 AM
  • Thanks, Sara - Sorry about that.

    Newbie :-(

    Roman


    I am confused.  In your first post you said it ran fine until you unplugged / plugged the cable?  Perhaps initially the port timeout should be raised.

    If this is a USB serial port then unplugging / plugging while the app is running is not recommended.  There are many bug reports about how that results in unforeseen behavior.

    Also, I wouldn't use blocking methods for the serial port.  I never had much luck with that approach.  In your example you send a character and then block until a line is available.  How long did it take before the character was actually sent?  How long did it take for the other end to receive and process the character?  How long did it take for the other end to send the response?

    The best approach is to use the DataReceived event. 


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

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Thursday, March 22, 2018 1:13 PM
  • In a nutshell, when I first start up from cold, I get the timeout  error, but second time through, and after that, it all works fine.

    That would be an issue with the device, or the particular installation.  For instance, does the device return an error code for an invalid command?  In that case the port failure might be due to a partial transmission from the device.  Or, does the installation execute some code at startup that tests the serial ports, for instance to look for a mouse?  That could put a port in an invalid state.   Serial port usage is not a precise art - handling error states is par for the course.

    • Marked as answer by Romeo986 Tuesday, March 27, 2018 12:01 AM
    Thursday, March 22, 2018 9:59 PM
  • Acamar,

    Yes, I just re-read my original question, and it is indeed a little ambiguous - sorry.

    But your answer has given me a thought - I shall go investigate the target a little more closely, even tho the code in that is even simpler than my vb snippet..

    Roman 

    Friday, March 23, 2018 12:31 PM
  • Acamar,

    Your questions pointed me in the right direction.

    It turns out that this is an artefact of the behaviour of the Arduino/Port combination.  For the possible benefit of others....

    when the port is opened on the windows machine, there is some interaction with the Arduino (presumably something to do with the CH340 USB/RS232 converter) which causes the Arduino to reset, meaning that it's unable to accept characters for about 1s. Hence the reason why the problem occurred on the first pass, but subsequently worked fine. (Incidentally, the start bit of the response from the Arduino comes back within 8uS of the end of the stop bit of the query - I love my new Logic Analyser!)

    I fixed it simply by putting the thread to sleep for a little while after opening the port.

    I suspect that there are far more elegant ways of doing these things - I note John Wein's comments about blocking and using Port Events - but for now, this works for me.

    Thank you all for your ideas.

    Roman

    Tuesday, March 27, 2018 12:01 AM