我正在尝试使用VB.NET中的电机控制命令创建一个枚举。我想在一台计算机上设置命令,序列化它,通过TCP连接将其发送到另一台计算机,反序列化并解释命令。我知道如何使用TCP连接,但我缺少关于枚举的概念知识。我正在使用Protobuf-net来序列化并具有以下命令描述。
Public Class RemoteControl
<ProtoContract>
Public Class Command
<ProtoContract>
Enum CommandAction
<ProtoMember(2)>
HOME_MOTOR
<ProtoMember(1)>
MOVE_ABS
End Enum
End Class
End Class
我的问题是,如何将RemoteControl
对象的实例设置为我想要的操作?我知道枚举使用整数,所以要发送MOVE_ABS
(标签为1),我试过
Dim myAction As New RemoteControl
myAction.Command.CommandAction = 1
这返回了一个错误,说“CommandAction是一种类型,不能用作表达式”。
此外,一旦我设法弄清楚如何发送此命令,我将如何在另一台计算机上解释它?如果发送的命令是RemoteControl.Command.CommandAction
,那么MOVE_ABS
之类的反序列化值是否等于1?
答案 0 :(得分:0)
像往常一样,我花了好几天的时间,然后在发布这个帖子之后立即弄明白了。
我最终做的是:
SERVER SIDE
Dim realProto As New RemoteControl.Command.CommandAction
realProto = RemoteControl.Command.CommandAction.MOVE_ABS
ProtoBuf.Serializer.SerializeWithLengthPrefix(myStream, realProto, ProtoBuf.PrefixStyle.Fixed32)
CLIENT SIDE
Dim readProto As New RemoteControl.Command.CommandAction
readProto = Protobuf.Serializer.DeserializeWithLengthPrefix(Of RemoteControl.Command.CommandAction)(myStream, Protobuf.PrefixStyle.Fixed32)
然后将readProto
设置为RemoteControl.Command.CommandAction
,其值是与操作标记对应的整数,或者我可以readProto.ToString
查看操作的名称。