@@ -7,7 +7,7 @@ class RequestPacket
77 private byte _functionCode ;
88 private byte [ ] _data ;
99 private byte _byteCount ;
10- private byte [ ] _writeData ;
10+ private byte [ ] _multiWirteData ;
1111 private byte [ ] _crc ;
1212
1313 private RequestPacket ( byte slaveAddr , byte functionCode , byte [ ] data )
@@ -22,13 +22,21 @@ private RequestPacket(byte slaveAddr, byte functionCode, byte[] data)
2222 }
2323
2424 // 멀티 코일 & 레지스터 쓰기를 위한 생성자
25- private RequestPacket ( byte slaveAddr , byte functionCode , byte [ ] data , byte byteCnt , byte [ ] writeData )
25+ private RequestPacket ( byte slaveAddr , byte functionCode , byte [ ] data , byte [ ] writeData )
2626 {
2727 _slaveAddr = slaveAddr ;
2828 _functionCode = functionCode ;
2929 _data = data ;
30- _byteCount = byteCnt ;
31- _writeData = writeData ;
30+
31+ ushort quantity = ( ushort ) ( data [ 2 ] << 8 | data [ 3 ] & 0xFF ) ;
32+
33+ _byteCount = _functionCode switch
34+ {
35+ 0x0F => ( byte ) ( quantity / 8 + ( quantity % 8 == 0 ? 0 : 1 ) ) ,
36+ 0x10 => ( byte ) ( quantity * 2 ) ,
37+ _ => 0
38+ } ;
39+ _multiWirteData = writeData ;
3240
3341 _frame = GetMultiWriteFrame ( ) ;
3442 _crc = new byte [ 2 ] ;
@@ -39,15 +47,14 @@ private RequestPacket(byte slaveAddr, byte functionCode, byte[] data, byte byteC
3947 public RequestPacket ( byte [ ] frame )
4048 {
4149 _frame = frame ;
50+ // 멀티 코일 & 레지스터 쓰기
4251 if ( frame [ 1 ] == 0x0F || frame [ 1 ] == 0x10 )
4352 {
4453 _slaveAddr = frame [ 0 ] ;
4554 _functionCode = frame [ 1 ] ;
46- _data = new byte [ frame . Length - 7 ] ;
47- Array . Copy ( frame , 2 , _data , 0 , _data . Length ) ;
48- _byteCount = frame [ frame . Length - 4 ] ;
49- _writeData = new byte [ frame . Length - 7 - 1 ] ;
50- Array . Copy ( frame , 3 + _data . Length , _writeData , 0 , _writeData . Length ) ;
55+ _data = frame . Skip ( 2 ) . Take ( 4 ) . ToArray ( ) ;
56+ _byteCount = frame [ 6 ] ;
57+ _multiWirteData = frame . Skip ( 6 ) . Take ( _byteCount ) . ToArray ( ) ;
5158 _crc = new byte [ 2 ] ;
5259 Array . Copy ( frame , frame . Length - 2 , _crc , 0 , 2 ) ;
5360 }
@@ -79,14 +86,14 @@ private byte[] GetFrame()
7986
8087 private byte [ ] GetMultiWriteFrame ( )
8188 {
82- byte [ ] frame = new byte [ 1 + 1 + _data . Length + 1 + _writeData . Length + 2 ] ; // SlaveAddr + FunctionCode + Data + CRC
89+ byte [ ] frame = new byte [ 1 + 1 + _data . Length + 1 + _multiWirteData . Length + 2 ] ; // SlaveAddr + FunctionCode + Data + CRC
8390
8491 frame [ 0 ] = _slaveAddr ;
8592 frame [ 1 ] = _functionCode ;
8693 Array . Copy ( _data , 0 , frame , 2 , _data . Length ) ;
8794
8895 frame [ 2 + _data . Length ] = _byteCount ;
89- Array . Copy ( _writeData , 0 , frame , 3 + _data . Length , _writeData . Length ) ;
96+ Array . Copy ( _multiWirteData , 0 , frame , 3 + _data . Length , _multiWirteData . Length ) ;
9097
9198 ushort crc = PacketHelpers . CalcCRC ( frame , 0 , frame . Length - 2 ) ;
9299 frame [ frame . Length - 2 ] = ( byte ) ( crc & 0xFF ) ;
@@ -120,6 +127,18 @@ public byte[] Data
120127 set { _data = value ; }
121128 }
122129
130+ public byte ByteCount
131+ {
132+ get { return _byteCount ; }
133+ set { _byteCount = value ; }
134+ }
135+
136+ public byte [ ] MultiWriteData
137+ {
138+ get { return _multiWirteData ; }
139+ set { _multiWirteData = value ; }
140+ }
141+
123142 public byte [ ] Crc
124143 {
125144 get { return _crc ; }
@@ -133,7 +152,7 @@ public class RequestPacketBuilder
133152 private byte _functionCode ;
134153 private byte [ ] _data ;
135154 private byte _byteCount ;
136- private byte [ ] _writeData ;
155+ private byte [ ] _multiWriteData ;
137156
138157 public RequestPacketBuilder SetSlaveAddr ( byte slaveAddr )
139158 {
@@ -153,22 +172,16 @@ public RequestPacketBuilder SetData(byte[] data)
153172 return this ;
154173 }
155174
156- public RequestPacketBuilder SetByteCount ( byte byteCount )
157- {
158- _byteCount = byteCount ;
159- return this ;
160- }
161-
162- public RequestPacketBuilder SetWriteData ( byte [ ] writeData )
175+ public RequestPacketBuilder SetWriteData ( byte [ ] multiWriteData )
163176 {
164- _writeData = writeData ;
177+ _multiWriteData = multiWriteData ;
165178 return this ;
166179 }
167180
168181 public RequestPacket Build ( )
169182 {
170183 return _functionCode == 0x0F || _functionCode == 0x10 ?
171- new RequestPacket ( _slaveAddr , _functionCode , _data , _byteCount , _writeData ) :
184+ new RequestPacket ( _slaveAddr , _functionCode , _data , _multiWriteData ) :
172185 new RequestPacket ( _slaveAddr , _functionCode , _data ) ;
173186 }
174187 }
0 commit comments