@@ -453,5 +453,87 @@ public static PassThruStructs.PassThruMsg[] ConvertReadExpression(this PassThruR
453453 // Return the message
454454 return MessagesBuilt ;
455455 }
456+
457+ // ------------------------------------------------------------------------------------------------------------------------------------------
458+
459+ /// <summary>
460+ /// Converts a given flow control message into a segmented message object
461+ /// Use the example blow to understand how this conversion routine is performed
462+ ///
463+ /// Sent Input Message:
464+ /// 00 00 07 DF 02 09 02
465+ /// Response Message:
466+ /// 00 00 07 E8 49 02 01 34 54 31 4B 36 31 41 4B 30 50 55 37 38 30 32 32 33
467+ /// Response Segments:
468+ /// 00 00 07 E8 10 14 49 02 01 34 54 31 --> Start of Response
469+ /// 00 00 07 E0 30 00 00 00 00 00 00 00 --> Flow Control Identifier
470+ /// 00 00 07 E8 21 4B 36 31 41 4B 30 50 --> Message 1 of 2
471+ /// 00 00 07 E8 22 55 37 38 30 32 32 33 --> Message 2 of 2
472+ /// </summary>
473+ /// <param name="ResponseRead">The message we're looking to segment out</param>
474+ /// <returns>An array of messages holding the input message contents segmented out</returns>
475+ public static PassThruStructs . PassThruMsg [ ] SegmentMessage ( this PassThruStructs . PassThruMsg ResponseRead , PassThruStructs . PassThruMsg CommandSent )
476+ {
477+ // First check if we need to segment this message or not
478+ if ( ResponseRead . DataSize <= 12 ) return new [ ] { ResponseRead } ;
479+
480+ // Take our input content and slice it into a set of messages here
481+ int CommandSize = CommandSent . Data [ 5 ] ;
482+ List < PassThruStructs . PassThruMsg > MessageContentsSplit = new List < PassThruStructs . PassThruMsg > ( ) ;
483+
484+ // Build the first segment of the message content here
485+ // 1. 00 00 07 E8 --> Message Start/Address
486+ // 2. 00 00 07 E8 10 14 --> Indicate Flow Control and size (Size of input minus 10)
487+ // 3. 00 00 07 E8 10 14 49 02 --> Tack on command response values here (Skip 4 on input command to find bytes)
488+ // 4. 00 00 07 E8 10 14 49 02 01 34 54 31 --> Append the start of our message contents here. Should only be 4 bytes of data
489+ List < byte > FirstSegment = new List < byte > ( ) ;
490+ FirstSegment . AddRange ( ResponseRead . Data . Take ( 4 ) ) ; // 00 00 07 E8
491+ FirstSegment . AddRange ( new byte [ ] { 0x10 , ( byte ) ( ResponseRead . DataSize - 10 ) } ) ; // 00 00 07 E8 10 14
492+ FirstSegment . AddRange ( ResponseRead . Data . Skip ( 4 ) . Take ( CommandSize ) ) ; // 00 00 07 E8 10 14 49 02
493+ FirstSegment . AddRange ( ResponseRead . Data . Skip ( 4 + CommandSize ) . Take ( 4 ) ) ; // 00 00 07 E8 10 14 49 02 01 34 54 31
494+
495+ // Convert the input segment to a PassThruMsg and store it
496+ MessageContentsSplit . Add ( new PassThruStructs . PassThruMsg ( ( uint ) FirstSegment . Count )
497+ {
498+ ExtraDataIndex = 0 ,
499+ Data = FirstSegment . ToArray ( ) ,
500+ TxFlags = TxFlags . NO_TX_FLAGS ,
501+ RxStatus = RxStatus . NO_RX_STATUS ,
502+ DataSize = ( uint ) FirstSegment . Count ,
503+ ProtocolId = ResponseRead . ProtocolId ,
504+ } ) ;
505+
506+ // Now iterate through the rest of our message content and build segments for it
507+ // 1. 00 00 07 E8
508+ // 2. 00 00 07 E8 2X
509+ // 3. 00 00 07 E8 2X XX XX XX XX XX XX XX (or shorter based on content size)
510+ for ( int MsgDataIndex = 12 ; MsgDataIndex < ResponseRead . DataSize ; MsgDataIndex ++ )
511+ {
512+ // Start each message with 00 00 07 E8 2X
513+ List < byte > MessageSegment = new List < byte > ( ) ;
514+ MessageSegment . AddRange ( ResponseRead . Data . Take ( 4 ) ) ; // 00 00 07 E8
515+ MessageSegment . Add ( ( byte ) MessageContentsSplit . Count ) ; // 00 00 07 E8 21
516+
517+ // Check to see if we can take the max message size or not.
518+ int BytesToTake = MsgDataIndex + 7 < ResponseRead . DataSize
519+ ? 7
520+ : ( int ) ( ResponseRead . DataSize - MsgDataIndex ) ;
521+ MessageSegment . AddRange ( ResponseRead . Data . Skip ( MsgDataIndex ) . Take ( BytesToTake ) ) ; // 00 00 07 E8 21 4B 36 31 41 4B 30 50
522+
523+ // Add this segment to our collection of messages here and move onto the next loop
524+ MessageContentsSplit . Add ( new PassThruStructs . PassThruMsg ( ( uint ) MessageSegment . Count )
525+ {
526+ ExtraDataIndex = 0 ,
527+ Data = FirstSegment . ToArray ( ) ,
528+ TxFlags = TxFlags . NO_TX_FLAGS ,
529+ RxStatus = RxStatus . NO_RX_STATUS ,
530+ DataSize = ( uint ) FirstSegment . Count ,
531+ ProtocolId = ResponseRead . ProtocolId ,
532+ } ) ;
533+ }
534+
535+ // Return our output list of message objects built
536+ return MessageContentsSplit . ToArray ( ) ;
537+ }
456538 }
457539}
0 commit comments