Click here to Skip to main content
11,920,156 members (58,355 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# string serialport
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) ;
Posted 30-Dec-12 21:43pm
Edited 15-Jan-13 20:03pm
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...
Zoltán Zörgő 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 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ő 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 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ő 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
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.

was it like this?
if (msg.Length > 57)
           substring = msg.Substring(54, 3); 
it shuld work for sure
ontheline89 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ő 31-Dec-12 2:58am
So you don't know the logic? What are you trying to achieve with this program anyway?
jibesh 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 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 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 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 31-Dec-12 3:10am
great. happy to hear that.
ontheline89 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 31-Dec-12 3:26am
let me try. have you tried searching in google? or in CP
ontheline89 31-Dec-12 3:28am
i tried it ,
int decValue = Convert.ToInt32(hexValue, 16);
not working
ontheline89 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)

richTextBox1.AppendText(substring) ;

richTextBox1.SelectedText = string.Empty;



public void playSimpleSound()
SoundPlayer simpleSound = new SoundPlayer(@"c:\smokealarm.wav");
jibesh 2-Jan-13 0:56am
since its a string you need to perform string comparison. change the if comparison like below
if( substring == "1B")
ontheline89 16-Jan-13 0:14am
I have just seen your reply. Thank you so much !
ontheline89 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 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 17-Jan-13 0:39am
Thank you for your reply,
jibesh 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 455
1 phil.o 170
2 CPallini 150
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,153
3 George Jonsson 2,810
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web04 | 2.8.151120.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2015
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