0

I use a Raspberry Pi 3 Model B and I have a Bluetooth HC-08 module.

I would like to be able to send and receive data. I use the port /dev/ttyS0 and I changed the file /boot/config.txt for "enable_uart=1"

I connected the RX pin of the module to pin 8 of the RPI and the TX pin to pin 10. ( TX -> RX / RX -> TX)

To test, I use a mobile application, LightBlue. I can read the data I send from the application but I can't write.

I also changed parameters in raspi-config, Interface Options / Serial -> No -> Yes

Here's my test code :

import time
import serial

print "Starting program"

ser = serial.Serial('/dev/ttyS0', baudrate=9600,
                    parity=serial.PARITY_NONE,
                    stopbits=serial.STOPBITS_ONE,
                    bytesize=serial.EIGHTBITS
                    )
time.sleep(1)
try:
    ser.write('Hello World\r\n')
    ser.write('Serial Communication Using Raspberry Pi\r\n')
    ser.write('By: Embedded Laboratory\r\n')
    print 'Data Echo Mode Enabled'
    while True:
        if ser.inWaiting() > 0:
            data = ser.read()
            print data

except KeyboardInterrupt:
    print "Exiting Program"

except:
    print "Error Occurs, Exiting Program"

finally:
    ser.close()
    pass

I don't know why I can read but not write data ... Do I miss something ?

Some help would be appreciated, thanks

0

I finally found the problem. The RX pin on my module was down. I changed Bluetooth module to a new one. And I could write again.

I share my test code if it can help someone

#!/usr/bin/env python
# serial_read.py

import time
import serial

ser = serial.Serial(port='/dev/ttyS0', baudrate=9600)

try:
  while True:
    if ser.inWaiting() > 0:
      data = ser.readline()
      print(data)
except KeyboardInterrup:
  print "Exiting program"
except:
  print "Error occurs"
finally:
  ser.close()
  pass


#!/usr/bin/env python
# serial_write.py

import time
import serial

ser = serial.Serial(port='/dev/ttyS0', baudrate=9600)

counter = 0

try:
  while True:
    ser.write('Write counter: %d \n'%(counter))
    time.sleep(1)
    counter++
except KeyboardInterrupt:
  print "Exiting program"
except:
  print "Error occurs"
finally:
  ser.close()
  pass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.