Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to split string between '┐' '└' which is capture from serial port. In the application I have developed I'm using serialport.readexisting(); to capture data & insert data in to a data base table for processing. Device passes the data to serial port like I given in Sample String as a data series.

Sample String:
XML
┐1 1.26582 0.0088└┐1 1.26682 0.0093└┐1 1.26922 0.0089└┐1 1.27172 0.0084└..............

Required Format
C#
┐1 1.26582 0.0088└
┐1 1.26682 0.0093└
┐1 1.26922 0.0089└
┐1 1.27172 0.0084└.
.
.
.
.


This is my code:
C#
Data = Port1.ReadExisting();
display.Invoke(new Action(() =>
{
    display.AppendText(Data);
        }));

com.CommandText = "insert into RawData (datetime,data) values('" +DateTime.Now.ToString() + "','" + Data + "')";
com.ExecuteNonQuery();
Posted
Updated 12-Jun-14 0:10am
v2

1 solution

That probably won't work: you are looking for specific byte values, which may (or may not) get the characters correctly: the chances are that they are control codes, which do not convert nicely to Unicode characters (and may convert differently depending on the locale).

Instead, read the data from your serial port as bytes, and look for the specific control code bytes which the device is transmitting. Extract the data between the bytes, convert that to Unicode characters and then save it to your DB.

Playing with strings from external devices that aren't sending string based data is a recipe for horrible problems later.
 
Share this answer
 
Comments
glennPattonWork3 12-Jun-14 10:43am    
"Playing with strings from external devices that aren't sending string based data is a recipe for horrible problems later." could have said it better myself! You could use a ReadTo() rather than the Readexisting() as I have forund ReadExisting is a little unreliable...

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