[{"data":1,"prerenderedAt":274},["ShallowReactive",2],{"post-\u002Fblog\u002F2023\u002Fmbus-application-layer":3},{"id":4,"title":5,"body":6,"categories":262,"date":264,"description":258,"extension":265,"image":266,"meta":267,"navigation":269,"path":270,"seo":271,"stem":272,"__hash__":273},"blog\u002Fblog\u002F2023\u002Fmbus-application-layer.md","MBus application layer",{"type":7,"value":8,"toc":257},"minimark",[9,14,45,49,85,89,92,133,145,149,152,183,202,206,220,224,227,231,234,237,242],[10,11,13],"h1",{"id":12},"introduction","Introduction",[15,16,17,22,23,29,30,35,36,44],"p",{},[18,19,21],"a",{"href":20},"\u002Fblog\u002F2023\u002Freading-a-meter-speaking-mbus","Previously"," on this series, a payload was obtained from a meter over the EN13757 protocol.\n",[18,24,28],{"href":25,"rel":26},"https:\u002F\u002Fm-bus.com\u002Fdocumentation-wired\u002F06-application-layer",[27],"nofollow","Chapter 6.3",", in a nutshell, means a variable-sized array of data blocks. Each block is a number with flags attached and you evaluate the flags when mapping to 'sensors'. If your data sink desires a power reading, you look for function field set to 'Instant' and quantity 'Power'. If you, on the other hand, desire to know when you had it flowin', you look for function 'Maximum' and quantities 'Power' & 'DateTime'.\nThis implies a variable type for the payload field and this is what it boils down to. Do yourself a favor and don't ",[18,31,34],{"href":32,"rel":33},"https:\u002F\u002Fwiki.c2.com\u002F?StringlyTyped",[27],"stringly-type"," it. A moral equivalent to ",[37,38,39,40,43],"em",{},"Variant\u003CNull, DateTime, Real, Integer, Byte",[41,42],"span",{},">"," works well in practice.",[10,46,48],{"id":47},"parser-overview","Parser overview",[15,50,51,52,56,57,60,61,64,65,68,69,60,71,73,74,76,77,68,79,60,81,84],{},"Inband communication, that is no dedicated control characters. Unsurprising for a binary protocol. On the flipside, every data block tells you how long it is so you always know when the next starts.\nEvery data block consists of a ",[53,54,55],"strong",{},"d","ata ",[53,58,59],{},"i","nformation ",[53,62,63],{},"b","lock, a ",[53,66,67],{},"v","alue ",[53,70,59],{},[53,72,63],{},"lock and a payload. Both the DIB and VIB consist of a ",[53,75,55],{},"ata\u002F",[53,78,67],{},[53,80,59],{},[53,82,83],{},"f","ield 'starter' byte, as well as up to 10 extension bytes. Each highest bit indicates an extension byte following so you can easily pick the DIB and VIB from the front.",[10,86,88],{"id":87},"dib","DIB",[15,90,91],{},"Start chopping bytes off the front of the buffer and putting them onto the dissection table. Is the highest bit set? Continue chopping & putting. If not, the DIB's on your table and time's to dissect.\nThe DIB tells you",[93,94,95,103,109,115,121,127],"ul",{},[96,97,98,99,102],"li",{},"The ",[53,100,101],{},"payload length",", which is invaluable for decoding this whole thing in the first place.",[96,104,98,105,108],{},[53,106,107],{},"payload type",", which also helps in encoding.",[96,110,98,111,114],{},[53,112,113],{},"function",", which can be Instant, Maximum, Minimum, or Error (along the line of 'Cumulative energy during some error conditions').",[96,116,98,117,120],{},[53,118,119],{},"unit"," is a number. Imagine having a power meter with 1 main and 2 auxiliary instruments. Those would get mapped to units 0 (main device) to 2.",[96,122,98,123,126],{},[53,124,125],{},"tariff"," is supposed to be what it says on the tin, if there's different registers to fill when usage is high than when it's low (or, y'know, get creative), that's the field to look for.",[96,128,98,129,132],{},[53,130,131],{},"storage number"," tends to get used for historical purposes. If you, let's say, got an energy reading from each of the last 24 months, they have to differ somehow. So the current reading gets a value of 0, the last month 1 and so on.",[15,134,135,136,140,141,144],{},"Extract all the information there is from the DIF (first byte of the DIB) and pile all the other ones on top as applicable. I won't go into details as ",[18,137,139],{"href":25,"rel":138},[27],"chapter 6.3.2"," does so already. Pay the following chapter a visit as well, that's the last-frame marker I've talked about ",[18,142,143],{"href":20},"previously",".\nAll that unless it's one of those special functions 0x1F and 0x0F. Those denote a manufacturer-specific area following. Sans a length, this ranges from the next byte til the very end of the buffer. Skip the next 2 sections.",[10,146,148],{"id":147},"vib","VIB",[15,150,151],{},"The basic flow is the same, you chop off bytes until you got the whole thing and then you dissect it. You can of course dissect while chopping, but that aproach yields a terrible debugging experience. The VIB denotes",[93,153,154,160,166,171],{},[96,155,98,156,159],{},[53,157,158],{},"quantity"," is an actual physical measurable quantity, such as your total energy spent in this month. Could also be an abstract number such as the meter's serial number and a couple other things.",[96,161,98,162,165],{},[53,163,164],{},"modifier"," (or orthogonal extension if you like it fancy) is another quanity which extends the previous one.",[96,167,98,168,170],{},[53,169,119],{},", as in 'actual physical unit'. I personally prefer a null-like value for this field for timestamps (software correctness & stuff), otherwise it could be something like J, m³ or h. Or gallons or MBTUs which are aktually kilo-BTUs (1MBTU = 1000BTU, Brits are weird) or whatnot. Were it for me, there would be plain & simple SI units there and billing systems would convert this stuff but hey, it's not like we sent man to moon with the SI or something...",[96,172,98,173,176,177,182],{},[53,174,175],{},"exponent"," is what it says on the tin, that x you put into 10xto multiply the payload to the apropriate scale. Somewhat similar to ",[18,178,181],{"href":179,"rel":180},"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FIEEE_754-1985",[27],"IEEE 754"," in spirit and easy to implement.",[15,184,185,186,191,192,197,198,201],{},"Decoding the VIB is a mess of tables described in ",[18,187,190],{"href":188,"rel":189},"https:\u002F\u002Fm-bus.com\u002Fdocumentation-wired\u002F08-appendix",[27],"chapter 8.4.3ff",". Start with the primary table and put the first byte in there. If you get a final answer, grab yourself a tee, a beer or whatever it is you grab to celebrate something nice happening. What about a cat?\nYou may also get a referral to another table. There's a couple of them, I can't say it's structured in any sensible manner, using tables (switch expressions, if you can afford them, are neat) for control flow is the way to go here. Someday, somehow, you'll reach a final answer, and then you can ",[18,193,196],{"href":194,"rel":195},"https:\u002F\u002Fstray.game\u002F",[27],"grab a cat",".\nOr maybe not because you may still have VIFEs (",[53,199,200],{},"e","xtensions) left after the last table ended. That's because there are orthogonal extensions. Those may change quantities & units (energy -> power) or introduce modifiers, such as the previous timestamp example. Another one would be a negative modifier to idincate, let's say, volume from times with negative flow.\nThen there's weird stuff such as the actual VIB being encoded in an ASCII string following the binary VIB. And don't get me started on the compact profile...",[10,203,205],{"id":204},"payload","Payload",[15,207,208,209,214,215,219],{},"After swaths of bit manipulations, the rest is straightforward. You take however many bytes indicated by the DIB and parse it accordingly. I personally don't see the point of BCD values when binary integers do the job just fine but ",[18,210,213],{"href":211,"rel":212},"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FMoore%27s_law",[27],"Moore's law"," & stuff.\nDepending on the quantity, you may have to massage the preliminary result some more. Classically, physical readings will get delivered with an exponent so you have to scale accordingly, likely requiring a real number in the process. Things like serial numbers, on the other hand, get used in integer form obtained in the first step. And if it's something fancy, such as manufacturer-specific error flags, you may very well keep the buffer as-is and delegate post-processing this mess to a higher layer.\nWell, I lied about bit stuff being over. You may get a timestamp in a type F, G or I. ",[18,216,218],{"href":188,"rel":217},[27],"Chapter 8.2"," isn't exactly helpful, a code example is about to follow.",[10,221,223],{"id":222},"manufacturer-specific-data","Manufacturer-specific data",[15,225,226],{},"An important function this area serves is the last-frame marker. A DIB of 0x1F means there's more frames following, an 0x0F means you can stop.\nOtherwise, not much to say here, really. Here be dragons. If you are a better shot at reverse-engineering than I am (which isn't a high bar to clear), you may have some fun with this but I'd go to the manufacturer for documentation.",[10,228,230],{"id":229},"wrap-up","Wrap-up",[15,232,233],{},"There's a couple ways to proceed from here. One is to iterate through all the data blocks, inspecting flags as you go. My preferred one is outlined in the example above, that is starting with the gauges I want to feed and picking the right flags from the dataset.\nIn my experience, decoding those data blocks and visualizing them in the debugger with relevant properties pinned is an effective way to aproach data mapping.",[235,236],"hr",{},[238,239,241],"h2",{"id":240},"kommentare-comments","Kommentare \u002F Comments",[15,243,244,245,250,251,256],{},"Hast du Fragen oder Anmerkungen zu diesem Artikel? ",[18,246,249],{"href":247,"rel":248},"https:\u002F\u002Fgithub.com\u002Fthe78mole-blog\u002Fthe78mole-blog.github.io\u002Fissues\u002Fnew?title=Kommentar+zu%3A+mbus-application-layer&labels=comment",[27],"Erstelle ein GitHub Issue"," oder starte eine ",[18,252,255],{"href":253,"rel":254},"https:\u002F\u002Fgithub.com\u002Fthe78mole-blog\u002Fthe78mole-blog.github.io\u002Fdiscussions",[27],"Diskussion",".",{"title":258,"searchDepth":259,"depth":259,"links":260},"",2,[261],{"id":240,"depth":259,"text":241},[263],"M-Bus","2023-01-16","md",null,{"tags":268},[],true,"\u002Fblog\u002F2023\u002Fmbus-application-layer",{"title":5,"description":258},"blog\u002F2023\u002Fmbus-application-layer","VQnwhBEJBoui9XdnwCf2Q3RrRk5hovHHTHIuyKeeux0",1777286693817]