Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

am receiving excel sheet data from serialport, when i was received , it's displaying complete rows.

receiving data is in following format:

$ POST,T121,c123,raj,2000,200,12/11/2013,1800 # $ POST,T122,c122,thilak,3000,300,12/10/2013,2700 # $ POST,T123,c123,nagendra,2000,2000,11/09/2013,0 #

but i need to read row by row .$ -->start of row #-->end of row

After reading data i need to place in corresponding column of another excel sheet.here , is the delimeter

here is the code:
C#
 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
Thread.Sleep(50);
                        byte[] buffer=new byte[serialPort1.BytesToRead];
                        serialPort1.Read(buffer,0,buffer.Length);
                        string s = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
                        MessageBox.Show(s);
              }
Posted

1 solution

So, either use string.Split and then string.Trim each line, or use a regex to split and trim at the same time.

Probably, I'd go with a regex, then use Linq to tidy up afterwards:
C#
string inp = "$ POST,T121,c123,raj,2000,200,12/11/2013,1800 # $ POST,T122,c122,thilak,3000,300,12/10/2013,2700 # $ POST,T123,c123,nagendra,2000,2000,11/09/2013,0 # ";
string[] lines = Regex.Split(inp, "[$#]").Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => s.Trim()).ToArray();
 
Share this answer
 
Comments
Member 10263519 15-Dec-13 23:58pm    
hi,
thanq ,my string stars with $ and ends with #

in between them all the words based on delimeter , i need to split that string and at the same time need to place in excel sheet.
Member 10263519 16-Dec-13 0:04am    
data is coming from excel through serial port
Member 10263519 16-Dec-13 0:44am    
Regex is in which namespace
Member 10263519 16-Dec-13 1:26am    
ok thanq
OriginalGriff 16-Dec-13 3:19am    
You're welcome!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900