VB6.0+Winsock接收电文解密过程
首先在Client中定义结构体:
Public Type GaugeStatus
stx As Byte
MessageCode As Integer
DataType As Integer
StatusData(0 To 15) As Byte
etx As Byte
End Type
Public GaugeHeart As GaugeStatus
其中StatusData长度为16,它的取值规则如下:
First 16 bits are a copy of the gauge inputs
Bit 0 On pushbuttonBit 8 Spare
Bit 1 Off push buttonBit 9 Spare
Bit 2 Open Shutter push button Bit 10 S1 Gulmay error control
Bit 3 Close Shutter push button Bit 11 S2 Gulmay error control
Bit 4Operator E-stopBit 12 Junction box E-stop
Bit 5Go Forward push buttonBit 13 Forward limit
Bit 6 Go Reverse push buttonBit 14 spare
Bit 7 Console E-stopBit 15 Reverse limit
Second 16 bits are a copy of gauge inputs
Bit 0Shutter 1 openBit 8 Source 2 power OK
Bit 1Shutter 1 closedBit 9 Shutter 2 open
Bit 2spareBit 10 Shutter 2 closed
Bit 3Tube coolant flow 1Bit 11 Tube coolant flow 2
Bit 4spareBit 12 Source 1 coolant temperature
Bit 5Gate interlockBit 13 Source 2 coolant temperature
Bit 6Status Lamps OKBit 14 spare
Bit 7Source 1 power OKBit 15 spare
Third 16 bits are a copy of the gauge inputs
………………0-`5bit
Fourth 16 bits are miscellaneous gauge status
………………0-15bit
Bit encoding
Each 16 bits is stored in 4 (8 bit) bytes with one nibble of data ascii encoded per byte. Ascii encoding is done by OR’ing the value with 16#30. For example, to encode the 16 bits below
Bit 0Shutter 1 open1Bit 8Source 2 power OK1
Bit 1Shutter 1 closed0Bit 9Shutter 2 open1
Bit 2spare0 Bit 10 Shutter 2 closed0
Bit 3Tube coolant flow 11Bit 11 Tube coolant flow 21
Bit 4spare0 Bit 12 Source 1 coolant temperature 1
Bit 5Gate interlock0 Bit 13 Source 2 coolant temperature 1
Bit 6Status Lamps OK1Bit 14 spare0
Bit 7Source 1 power OK1Bit 15 spare0
The first nibble consists of bits 1001 and so will be encoded as 16#39
The second nibble consists of bits 1100 and so will be encoded as 16#3C
The third nibble consists of bits 1011 and so will be encoded as 16#3B
The fourth nibble consists of bits 0011 and so will be encoded as 16#33
那么根据编码规则,我应该怎么取值呢?
Private Sub WinsockSrv_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim GetDataBuf() As Byte
WinsockCli(Index).GetData GetDataBuf, vbByte
Select Case bytesTotal
Case 22
‘这里我应该怎么取值呢?具体怎么编码呢?
End Sub
[解决办法]
设计进制转换和位运算.
[解决办法]
见:八进制与十六进制转二进制的方法