将来自 Open Protocol 的丑陋的 ASCII 包转换为对象
OpenProtocol communication utility
Open Protocol, as the name says, it's a protocol to communicate with Atlas Copco Tightening Controllers or whatever that implement that protocol. Most common Tightening Controllers from Atlas Copco company are PowerFocus4000 and PowerMacs.
Although, some other companies adhered to use the same protocol.
OpenProtocolInterpreter is a library that converts the ugly string that came from Open Protocol packages, which is commonly called MID, to an object. "Substringing" packages is such a boring thing to do, so let OpenProtocolIntepreter do it for you!
If you're curious, just take a look at their documentation.
Install-Package OpenProtocolInterpreterIt's simple, you give us your byte[] or string package and we deliver you an object, simple as that!
For example, let's imagine you received the following string package:
string package = "00240005001 0018";It's MID 5, so OpenProtocolIntepreter will return a Mid0005 class for you with all his datafields and the package entire translated to an object.
MidInterpreter starts empty: you must tell it which MIDs it should know about before parsing anything.
UseAllMessages() is the "give me everything" shortcut — see MIDs identifying customization to narrow it down.
var interpreter = new MidInterpreter().UseAllMessages();
var midPackage = "00260004001 001802";
var myMid04 = interpreter.Parse(midPackage);
//MID 0004 is an error mid which contains which MID failed and its error code
int myFailedMid = myMid04.FailedMid; // 18
Error errorCode = myMid04.ErrorCode; // Error.ParameterSetIdNotPresentEverything a MID carries is a plain, settable property, so you can read it straight away.
byte[] packages are accepted by the very same overloads:
byte[] rawPackage = socket.Receive();
var mid = interpreter.Parse(rawPackage); // returns Mid, use it when you don't know what is coming
var mid04 = interpreter.Parse(rawPackage);Use
Parse(package)when you don't know which MID will arrive andParse(package)when you do. The generic overload throwsInvalidCastExceptionwhen the package turns out to be another MID.
It can generate an object from a string, but can it make it to the other way?? FOR SURE!
var jobUploadRequest = new Mid0032(revision: 2) { JobId = 1 };
var package = jobUploadRequest.Pack();
//Generated package => 00240032002 0001
var bytes = jobUploadRequest.PackBytes(); // same content, encoded with Mid.DefaultEncoding (ASCII)
var terminated = jobUploadRequest.PackWithNul(); // appends the NUL character some controllers expectThe header length is recalculated for you on every Pack(), based on the revision in Header.Revision.
Every MID exposes a parameterless constructor (revision 1) and a Mid(Header header) constructor; the ones that
have more than one documented revision also expose Mid(int revision).
MIDs advertise their relationships through marker interfaces, so it might help you to remember which MID answers which or even give a shortcut for you:
Mid0062 ack = new Mid0061(2).GetAcknowledge(); // IAcknowledgeable, keeps the source revision
Mid0033 reply = new Mid0032(2).GetReply(); // IAnswerableByA MID has each property is decorated with a data field attribute describing where that value lives inside the package. Which also represents the string index/position of the package accordingly with Open Protocol documentation. E.g.:
…Every attribute accepts:
| Member | Meaning |
|---|---|
revision |
Revision this definition belongs to. |
field |
Data field id/sequence/index inside the revision. |
Index |
Offset of the value inside the package, counted from the start of the package (the header takes the first 20 chars). |
Size |
Length of the value. Fixed automatically by some attributes that has fixed size (booleans, timestamps). |
HasPrefix |
true when the package carries the two digit field id right before the value. |
PaddingChar |
Char used to pad the value to its full size when packing. |
PaddingOrientation |
Defines if char filling RightPadded or LeftPadded |
Parsing and packing both flow through the same definitions: on Parse the slice is read, converted and written back into the property; on Pack the property value is read, converted and written into the package.
| Attribute | Property type | Notes |
|---|---|---|
BooleanDataFieldDefinition |
bool |
Size forced to 1 |
StringDataFieldDefinition |
string |
Defaultly right padded with ' ' |
Int32DataFieldDefinition |
int, int-backed enum |
left padded with '0' |
Int64DataFieldDefinition |
long, long-backed enum |
left padded with '0' |
DecimalDataFieldDefinition |
decimal |
left padded with '0' |
TruncatedDecimalDataFieldDefinition |
decimal |
DecimalPoints (default 2) implicit decimal places |
TimestampDataFieldDefinition |
DateTime |
Size forced to 19, YYYY-MM-DD:HH:MM:SS |
Int32CollectionDefinition |
List |
requires EachFieldSize |
EnumCollectionDefinition |
List where T : Enum |
one char per entry |
VariableDataFieldCollectionDefinition |
List |
the variable data fields block |
At parse/pack time only the definitions up to Header.StandardizedRevision are used so we can avoid confusion with revision 0 since when zero or blank it represents revision 1.
Now we will get real! Put one thing in mind, in real world we will always need to build something more complex than the dummies examples we give to you. With this in mind, this section is for you:
We have several MIDs inside Open Protocol documentation, but do you really need all of them? The answer is... NO!
You will probably need only to use a range of MIDs, with this in mind, we did something to make things faster. You can tell us which MIDs we should considerate!
Here is an example:
…You can also register a whole category at once, and filter it by your role in the communication:
var interpreter = new MidInterpreter()
.UseCommunicationMessages(InterpreterMode.Integrator)
.UseTighteningMessages(InterpreterMode.Integrator)
.UseAlarmMessages(InterpreterMode.Integrator);InterpreterMode.Integrator keeps only the MIDs a controller can send you, InterpreterMode.Controller keeps
only the ones an integrator can send, and InterpreterMode.Both (the default) keeps everything.
There is one Use...Messages extension per category, all of them accepting a mode, an IEnumerable or an
IDictionary (see MIDs overriding).
MIDs 0006 (request), 0008 (subscription) and 0009 (unsubscription) carry a free form payload whose layout
depends on the MID being requested/subscribed. Those payloads are modelled by ExtraData classes, which use the very same data field attributes as a MID:
var subscription = new Mid0008();
subscription.SetExtraData(new Mid1201ExtraDataSubscription
{
SendAlternatives = 1,
DataIdentifierTimestamp = new DateTime(2026, 8, 7, 13, 45, 0),
SendObjectData = true
});
var package = subscription.Pack();
//Generated package => 00590008001 12010013000000000012026-08-07:13:45:001SetExtraData fills SubscriptionMid, WantedRevision, ExtraDataLength and ExtraData for you.
On the receiving side, parse the container MID first and then hand its raw ExtraData to the matching class:
var mid08 = interpreter.Parse(package);
var extraData = (Mid1201ExtraDataSubscription)new Mid1201ExtraDataSubscription(mid08.WantedRevision)
.Parse(mid08.ExtraData);
int alternatives = extraData.SendAlternatives;
DateTime timestamp = extraData.DataIdentifierTimestamp;A MID may declare one ExtraData per kind, since request (IExtraDataRequest), subscription (IExtraDataSubscription) and unsubscription (IExtraDataUnsubscription) payloads don't necessarily share the same content.
If necessary you can add
ExtraDatamanually as plain string.
Maybe you have a totally crazy controller that does not implement the Mid as the documentation says or you might want to inject your own Mid inheriting another Mid, so you can customize it and add more properties to handle some conversions. Anyway, if you need that, it's possible to override!
Here is an example:
//This will override Mid 81 with my custom Mid
var interpreter = new MidInterpreter()
.UseAllMessages()
.UseTimeMessages(new Dictionary { { Mid0081.MID, typeof(OverridedMid0081) } });
var mid = interpreter.Parse("00390081001 2026-08-07:13:45:00");
//mid is an OverridedMid0081
public class OverridedMid0081 : Mid0081
{
public string FormattedDate
{
get => Time.ToString("dd/MM/yyyy HH:mm:ss");
set => Time = DateTime.Parse(value);
}
public OverridedMid0081()
{
}
}Your type must expose a parameterless constructor — that's the one the interpreter compiles and calls.
Maybe your controller is weird and have unknown MID numbers, MIDs that are not in the documentation and you want to inject into MidInterpreter, there is a way:
…NOTE: Custom messages might not perform as fast as other MIDs because they don't have optimizations for finding it
Declared a delegate:
protected delegate void ReceivedCommandActionDelegate(ReceivedMidEventArgs e);ReceivedMidEventArgs class:
public class ReceivedMidEventArgs : EventArgs
{
public Mid ReceivedMid { get; set; }
}Created a method to register all those MID types by delegates:
…What was done is registering in a dictionary the correspondent delegate for a determinated MID, once done that we just need to invoke the delegate everytime you face a desired MID.
When a package income:
pro暂无开放 Issues,或尚未同步最近议题。