Parsing ASCII communication with Hardware using Java/Scala? -
i writing software interface external device via exchange of ascii commands. example:
pos? 1 2 =>1=-1158.4405 =>2=+0000.0000
where above ask position of motorised microscope stage 1-st , 2-nd axes. responds positions in um. more details , examples.
my question: there library ease parsing such string outputs, and/or generate queries? otherwise, best practises parsing , communicating hardware using java/scala?
trying cope following syntax (see 12.1 format
):
reply syntax: [<argument>[{sp<argument>}]"="]<value>lf multi-line reply syntax: {[<argument>[{sp<argument>}]"="]<value>sp lf} [<argument>[{sp<argument>}]"="]<value>lf last line!
this code:
import scala.util.parsing.combinator._ case class result(argument: string, value: float) class replyparser extends regexparsers{ override def skipwhitespace = false private def floatingpointnumber: parser[string] = """(-|\+)?(\d+(\.\d*)?|\d*\.\d+)""".r private def value: parser[float] = floatingpointnumber ^^ (_.tofloat) private def argument: parser[string] = "[^= \n]+".r private def arguments: parser[list[string]] = rep1sep(argument," ") <~ "=" private def result: parser[list[result]] = arguments.? ~ value ^^ { case arguments ~ value => arguments.getorelse(list("")).map { result(_,value) } } def reply: parser[list[result]] = rep1sep(result, " \n".r) <~ " " ^^ { case result => result.flatten } } object parsing extends replyparser { def main(args: array[string]) { val result = parseall(reply,"a=+000.123 \nc d=-999.567 \n789 ") println(s"$result") } }
Comments
Post a Comment