Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagecpp
titleQuellcode
linenumberstrue
collapsetrue
/*****************************************************************************/
/*! Function to handle I/O-data. Using to handle the I/O data from different
*   devices in own threads.
*   Packet Transfer and I/O Data exchange
*   \return CIFX_NO_ERROR on success                                         */
/*****************************************************************************/
DWORD WINAPI IODataHandler(LPVOID hChannel)
{
    long                   lRet = CIFX_NO_ERROR;
    /* Read and write I/O data (32Bytes). Output data will be incremented each cyle */
    unsigned char       abRecvData[64] = { 0 };
    unsigned char       abSendData[64] = { 0 };

   unsigned long       ulCycle = 0;
    uint32_t            ulState = 0;

    /* Do I/O Data exchange until a key is hit */
    while (/* loop condition */)
    {
        if (CIFX_NO_ERROR != (lRet = xChannelIORead(hChannel, 0, 0, sizeof(abRecvData), abRecvData, IO_WAIT_TIMEOUT)))
        {
			...
        }
        if (CIFX_NO_ERROR != (lRet = xChannelIOWrite(hChannel, 0, 0, sizeof(abRecvData), abRecvData, IO_WAIT_TIMEOUT)))
        {
            ...
        }
		...    
    }
}


/*****************************************************************************/
/*! Function to demonstrate communication channel functionality
*   Packet Transfer and I/O Data exchange
*   \return CIFX_NO_ERROR on success                                         */
/*****************************************************************************/
long ChannelDemo(HANDLE hDriver, char* szBoard[], uint32_t ulChannel[], uint32_t channelCount)
{
  HANDLE* hChannels;
  hChannels = malloc(channelCount * 4);

  HANDLE hChannel       = NULL;
  long   lRet           = CIFX_NO_ERROR;

  for (uint32_t i = 0; i < channelCount; i++) {

      printf("---------- Communication Channel demo ----------\r\n");
      lRet = xChannelOpen(hDriver, szBoard, ulChannel, &hChannel);
      if (CIFX_NO_ERROR != lRet)
      {
          printf("Error opening Channel!");
      }
      else
      {
          ...
		  ...
		  ...
		  
		  /* Set Host Ready to signal the filed bus an application is ready */
          lRet = xChannelHostState(hChannel, CIFX_HOST_STATE_READY, &ulState, HOSTSTATE_TIMEOUT);

          if (CIFX_NO_ERROR != lRet)
          {
              printf("Error setting host ready!\r\n");
          }
          else
          {
              /* Switch on the bus if it is not automatically running (see configuration options) */
              lRet = xChannelBusState(hChannel, CIFX_BUS_STATE_ON, &ulState, 0L);

              if (CIFX_NO_ERROR != lRet)
              {
                  printf("Unable to start the filed bus!\r\n");
              }
              else
              {
                  DWORD dwThreadID = 0;
                  hAppThreads[i] = CreateThread( NULL, 0, IODataHandler, hChannels[i], 0, &dwThreadID);
              }
          }
      }

      /* Wait for all threads terminated */
      lRet = WaitForMultipleObjects(channelCount, hAppThreads, TRUE, INFINITE);

      /* Close all Channel-Handler */
      for (uint32_t i = 0; i < channelCount; i++) {
          xChannelClose(ulChannel[i]);
      }
      free(hChannels);

  }
}

...