none
SerialPort works from VS2010 IDE, but not from the exe ? RRS feed

  • Question

  • Hello,

    I have written a modbus-like application, that polls an attached instrument for data via the serial port (a USB to Serial Adapter).

    While reading from the Slave, in case of no response (read timeout), seven attempts are made. If all seven result in a read timeout, I have set up an error message that there is no Slave connected.

    Now everything works perfectly while debugging (F5) from the VS2010 IDE.

    However, in the deployed application, or even from the Bin (Debug or Release) the Application sends all 7 requests in quick succession, without waiting for the timeout (I checked with a Serial Port Sniffer) causing the request to fail ('Slave not Present' error) .

    Why is this happening ?

    Thanks in Advance !

    Saturday, August 4, 2012 1:22 PM

Answers

  • How can we know that without code sample?

    You need a more relaxed time-out handling as you said it works while debugging.

    • Marked as answer by Monostable Saturday, August 4, 2012 3:05 PM
    Saturday, August 4, 2012 2:08 PM
  • Yes Stefan, You are right.

    I guess debugging involves overheads, causing the read timeout to run "slower".

    Outside of VS2010, the program runs at full speed, causing the read to timeout "faster".

    Thank You.

    • Marked as answer by Monostable Saturday, August 4, 2012 3:08 PM
    Saturday, August 4, 2012 3:07 PM

All replies

  • How can we know that without code sample?

    You need a more relaxed time-out handling as you said it works while debugging.

    • Marked as answer by Monostable Saturday, August 4, 2012 3:05 PM
    Saturday, August 4, 2012 2:08 PM
  • Yes Stefan, You are right.

    I guess debugging involves overheads, causing the read timeout to run "slower".

    Outside of VS2010, the program runs at full speed, causing the read to timeout "faster".

    Thank You.

    • Marked as answer by Monostable Saturday, August 4, 2012 3:08 PM
    Saturday, August 4, 2012 3:07 PM
  • Hi Monostable,

    I am having the same problem.

    I have created an application which reads GPS coordinates from a GPS device using Serial Com Port.

    When I run my application through VS2010 IDE, then it work fine.

    But when I run it from the exe it doesn't read from Serial COM port.

    So what did you actually do?

    1. Did you use System.Threading.Thread.Sleep before ReadFile.

    2. Did you change the milliseconds of "COMMTIMEOUTS" for SetCommTimeouts.

    Could you please help me out. Thanks.


    Wednesday, May 22, 2013 1:42 PM
  • The most important part is to setup the port correctly.

    I always use the "full"  SerialPort Constructor (String, Int32, Parity, Int32, StopBits).

    For a simple device it is often COMx, 9600, Parity.None, 8, StopBits.One as these are the most common used settings.

    But you should first read your devices manual and - I had often that case - you really need to test it carefully with different baud rates (2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200). Also take a look at the values of SerialPort.ReadTimeout and SerialPort.ReadBufferSize.

    Wednesday, May 22, 2013 3:07 PM
  • I believe I have already given a hint in my earlier reply...

    I had a few 'soft' timeouts that i used to detect absence of a valid slave...they timed out if the slave did not answer in a reasonable time.

    These timeouts were sufficient in debug mode, because the debug mode slowed them down due to its own overhead making the program run slower.

    In actual run mode, the program ran a full speed, and caused the timeouts to lapse too quickly.

    I simply increased the values of all timeouts and delays i'd used, & solved the problem.

    You should check for similar issues.



    • Edited by Monostable Wednesday, May 22, 2013 7:11 PM
    Wednesday, May 22, 2013 7:04 PM
  • Thanks Stefan for your reply.

    I am actually not using "SerialPort" class rather I am using "kernel32.dll"

    Sample of my code:

    Private Const FILE_SHARE_READ As Integer = &H1
    Private Const FILE_SHARE_WRITE As Integer = &H2

    Private Const OPEN_EXISTING As Integer = &H3

    Private Const GENERIC_READ As Integer = &H80000000
    Private Const GENERIC_WRITE As Integer = &H40000000

    Dim lpCommTimeouts As COMMTIMEOUTS

    '----- open comm port and get handle to device
    intHandle = CreateFile(myPortName, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ + FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)

    '----- set comm port timeouts mintTimeout miliseconds
    lpCommTimeouts.ReadIntervalTimeout = 0
    lpCommTimeouts.ReadTotalTimeoutMultiplier = 0
    lpCommTimeouts.ReadTotalTimeoutConstant = timeout
    lpCommTimeouts.WriteTotalTimeoutMultiplier = 10
    lpCommTimeouts.WriteTotalTimeoutConstant = 100

    intResult = SetCommTimeouts(intHandle, lpCommTimeouts)


    intResult = ReadFile(Me.m_intHandle, Me.m_byteRxBuffer, number, lpNumberOfBytesRead, 0) 'It gives 0 While running the exe

    If intResult = 0 Then

     Throw New Exception("Error reading COM:" & Me.m_intPort)

    Else

     'Parse GPS Data

    End If


    Thursday, May 23, 2013 4:39 AM
  • Thanks Monostable for your reply.

    I think you are also using "SerialPort" instead of "kernel32.dll".

    I am going to change the timeouts in my application esp. in the method "SetCommTimeouts" and will let you know.

    Thanks once again.

    Thursday, May 23, 2013 4:43 AM
  • Thanks a lot Stefan and Monostable for your support.

    I have resolved the issue, just by calling the method "ReadFile" of "kernel32.dll" again on failure:

    Dim reTryCount As Integer = 1

    reTryCountReadFileLabel:

     

    intResult = ReadFile(

    Me.m_intHandle, Me.m_byteRxBuffer, number, lpNumberOfBytesRead, 0)

    If intResult = 0 Then

    If reTryCount < 3 Then

    reTryCount = reTryCount + 1

    GoTo reTryCountReadFileLabel

    End If

    Thursday, May 23, 2013 10:00 AM