fork download
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. struct SerialPacket
  6. {
  7. int payloadSize;
  8. char state;
  9. char configuation; // A -> Append, S -> SetRelays, C-> Clear all;
  10. int relayIDS[35];
  11.  
  12. }m_packet;
  13.  
  14. char bufferData[] = "5,h,l,1,2,3,4,5";
  15.  
  16.  
  17. char * p = bufferData;
  18.  
  19. m_packet.payloadSize = strtoul( p, &p, 10 );
  20. p++;
  21.  
  22. m_packet.state = *p;
  23. p += 2;
  24.  
  25. m_packet.configuation = *p;
  26. p += 2;
  27.  
  28. uint8_t id = 0;
  29. while ( *p && id < m_packet.payloadSize )
  30. {
  31. m_packet.relayIDS[ id++ ] = strtoul( p, &p, 10 );
  32.  
  33. if ( *p )
  34. {
  35. p++;
  36. }
  37. }
  38.  
  39.  
  40. printf( "m_packet.payloadSize = %d\n", m_packet.payloadSize );
  41. printf( "m_packet.state = %c\n", m_packet.state );
  42. printf( "m_packet.configuation = %c\n", m_packet.configuation );
  43.  
  44. for ( uint8_t i = 0; i < m_packet.payloadSize; i++ )
  45. {
  46. printf( "m_packet.relayIDS[%hhu] = %d\n", i, m_packet.relayIDS[i] );
  47. }
  48.  
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 5408KB
stdin
Standard input is empty
stdout
m_packet.payloadSize = 5
m_packet.state = h
m_packet.configuation = l
m_packet.relayIDS[0] = 1
m_packet.relayIDS[1] = 2
m_packet.relayIDS[2] = 3
m_packet.relayIDS[3] = 4
m_packet.relayIDS[4] = 5