]> sigrok.org Git - libserialport.git/blobdiff - examples/send_receive.c
change type of result variables to ssize_t
[libserialport.git] / examples / send_receive.c
index e1aabf002c0b4b99ba7785b1e3ffbd3e6218ded6..265648916d367b261f5332eb7520b19cec6f50d6 100644 (file)
@@ -81,12 +81,12 @@ int main(int argc, char **argv)
                        printf("Timed out, %d/%d bytes sent.\n", result, size);
 
                /* Allocate a buffer to receive data. */
-               char buf[size + 1];
+               char *buf = malloc(size + 1);
 
                /* Try to receive the data on the other port. */
                printf("Receiving %d bytes on port %s.\n",
                                size, sp_get_port_name(rx_port));
-               check(sp_blocking_read(rx_port, buf, size, timeout));
+               result = check(sp_blocking_read(rx_port, buf, size, timeout));
 
                /* Check whether we received the number of bytes we wanted. */
                if (result == size)
@@ -97,6 +97,9 @@ int main(int argc, char **argv)
                /* Check if we received the same data we sent. */
                buf[result] = '\0';
                printf("Received '%s'.\n", buf);
+
+               /* Free receive buffer. */
+               free(buf);
        }
 
        /* Close ports and free resources. */