0

Im fairly new to IOS programming and objective-c. I have an embedded system that runs a program written in C that is sending UDP packet to iPhone app I am working on.

I am able to read the packet data (NSData) if it only contains a string but, cannot if the data is structured with additional markup.

Here is the C code that sends the packet.

typedef struct s_msg_temp_report {
   uint8_t id0;
   uint8_t id1;
   uint8_t name[9];
   uint8_t led;
   uint32_t temp;
} t_msg_temp_report;

static t_msg_temp_report msg_temp_report =
{
   .id0 = 0,
   .id1 = 2,
   .name = DEMO_PRODUCT_NAME,
   .led = 0,
   .temp = 0,
};
            /* Send client report. */
        msg_temp_report.temp = (uint32_t)(at30tse_read_temperature() * 100);
        msg_temp_report.led = !port_pin_get_output_level(LED_0_PIN);
        ret = sendto(tx_socket, &msg_temp_report, sizeof(t_msg_temp_report),
        0,(struct sockaddr *)&addr, sizeof(addr));
        if (ret == M2M_SUCCESS) {
            puts("Assignment 3.3: sensor report sent");
        } else {
            puts("Assignment 3.3: failed to send status report !");
        }

What is the best way to to process (NSData) object data into a usable object for string conversion?

Ramkrishna Sharma
  • 6,961
  • 3
  • 42
  • 51
James
  • 70
  • 1
  • 9
  • Did you check those two posts ? [How to pack struct in NSData?](http://stackoverflow.com/questions/12328582/how-to-pack-struct-in-nsdata) and [Send and receive NSData via GameKit](http://stackoverflow.com/questions/4837102/send-and-receive-nsdata-via-gamekit) – J. Piquard Oct 19 '16 at 07:28
  • what are you asking? There is no string conversion in the posted code (and none is needed) the full struct contents will be sent (the receiver needs to have the same ENDIANness as the sender) otherwise each of the fields should be converted before sending. – user3629249 Oct 19 '16 at 09:37
  • suggest both the sender and the receiver use the same struct definition – user3629249 Oct 19 '16 at 09:39
  • @user3629249 this is more difficult as you are suggesting because to align the same struct on both platforms, you have to take care with the real size of this data structure due to the default pack(size). – J. Piquard Oct 19 '16 at 11:25
  • @user3629249 - I did try what you suggest but all of the variables were empty when trying to print to NSLog. There is something more to it. – James Oct 19 '16 at 12:51
  • @J. Piquard - Thank you for the links. I knew there had to be something on here but, I was having trouble finding it. I will try what is suggested there. – James Oct 19 '16 at 12:54
  • @James Did you tried some code at iOS side in objective-c ? – J. Piquard Oct 19 '16 at 13:02
  • please post the code where the packet is read/printed. – user3629249 Oct 19 '16 at 16:27
  • Have your printed out the fields in the sender to assure they are correct? What is the value of `DEMO_PRODUCT_NAME` What is actually returned by the function: `at30tse_read_temperature()`? – user3629249 Oct 19 '16 at 16:37
  • @J.Piquard, the use of `uint8_t`, etc should make the fields the same size in both platforms. – user3629249 Oct 20 '16 at 03:02

0 Answers0