Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# serialport strings
I am display data which is coming at COM Port. I am saving that incoming data to a String and then displaying sub string from that string.
 
I need to save substrings with TimeStamp to a SQL Server Database. What i need to do for it ?
 

 
private void DisplayData(MessageType type, string msg)
        {
           richTextBox1.Invoke(new EventHandler(delegate
            {
 
                string substring = string.Empty;
              if (msg.Length > 54)
                    substring = msg.Substring(54, 3);
                 richTextBox1.AppendText(substring) ;
 
            }));
        }
        #endregion
Posted 30-Dec-12 21:43pm
Edited 15-Jan-13 20:03pm
v2
Comments
Sergey Alexandrovich Kryukov at 31-Dec-12 2:48am
   
Do you use Invoke because you call DisplayData in a separate thread? That would be right thing to do.
So, I don't see the problem. Probably you need to show how you read the data. It's very usual that the length of string is different, but String.Length can also be different...
—SA
Zoltán Zörgő at 31-Dec-12 2:49am
   
How could we give you an answer without knowing what is the format of your input string, ant what do you want to cut from the string? So, if you can not formulate the logic of it in English, at least give us several examples of the input string and the substring you need.
ontheline89 at 31-Dec-12 2:53am
   
7E 45 00 FF FF 00 01 0A 00 93 00 00 01 00 00 01 00 13 18 75 51 9D 7E
This is a string.
But number of values in strings are changing with time.
How can i set indexes of a string when its indexes are changing with time.
Zoltán Zörgő at 31-Dec-12 2:56am
   
But what is the logic in it? Is it a sequence, let's say every first starts with 6, every second with 9, every third with 7? Or the substring starting index is constant, only the length of the input varies?
ontheline89 at 31-Dec-12 3:00am
   
The length of a string is varying.
It gives error " Index and length must refer to a location within the string. "
Zoltán Zörgő at 31-Dec-12 3:09am
   
Ok. In the example you gave above, you have 23 characters. The code is still passing the (msg.Length > 54) condition?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

All you need to check is change the length checking from 54 to 54+3 57 which make sure's that you have total 57 strings and it executes the parse only when 54 +3 char's are in msg string.
 
/Edit
was it like this?
if (msg.Length > 57)
           substring = msg.Substring(54, 3); 
it shuld work for sure
  Permalink  
v2
Comments
ontheline89 at 31-Dec-12 2:56am
   
i have checked it, string length is changing.
Most of time it gives about 57 value long string, but after some time, its length reduces.
Zoltán Zörgő at 31-Dec-12 2:58am
   
So you don't know the logic? What are you trying to achieve with this program anyway?
jibesh at 31-Dec-12 3:01am
   
change the code if (msg.Length > 54) to if (msg.Length > 57) and if length below 57 you can discard because system should respond only the valid values not to other junk data.
ontheline89 at 31-Dec-12 3:03am
   
Jibesh i did this. But it still gives error after displaying some values correctly.
Index and length must refer to a location within the string. ERROR
jibesh at 31-Dec-12 3:05am
   
can you paste the code.. i guess something you did wrong.
 
was it like this?
if (msg.Length > 57)
substring = msg.Substring(54, 3); it shuld work for sure
ontheline89 at 31-Dec-12 3:08am
   
Thanks you Jibesh its working now correctly. I did this as you said.
if (msg.Length > 57)
substring = msg.Substring(54, 3);
jibesh at 31-Dec-12 3:10am
   
great. happy to hear that.
ontheline89 at 31-Dec-12 3:18am
   
thanks .... Jibesh you helped me a lot ,
one more thing i need to ask from you.
I am getting values which are in Hexadecimal , i need to convert Sub Sting in Decimal. what should i do ?
jibesh at 31-Dec-12 3:26am
   
let me try. have you tried searching in google? or in CP
ontheline89 at 31-Dec-12 3:28am
   
i tried it ,
int decValue = Convert.ToInt32(hexValue, 16);
not working
ontheline89 at 1-Jan-13 9:29am
   
Jibesh can you please tell one thing more, i am trying to play a .wav file when sub string value is 1B , but i am getting Int to string conversion errors .....private void DisplayData(MessageType type, string msg)
{
richTextBox1.Invoke(new EventHandler(delegate
{
 

 
string substring = string.Empty;
if (msg.Length > 57)
substring = msg.Substring(54, 3);
if (substring = 1B)
playSimpleSound();
 

 

richTextBox1.AppendText(substring) ;

 

richTextBox1.SelectedText = string.Empty;
 
richTextBox1.ScrollToCaret();
 

}));
}
#endregion
 

public void playSimpleSound()
{
SoundPlayer simpleSound = new SoundPlayer(@"c:\smokealarm.wav");
simpleSound.Play();
}
jibesh at 2-Jan-13 0:56am
   
since its a string you need to perform string comparison. change the if comparison like below
if( substring == "1B")
playSimpleSound
ontheline89 at 16-Jan-13 0:14am
   
I have just seen your reply. Thank you so much !
ontheline89 at 16-Jan-13 8:58am
   
Hello Jibesh,
If i need to store these substring to a SQL Server Database with TimeStamp then what should i do? Thanks
jibesh at 16-Jan-13 14:32pm
   
I really dont understand substring with TimeStamp. If you are talking about the time data received at your side then you can use DateTime.Now in your code to get the current time and use these values to insert into your database.
 
Hope you already have database inplace and you know how to insert a value into it.
ontheline89 at 17-Jan-13 0:39am
   
Thank you for your reply,
jibesh at 31-Dec-12 3:11am
   
I wonder who down voted my solution would care to explain why?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,192
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100