7 Replies - 986 Views - Last Post: 09 June 2011 - 07:56 PM Rate Topic: -----

#1 m_alien  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 13-May 11

Serial Port XML Data parsing errors

Posted 09 June 2011 - 04:34 PM

Hello

I am trying to parse incoming data from a serial port that appears to be XML format but a few problems arise when going from a simulated setup to the real deal.

I first made a this program at home using a serial port emulator and com port connector program that reads from a text file and then sends it through the com port, I then read it from the com port it is connected to it. My program is able to parse the incoming XML perfectly fine, I'm using an XML Literals syntax.

The problem I run into is when I setup my program with the device and try and parse the data that is coming out of it. The data I receive sometimes has a few lines of garble before it starts seeing my expected XML. Of course these few lines of garbled text throws me an exception, but even when it appears I'm getting a good clean start of my XML data my program still throws exceptions such as "unexpected end of line 1 line number (different lines)" or "multiple root elements line 1"

Am I going about this the right way? using XML Literals to process my XML string properly or should I use something else that will allow for not well formed XML? or am I just missing something.

My device outputs data every 500ms and I can poll my device for stored data. An example of what I am receiving sometimes and what I am expecting is below. "The first few lines of text sometimes appear and sometimes don't"

$#^$*^HKNKJO
%^TYUGMKKJHLJK*&^
^%$$RRER^RFFJJJGHJK
^%T*&*
&^%*&^(&)*(_)GHJB<N<LML
#%$#$^*^*^*(
<>?<:K:JKLHKJH
^%*&&Y*HKNB
<li840><data><celltemp>5.1946425e1</celltemp><cellpres>9.4315986e1</cellpres><co2>1.1377861</co2><co2abs>3.1518132e-4</co2abs><h2o>4.8807077e-1</h2o><h2oabs>3.9576592e-3</h2oabs><h2odewpoint>-3.1063446e1</h2odewpoint><ivolt>2.4082641e1</ivolt><raw><co2>2734394</co2><co2ref>2219112</co2ref><h2o>1489893</h2o><h2oref>1341055</h2oref></raw></data><cfg><heater>true</heater><pcomp>true</pcomp><filter>5</filter><outrate>1</outrate><alarms><source>co2</source><enabled>false</enabled><high>0</high><hdead>0</hdead><low>0</low><ldead>0</ldead></alarms><bench>14</bench><span>3000</span><dacs><range>5.0</range><d1>h2odewpoint</d1><d2>co2</d2></dacs></cfg><cal><co2lastzero>2011-5-9_at_13:00</co2lastzero><co2kzero>8.1121540e-1</co2kzero><co2lastspan>2011-5-9_at_13:14</co2lastspan><co2kspan>9.9754638e-1</co2kspan><h2olastzero>2011-5-9_at_13:00</h2olastzero><h2okzero>8.9662647e-1</h2okzero><h2olastspan>6 Jun 2007</h2olastspan><h2okspan>9.9288215e-1</h2okspan></cal><poly><date>HGA-0559 5 Jun 2007</date><xs>-2.0000002e-3</xs><bb>1.4500000</bb><co2><a1>1.0916099e1</a1><a2>5.7542901e1</a2><a3>9.1805896e1</a3><a4>3.2835602e2</a4><a5>-2.5574002e2</a5></co2><h2o><a1>3.6979999e-1</a1><a2>2.5436999</a2><a3>4.4586000</a3></h2o><press><a0>1.0499000e1</a0><a1>1.1110999e2</a1></press></poly><rs232><co2>true</co2><h2o>true</h2o><celltemp>true</celltemp><cellpres>true</cellpres><ivolt>true</ivolt><co2abs>true</co2abs><h2oabs>true</h2oabs><h2odewpoint>true</h2odewpoint><raw>true</raw><echo>false</echo><strip>false</strip></rs232><ver>1.0.15</ver><ack>true</ack></li840>

Is This A Good Question/Topic? 0
  • +

Replies To: Serial Port XML Data parsing errors

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Serial Port XML Data parsing errors

Posted 09 June 2011 - 05:26 PM

I think Regex is an option if the starting elements are always the same.
Was This Post Helpful? 1
  • +
  • -

#3 m_alien  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 13-May 11

Re: Serial Port XML Data parsing errors

Posted 09 June 2011 - 05:55 PM

Hey Hawk

You really think Regex is the way to go? Gezzz that kills man, it had taken me weeks just to get it to work properly using the XML Literals syntax which was great and all because it looks so clean and neat. When I look at Regex syntax i just cringe lol it's just ugly looking to me lolol. Anyway enough of me wining, could you give me a small example of how to start or some links to sites that could point point me in a beginners direction. lol

thanks
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Serial Port XML Data parsing errors

Posted 09 June 2011 - 06:07 PM

The pattern is simple: "<li840>.+"

That tells Regex to find this element and everything after.
Was This Post Helpful? 1
  • +
  • -

#5 m_alien  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 13-May 11

Re: Serial Port XML Data parsing errors

Posted 09 June 2011 - 06:15 PM

Ahhhhhhh okay so how and where do I put that in my code if my code looks something like this.


Public Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

LicorRecData(SerialPort1.ReadExisting)

End Sub
Public Sub LicorRecData(ByVal LicorData As String)


Dim li840 As XElement = XElement.Parse(LicorData)


Thanks
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Serial Port XML Data parsing errors

Posted 09 June 2011 - 07:37 PM

Public Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
  LicorRecData(SerialPort1.ReadExisting)
End Sub

Public Sub LicorRecData(ByVal LicorData As String)
  Dim m As Match = Nothing
  Dim r As New Regex("<li840>.+", RegexOptions.IgnoreCase)
  m = r.Match(LicorData)
  If m.Success Then
   Dim li840 As XElement = XElement.Parse(m.Value)
  End If
End Sub

Was This Post Helpful? 2
  • +
  • -

#7 m_alien  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 13-May 11

Re: Serial Port XML Data parsing errors

Posted 09 June 2011 - 07:48 PM

O my god that worked like a dream, your the man hawk.

If i could push that helpfully button more times I would

I need to read up and learn more about Regex damn.
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Serial Port XML Data parsing errors

Posted 09 June 2011 - 07:56 PM

Your welcome!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1