//-----------------------------------------------------------------------------
//  swap_data
//
//
//      INPUTS:
//
//     OUTPUTS:
//
//
//
//
//-----------------------------------------------------------------------------
static int swap_data(int myindex)
{
  int j, n, ret = 1;
  struct mb_ana_group *aMst;
  struct mb_dig_group *dMst;
  int retval = 1;



modbus_Level(myindex, LEVEL_2, FUNCTION_37);

  if( !mbDI[myindex].new_outputs )
    return retval;

  //printf("   <<<%dDO %d, T:%4d:::T:%4d, GT:%4d\n", myindex, 3, mbDB[myindex].digOutT[3], mbDI[myindex].digOutT[3], mbDG[myindex].digOutT[3]);

  //printf ( "swap_data: %d\n" , myindex ); fflush(stdout);
  /*...
    retval = q_receive ( ModBus[myindex].InQ, (ULONG)Q_NOWAIT, TIMEOUT, mb_bufr ) ;
    retval = 1 ;
    if ( ( retval != 0 ) && ( retval != ERR_NOMSG ) )
    {
      printf ( "swap_data: Nothing %d\n" , retval ); fflush(stdout);
      // this is an error. keep track
      //qRcvErr++ ;
      return 0;
    }
    mbPtr =  (struct mb_Q_data *)mb_bufr [ 0 ];
    printf ( "swap_data:%d >>>\n" , myindex ); fflush(stdout);
  ...*/
  // Copy DOs from Q to thread
  memcpy( &mbDB[myindex].digOuts[0] , &mbDI[myindex].digOuts[0] , TOTAL_DO_POINTS*sizeof(int)   );
  // Copy DOs from Q to thread
  memcpy( &mbDB[myindex].digOutT[0] , &mbDI[myindex].digOutT[0] , TOTAL_DO_POINTS*sizeof(int)   );
  // Copy AOs Q to thread
  memcpy( &mbDB[myindex].anaOuts[0] , &mbDI[myindex].anaOuts[0] , TOTAL_AO_POINTS*sizeof(float) );
  // Copy AOs Q to thread
  memcpy( &mbDB[myindex].digInLR[0] , &mbDI[myindex].digInLR[0] , TOTAL_DI_POINTS*sizeof(int)   );
  mbDI[myindex].new_outputs = 0;

  for(n=0; n<TOTAL_DI_POINTS; n++)
  {
    mbDB[myindex].digInLC[n] = mbDB[myindex].digInLC[n] - mbDB[myindex].digInLR[n];
    if( mbDB[myindex].digInLC[n] < 0 )
       mbDB[myindex].digInLC[n] = 0;
  }

  //mbPtr[myindex].init = MB_OWNER_FREE;
  /*...
    mb_bufr [ 0 ] = (unsigned long)(struct mb_Q_data*)&mbDO[0] ;
    mbPtr =  (struct mb_Q_data *)mb_bufr [ 0 ];
  ...*/
  // Copy DIs read in to Q
  memcpy( &mbDO[myindex].digIns[0]  , &mbDB[myindex].digIns[0]  , TOTAL_DI_POINTS*sizeof(int)   );
  // Copy DIs read in to Q
  memcpy( &mbDO[myindex].digInLC[0] , &mbDB[myindex].digInLC[0] , TOTAL_DI_POINTS*sizeof(int)   );
  // Copy DIs read in to Q
  memcpy( &mbDO[myindex].digInLT[0] , &mbDB[myindex].digInLT[0] , TOTAL_DI_POINTS*sizeof(int)   );
  /*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  for(n=0; n<TOTAL_DO_POINTS; n++)
    if(mbDI[myindex].digOutT[n])
      printf("+++DO %d, T:%4d\n", n, mbDI[myindex].digOutT[n]);
  */

  // Copy AIs read in to Q
  memcpy( &mbDO[myindex].anaIns[0] , &mbDB[myindex].anaIns[0] , TOTAL_AI_POINTS*sizeof(float) );

  mbDO[myindex].new_inputs = 1;
  /*...
    retval = q_send ( ModBus[myindex].OutQ , mb_bufr ) ;
    if ( retval != 0 )
    {
      //qSndErr++ ; // increment error counter
      printf ( "Modbus Queue: error sending to Snap Out: %X\n" , retval ) ;
    }
  ...*/
  // AO....................................................................
  n=0;
  if(ModBus[myindex].AO.address != MB_NOT_INIT)
  {
    aMst = &ModBus[myindex].AO;
    while(aMst->address)                            //
    {
      for(j=0; j<aMst->count; j++)                //
      {
        if( aMst->apt[j].fltValue != mbDB[myindex].anaOuts[n] )
        {
          aMst->apt[j].fltValue = mbDB[myindex].anaOuts[n];
          aMst->apt[j].changed++;
          aMst->changed++;
        }
        n++;
      }
      if( aMst->next != NULL )                      //
        aMst = aMst->next;                          //
      else
        break;
    }
  }


  // DO....................................................................
  n=0;
  if(ModBus[myindex].DO.address != MB_NOT_INIT)
  {
    dMst = &ModBus[myindex].DO;
    while(dMst->address)             //
    {
      for(j=0; j<dMst->count; j++)                //
      {
        /*        if(3==j)printf("%dDO %2d:%2d, Val %d:%d, Timed %4d:%4d \n", myindex, j, n,
                        mbDB[myindex].digOuts[n], dMst->dpt[j].iValue,
                        dMst->dpt[j].timed, mbDB[myindex].digOutT[n]);   */
        if( dMst->dpt[j].iValue != mbDB[myindex].digOuts[n] )
        {
          if( dMst->dpt[j].timed == 0 )
          {
            dMst->dpt[j].iValue = mbDB[myindex].digOuts[n];
            dMst->dpt[j].timed  = mbDB[myindex].digOutT[n];
            dMst->dpt[j].changed++;
            dMst->changed++;
            //printf("NewDO# %d", j);
            //if( dMst->dpt[j].timed ) printf(" ,Timed: %d", dMst->dpt[j].timed);
            //printf("\n");
          }
        }
        n++;
      }
      if( dMst->next != NULL )                      //
        dMst = dMst->next;                          //
      else
        break;
    }
  }

  return ret;

}

//-----------------------------------------------------------------------------
//  GCT_swap_data
//
//
//      INPUTS:
//
//     OUTPUTS:
//
//
//
//
//-----------------------------------------------------------------------------
int GCT_swap_data(int myindex)
{
  int ret = 1;
  //int n;
  //int retval = 1;

modbus_Level(myindex, LEVEL_2, FUNCTION_38);

  //  printf ( "GCT_swap_data: %d\n" , myindex ); fflush(stdout);
  /*...
    retval = q_receive ( ModBus[myindex].OutQ, (ULONG)Q_NOWAIT, TIMEOUT, mb_bufr ) ;
    retval = 1 ;

    if ( ( retval != 0 ) && ( retval != ERR_NOMSG ) )
    {
      // this is an error. keep track
      //qRcvErr++ ;
      printf ( "GCT_swap_data: Nothing %d\n" , retval ); fflush(stdout);
      return 0;
    }

    mbPtr2 =  (struct mb_Q_data *)mb_bufr2 [ 0 ];
  ...*/
  // Copy DIs read in to Q
  memcpy( &mbDG[myindex].digIns[0]  , &mbDO[myindex].digIns[0]  , TOTAL_DI_POINTS*sizeof(int)   );
  // Copy DIs read in to Q
  memcpy( &mbDG[myindex].digInLC[0] , &mbDO[myindex].digInLC[0] , TOTAL_DI_POINTS*sizeof(int)   );
  // Copy DIs read in to Q
  memcpy( &mbDG[myindex].digInLT[0] , &mbDO[myindex].digInLT[0] , TOTAL_DI_POINTS*sizeof(int)   );
  // Copy AIs read in to Q
  memcpy( &mbDG[myindex].anaIns[0]  , &mbDO[myindex].anaIns[0]  , TOTAL_AI_POINTS*sizeof(float) );
  mbDO[myindex].new_inputs = 0;

  /*...
    mb_bufr2 [ 0 ] = (unsigned long)(struct mb_Q_data*)&mbDI[0] ;
    mbPtr2 =  (struct mb_Q_data *)mb_bufr [ 0 ];
  ...*/

  // Return acknowedged Latched DI counts
  memcpy( &mbDI[myindex].digInLR[0] , &mbDG[myindex].digInLC[0] , TOTAL_DI_POINTS*sizeof(int)   );

  // Copy DIs read in to Q
  // Copy DOs from Q to thread
  memcpy( &mbDI[myindex].digOuts[0] , &mbDG[myindex].digOuts[0] , TOTAL_DO_POINTS*sizeof(int)   );
  // Copy DOs from Q to thread
  memcpy( &mbDI[myindex].digOutT[0] , &mbDG[myindex].digOutT[0] , TOTAL_DO_POINTS*sizeof(int)   );
  // Copy AOs Q to thread
  memcpy( &mbDI[myindex].anaOuts[0] , &mbDG[myindex].anaOuts[0] , TOTAL_AO_POINTS*sizeof(float) );
  mbDI[myindex].new_outputs = 1;
  /*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  for(n=0; n<TOTAL_DO_POINTS; n++)
    if(mbDG[myindex].digOutT[n])
      printf(">>>%dDO %d, T:%4d:::T:%4d\n", myindex, n, mbDG[myindex].digOutT[n], mbDI[myindex].digOutT[n]);
  */
  /*...
    retval = q_send ( ModBus[myindex]. InQ , (ULONG*)mbPtr2 ) ;
    if ( retval != 0 )
    {
      //qSndErr++ ; // increment error counter
      printf ( "Modbus Queue: error sending to Snap Out: %X\n" , retval ) ;
    }
  ...*/
  return ret;

}

