Sending serial data

Hi,

I'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't visible in the serial monitor(putty).

    sprintf(data_array, "%.2f", val0);
    uint16_t d_len = strlen(data_array);

    err_code = ble_nus_data_send(&m_nus, data_array, &d_len, m_conn_handle); 
    APP_ERROR_CHECK(err_code);

This is the way I'm sending my values. Am I missing something that I didn't see? Can someone help me out? 

Maybe it isn't a question to ask here, but maybe someone know the answer.

Kind regards,

JP 

Parents Reply Children
  • Hi Vidar,

    I processed your feedback. I don't know if I did it the right way. See my code:

    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, &m_sample);
        APP_ERROR_CHECK(err_code);
    
        val0 = m_sample * 3.3 / 4096;
        memcpy(&data_array[0], &val0, sizeof(val0));
    
        err_code = nrfx_saadc_sample_convert(1, &m_sample);
        APP_ERROR_CHECK(err_code);
    
        val1 = m_sample * 3.3 / 4096;
        memcpy(&data_array[20], &val1, sizeof(val1));
    
        err_code = nrfx_saadc_sample_convert(2, &m_sample);
        APP_ERROR_CHECK(err_code);
    
        val2 = m_sample * 3.3 / 4096;
        memcpy(&data_array[40], &val2, sizeof(val2));
    
        err_code = nrfx_saadc_sample_convert(3, &m_sample);
        APP_ERROR_CHECK(err_code);
    
        val3 = m_sample * 3.3 / 4096;
        memcpy(&data_array[60], &val3, sizeof(val3));
      
        uint16_t d_len = sizeof(data_array)-1;
    
        err_code = ble_nus_data_send(&m_nus, data_array, &d_len, m_conn_handle); 
        APP_ERROR_CHECK(err_code);
      }
    }

    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?

    This is what I'm receiving now:

    b'\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'

    I tried this in python. 

    import serial
    import matplotlib.pyplot as plt
    import struct
    
    ser = serial.Serial('COM7',115200)
    ser.close()
    ser.open()
    while True:
    
        data = ser.readline(4)
        val = struct.unpack('<f', data)
        print(val)

    After this is what I get once then it stops.

        val = struct.unpack('<f', 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

    I dont have much experience with python, therefore I don't know if I send wrong or reading it wrong.

    Kind regards,

    JP

Related