<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Sending serial data</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/89465/sending-serial-data</link><description>Hi, 
 I&amp;#39;m sending values over ble. On the client side I opened an serial monitor. Everything seems fine. The values are received in the right way. 
 
 But when I try to read the serial port in python it says: 
 
 There is an random character, what isn</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 30 Jun 2022 19:55:13 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/89465/sending-serial-data" /><item><title>RE: Sending serial data</title><link>https://devzone.nordicsemi.com/thread/374977?ContentTypeID=1</link><pubDate>Thu, 30 Jun 2022 19:55:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6d87e5a2-bad2-4470-a69b-5d9a4521c31c</guid><dc:creator>Juniorprogrammer</dc:creator><description>&lt;p&gt;Hi Vidar,&lt;/p&gt;
&lt;p&gt;I processed your feedback. I don&amp;#39;t know if I did it the right way. See my code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void app_timer_handler(void * p_context)
{
  if(m_conn_handle != BLE_CONN_HANDLE_INVALID)
  {
    uint8_t data_array[80];

    float val0;
    float val1;
    float val2;
    float val3;

    ret_code_t err_code;

    err_code = nrfx_saadc_sample_convert(0, &amp;amp;m_sample);
    APP_ERROR_CHECK(err_code);

    val0 = m_sample * 3.3 / 4096;
    memcpy(&amp;amp;data_array[0], &amp;amp;val0, sizeof(val0));

    err_code = nrfx_saadc_sample_convert(1, &amp;amp;m_sample);
    APP_ERROR_CHECK(err_code);

    val1 = m_sample * 3.3 / 4096;
    memcpy(&amp;amp;data_array[20], &amp;amp;val1, sizeof(val1));

    err_code = nrfx_saadc_sample_convert(2, &amp;amp;m_sample);
    APP_ERROR_CHECK(err_code);

    val2 = m_sample * 3.3 / 4096;
    memcpy(&amp;amp;data_array[40], &amp;amp;val2, sizeof(val2));

    err_code = nrfx_saadc_sample_convert(3, &amp;amp;m_sample);
    APP_ERROR_CHECK(err_code);

    val3 = m_sample * 3.3 / 4096;
    memcpy(&amp;amp;data_array[60], &amp;amp;val3, sizeof(val3));
  
    uint16_t d_len = sizeof(data_array)-1;

    err_code = ble_nus_data_send(&amp;amp;m_nus, data_array, &amp;amp;d_len, m_conn_handle); 
    APP_ERROR_CHECK(err_code);
  }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I was using an data_array[80] of 80, because I was using before an string in the array. Can I make it smaller and will it work right in this way?&lt;/p&gt;
&lt;p&gt;This is what I&amp;#39;m receiving now:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;b&amp;#39;\xcdL\x07=\xfc\x00\x00 \x00\x00\x00\x00\x98#\x00 \x01\x00\x00\x00\x00\x00\x04= \x01\x00 \x00\x10\x00@\x00\x00\x00\x00\x01\x00\x00\x00\x9a\x91\xaa?D\x85\x00@\x0c\x0c\x00 \x11\xc6\x01\x00$\x0c\x00 f\xe6.@\x00\x00\x00\x00\xe8\x1d\x00 \x00\x00\x00\x00\xdc\x00\x00\r\n&amp;#39;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I tried this in python.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;import serial
import matplotlib.pyplot as plt
import struct

ser = serial.Serial(&amp;#39;COM7&amp;#39;,115200)
ser.close()
ser.open()
while True:

    data = ser.readline(4)
    val = struct.unpack(&amp;#39;&amp;lt;f&amp;#39;, data)
    print(val)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;After this is what I get once then it stops.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;    val = struct.unpack(&amp;#39;&amp;lt;f&amp;#39;, data)
struct.error: unpack requires a buffer of 4 bytes
(0.02980956993997097,)
(1.0842347427221237e-19,)
(0.0,)
(1.0853798708826268e-19,)
(1.401298464324817e-45,)
(0.04672851413488388,)
(1.0842393956130693e-19,)
(2.0009765625,)
(0.0,)
(1.401298464324817e-45,)
(1.372045874595642,)
(2.008133888244629,)
(1.0846007701431784e-19,)
(1.6288833479158106e-40,)
(1.0846038720704755e-19,)
(2.7328124046325684,)
(0.0,)
(1.085191687293271e-19,)
(0.0,)
(3.944407969607931e-31,)

Process finished with exit code 1&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I dont have much experience with python, therefore I don&amp;#39;t know if I send wrong or reading it wrong.&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;JP&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending serial data</title><link>https://devzone.nordicsemi.com/thread/374876?ContentTypeID=1</link><pubDate>Thu, 30 Jun 2022 11:19:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aa563ee9-d18d-4e81-918c-a1c65f23775a</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You can use memcpy() to copy one or several float samples to your uint8_t array.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; memcpy(&amp;amp;array[offset],&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;float_sample, sizeof(float_sample));&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending serial data</title><link>https://devzone.nordicsemi.com/thread/374868?ContentTypeID=1</link><pubDate>Thu, 30 Jun 2022 10:55:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f825072e-51f7-4b6c-be9a-eb83cb58131f</guid><dc:creator>Juniorprogrammer</dc:creator><description>&lt;p&gt;Hi Vidar,&lt;/p&gt;
&lt;p&gt;I tried that before, but I can&amp;rsquo;t pass a float value to ble_nus. It asks for an uint8. Therefore I used a string.&lt;/p&gt;
&lt;p&gt;Also tried to send an data_array over ble_nus, because I have 4 SAADC values. But it wouldn&amp;rsquo;t work for me.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Do you know how to send an array with my float values over ble?&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;JP&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending serial data</title><link>https://devzone.nordicsemi.com/thread/374866?ContentTypeID=1</link><pubDate>Thu, 30 Jun 2022 10:48:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8f9f97a2-ec25-4dad-839e-fb0f256b5770</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t think you should need to convert the float value to a string. I mean, you could, but it will just add more overhead to your transfer if you are going to convert it back again anyway. Instead you can remove the sprintf() function and pass the value directly to your BLE send function.&lt;/p&gt;
&lt;p&gt;The python code may look something like this (not tested):&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="python"&gt;value, = struct.unpack(&amp;#39;&amp;lt;f&amp;#39;, bytearray(&amp;lt;4 byte array received on UART&amp;gt;))

printf(value)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending serial data</title><link>https://devzone.nordicsemi.com/thread/374760?ContentTypeID=1</link><pubDate>Wed, 29 Jun 2022 13:59:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:109de082-51f9-487f-a193-68ba85e65be2</guid><dc:creator>Juniorprogrammer</dc:creator><description>&lt;p&gt;I did watched the data on python.&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1656511110249v1.png" /&gt;&lt;/p&gt;
&lt;p&gt;In the 2th, 3th etc it add an \x sequence. Is this something on the client side of my software?&lt;/p&gt;
&lt;p&gt;This is my python code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;import serial
import matplotlib.pyplot as plt

plt.ion()
fig=plt.figure()


i=0
x=list()
y=list()
i=0
ser = serial.Serial(&amp;#39;COM7&amp;#39;,115200)
ser.close()
ser.open()
while True:

    data = ser.readline()
    print(data.decode(&amp;quot;utf-8&amp;quot;))
    #x.append(i)
    #y.append(data.decode())

    #plt.scatter(i, float(data.decode()))
    #i += 1
    #plt.show()
    #plt.pause(0.0001)  # Note this correction&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>