locked
ystem.ArgumentOutOfRangeException was unhandled when received data from serial port RRS feed

  • Question

  • i am usinf window form desktop application when received data  from serial port following error show .

    System.ArgumentOutOfRangeException was unhandled
      HResult=-2146233086
      Message=Index and length must refer to a location within the string.
    Parameter name: length
      ParamName=length
      Source=mscorlib
      StackTrace:
           at System.String.Substring(Int32 startIndex, Int32 length)
           at MYKX21_Final.Screens.MainForm.serialPort1_DataReceived(Object sender, SerialDataReceivedEventArgs e) in C:\Users\SAQ\Desktop\InterfacingSoftware V8\MYKX21-Final\Screens\MainForm.cs:line 235
           at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src, SerialDataReceivedEventArgs e)
           at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object state)
           at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
           at System.Threading.ThreadPoolWorkQueue.Dispatch()
           at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
      InnerException: 

    • Edited by saqsaqPK Thursday, April 26, 2018 10:13 AM update
    Thursday, April 26, 2018 10:11 AM

Answers

  • There's a few mentions in forums about getting the full data in DataReceived (e.g. this).

    It implies you should check the BytesToRead property to see if you have read everything. e.g

    string dataIn = ''
    while(serialPort1.BytesToRead > 0)
    {
       dataIn += serialPort1.ReadExisting();
    }

    I'm not in a position to try this out unfortunately, but you can give it a go and see what happens. (The reason it probably works when you stop at a debug point is because that adds enough of a delay for all the data to arrive).


    • Edited by RJP1973 Thursday, April 26, 2018 4:31 PM
    • Marked as answer by saqsaqPK Thursday, April 26, 2018 5:16 PM
    Thursday, April 26, 2018 4:30 PM

All replies

  • As the error message says, somewhere you are calling String.Substring and passing in a length parameter that is too long (the start index plus the length must be within the range of the string - see the documentation here).

    You need to trace your code, maybe add a debug breakpoint, and see where you are calling string.Substring somewhere around your DataReceived event.

    Thursday, April 26, 2018 10:25 AM
  • Yes you right .  i am receiving data from serial port of device.  data contain  121 character.

    D1U18020000000E000001f00000S009000570101640048500851202880033800265003140015000536000280001400048004420013100098002530 

    Problem is date data  receiving under process so substing  not working properly , kindly advise us how to solved this issue . 

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                DataIN += serialPort1.ReadExisting();
                DataIN = DataIN.Replace("*", "0");
    
                // Declear All varibale 
                string STX = DataIN.Substring(0, 1);
                string TextDistinctionCodeI = DataIN.Substring(1, 1);
                string TextDistinctionCodeII = DataIN.Substring(2, 1);
    
    
                string SampleDistinctionCode = DataIN.Substring(3, 1);
    
               // Convert Date Format 
                string Date = DataIN.Substring(4, 6);
                string YearMonthDay = DataIN.Substring(4, 2);
                string MonthDay = DataIN.Substring(6, 2);
                string DayYear = DataIN.Substring(8, 2);
                string AnalysisInformation = DataIN.Substring(10, 1);
                string SampleID = DataIN.Substring(11, 12);
                string PDAInformation = DataIN.Substring(23, 6);
                string RDWSelectInformation = DataIN.Substring(29, 1);


    • Edited by saqsaqPK Thursday, April 26, 2018 1:44 PM update
    Thursday, April 26, 2018 1:41 PM
  • Are you really sure that your DataIN variable contains 121 characters?

    Because it looks like your debugger is only showing about 16 characters and I would normally expect it to show more if the variable did actually contain more characters than that.

    It is possible that not all your expected data is getting sent at once. I admit I don't know much about SerialPorts, but you may need to simply concatenate your data in the DataReceived event until it reaches the length you expect before you try to process it.


    • Edited by RJP1973 Thursday, April 26, 2018 2:07 PM
    Thursday, April 26, 2018 2:07 PM
  • Yes DataIN variable contains 121 characters. 

    i am not received complete data from serial port . 

    Kindly advise us how to concatenate my data in the DataReceived event until it reaches the length expect before try to process it. 




    • Edited by saqsaqPK Thursday, April 26, 2018 2:23 PM update
    Thursday, April 26, 2018 2:13 PM
  • My problem is that when debug stop string received complete 

    data process successfully . when i clear problem show ,

    is there is any way to solved this issue . 

    Thursday, April 26, 2018 4:04 PM
  • There's a few mentions in forums about getting the full data in DataReceived (e.g. this).

    It implies you should check the BytesToRead property to see if you have read everything. e.g

    string dataIn = ''
    while(serialPort1.BytesToRead > 0)
    {
       dataIn += serialPort1.ReadExisting();
    }

    I'm not in a position to try this out unfortunately, but you can give it a go and see what happens. (The reason it probably works when you stop at a debug point is because that adds enough of a delay for all the data to arrive).


    • Edited by RJP1973 Thursday, April 26, 2018 4:31 PM
    • Marked as answer by saqsaqPK Thursday, April 26, 2018 5:16 PM
    Thursday, April 26, 2018 4:30 PM
  • thanks for your replay . test the code but problem not solved .:(
    Thursday, April 26, 2018 4:53 PM
  • Thanks for your support . update the code and update  serialport property . 

    data received successfully .

     
    Thursday, April 26, 2018 5:16 PM