Search code examples
pythondebuggingbluetoothserial-portraspberry-pi3

How to use logging (or other methods) in order to receive full report/log that includes every step the compiler made prior to program getting stuck?


TL;DR In Python, How to use logging (or other methods) in order to receive full report/log that includes every step the compiler made prior to program getting stuck (or hang on a Serial Write command)?

I'm trying to open serial communication between a Raspberry Pi 3B and a sensor with a built-in bluetooth module. I'm running the Python code using Tonny IDE (Code attached down bellow) and the run always get stuck on: ser.write(data.encode())

So far I've tried :

  1. In the Python code, changing the port address from '/dev/ttyAMA0' to '/dev/serial0' or '/dev/rfcomm0'
  2. Manually pair the bluetooth sensor to the Raspberry Pi 3b '/dev/rfcomm0' port. It worked, but the Python code still got stuck on serial write command.
  3. In the Serial configuration, I've tried changing the timeout or adding a writeTimeout. Didn't help.

The code:

import numpy as np
import serial
from datetime import datetime
import csv
import os
import json


COLS = 16
ROWS = 16

Values = np.zeros((ROWS,COLS))

ser = serial.Serial(
    port='/dev/ttyAMA0',
    baudrate = 115200, #RECOMMENDED BAUD RATE FOR THE SENSOR
    timeout = 3.0)


time.sleep(1)
print("Started")
data="S"
ser.write(data.encode()) #CODE STUCKS HERE.
.
.
.

Solution

  • If you want to use Bluetooth's Serial Port Profile (SPP) then you need to use Python's socket library. e.g: https://blog.kevindoran.co/bluetooth-programming-with-python-3/

    However, if the sensor with a built-in Bluetooth module is Bluetooth Low Energy (BLE) then you will have to use GATT enquiry rather than SPP.