User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Bluetooth serial freezing

Wed Apr 02, 2014 1:40 pm

I have a problem with python serial occasionally freezing.
Basically I am using a USB Bluetooth dongle to send and receive messages.
This works fine but very occasionally it freezes. I have tracked it down to the bit where I am sending a message and the python reports on the occasions where a control C will bring things back to life:-

Code: Select all

  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 471, in write
    n = os.write(self.fd, d)
KeyboardInterrupt
This might be because the bluetooth channel is being dropped but I don't know.
I open it up with:-

Code: Select all

blue = serial.Serial('/dev/rfcomm(0)', timeout = 1)
Does anyone know:-
1) How to stop this? - like I say most of the time it runs fine
and / or
2) How to detect a possible dropped bluetooth communication link before I try and send a message?

Thanks

camolas
Posts: 69
Joined: Mon Jun 17, 2013 6:58 pm

Re: Bluetooth serial freezing

Thu Apr 03, 2014 12:48 pm

Hi,

Can you please explain me how you setup the bt usb for serial port? You are communicate with kind of device ?
Im trying to communicate to a android phone but no success, i will try to help you after you explain me your setup

Bye

User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Bluetooth serial freezing

Thu Apr 03, 2014 1:06 pm

Can you please explain me how you setup the bt usb for serial port?
Open up the Bluetooth Devices manager, this should be in the bottom right hand corner of the desktop next to the CPU usage display. Then right click on the device you want to communicate with ( do a scan if it is not already paired and trusted ) and from the pop up menu select "Connect to Serial services", there will be some scanning in the bottom left of the Bluetooth manager window and it will say something like "connected as /dev/rfcomm0 " and there will be a large window pop up and say the same thing.
Then from python open up the serial port with

Code: Select all

device = serial.Serial('/dev/rfcomm0', timeout = 1)
Then write to it using

Code: Select all

device.write(message) # where message is a string or char array
and read using

Code: Select all

n = 0
while n== 0 :
   n = device.inWaiting()
input = device.read(n) # read n bytes from the serial device

camolas
Posts: 69
Joined: Mon Jun 17, 2013 6:58 pm

Re: Bluetooth serial freezing

Thu Apr 03, 2014 2:15 pm

What device are you connecting to?

User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Bluetooth serial freezing

Thu Apr 03, 2014 2:19 pm

A Lego brick.

camolas
Posts: 69
Joined: Mon Jun 17, 2013 6:58 pm

Re: Bluetooth serial freezing

Thu Apr 03, 2014 2:23 pm

I dont have one to try :( , can you try with a PC or something else to see if the problem is the same?

User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Bluetooth serial freezing

Thu Apr 03, 2014 2:31 pm

can you try with a PC
No I don't have one.

camolas
Posts: 69
Joined: Mon Jun 17, 2013 6:58 pm

Re: Bluetooth serial freezing

Thu Apr 03, 2014 2:40 pm

The easy way is to buy a serial bt module for the pi and use it in python as serial port i have one and it works. You need to test your setup with a different bt device to see if the problem is from pi or lego... try to use a phone with bt serial port or bt gps...

User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Bluetooth serial freezing

Thu Apr 03, 2014 2:47 pm

The easy way is to buy a serial bt module for the pi and use it in python as serial port i have one and it works.
Yes this is exactly what I have done if you read my original post. And it work most of the time. It does not work some time.
try to use a phone with bt serial port or bt gps.
I do not have a phone with BT.

camolas
Posts: 69
Joined: Mon Jun 17, 2013 6:58 pm

Re: Bluetooth serial freezing

Thu Apr 03, 2014 2:50 pm

are you using a usb bt or this serial? http://blog.miguelgrinberg.com/post/a-c ... spberry-pi

User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Bluetooth serial freezing

Thu Apr 03, 2014 3:44 pm

are you using a usb bt
Yes.

I have made a bit of progress. I find things are 100% reliable if I follow every message sent to bluetooth with a print statement. I have tried replacing this print with a delay and a flush but nothing works like a print.

The whole program is inside pygame framework if that makes any odds. This is my message send function:-

Code: Select all

def messageSend(message):
    #print"going to send message"
    #time.sleep(0.2)
    if brick.isOpen() == True :
      for n in range(0, 2 + ord(message[0]) + (ord(message[1]) * 256 )):
         brick.write(message[n])
      print"sent message"   
      #brick.flush() # output all data now
      #time.sleep(0.4)   
    else :
        print"Serial port not open"
The things I have tried are commented out. Like I say only the print statement works.

So the question is what does the print statement do that flush or sleep will not do?


User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Bluetooth serial freezing

Thu Apr 03, 2014 5:39 pm

While I appreciate you are trying to help, what has pairing a bluetooth device got to do with my problem?
I have already paired the devices successfully, it is using them that is being a problem like I said in my posts.

camolas
Posts: 69
Joined: Mon Jun 17, 2013 6:58 pm

Re: Bluetooth serial freezing

Thu Apr 03, 2014 5:52 pm

try minicom -s set the serial device to be /dev/rfcomm0 (see in /etc/bluetooth/rfcomm.conf if is rfcomm0) and see if it works from there

User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Bluetooth serial freezing

Thu Apr 03, 2014 6:38 pm

Can you please explain the thinking behind this, it just seems to me you are suggesting random things to do with the setup of a bluetooth device. As I keep saying I am way beyond that stage.

camolas
Posts: 69
Joined: Mon Jun 17, 2013 6:58 pm

Re: Bluetooth serial freezing

Thu Apr 03, 2014 6:43 pm

My idea is to use minicom -s to connect to lego bt and send the commands and see if you can communicate without any freezing

User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Bluetooth serial freezing

Thu Apr 03, 2014 6:58 pm

Thanks but I think you are missing the point of this thread.

camolas
Posts: 69
Joined: Mon Jun 17, 2013 6:58 pm

Re: Bluetooth serial freezing

Thu Apr 03, 2014 7:03 pm

Please explain it better and i will try to hellpe

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Bluetooth serial freezing

Fri Apr 04, 2014 5:09 pm

This is a good one. Are you sure adding the print statement has fixed the problem 100% or have you just been lucky since trying it?
My thinking is that this line may be part of the problem...

Code: Select all

for n in range(0, 2 + ord(message[0]) + (ord(message[1]) * 256 )):
...is there a chance the message could be so long that the Bluetooth isn't actually freezing but just taking a very long time to send the message?
What do the first 2 characters in the message represent?

Clutching at straws.
Dave.
Apple say... Monkey do !!

User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Bluetooth serial freezing

Fri Apr 04, 2014 9:14 pm

Are you sure adding the print statement has fixed the problem 100% or have you just been lucky since trying it?
Just been lucky.
I moved the SD card to another Pi (same amount of memory but earlier vintage) in another location, and re arranged the order of some modules in the software and it crashes more than ever.
is there a chance the message could be so long that the Bluetooth isn't actually freezing but just taking a very long time to send the message?
No the messages are only about 25 bytes, the first two bytes show how many bytes there are in the message but the ones I am sending are actually short.

Further investigation today shows that when it freezes and I look at the debug in the IDLE debug menu it is freezing in the send routine of pyserial with an error 11 "resource temporarily unavailable" after sending about three bytes, it is not in the receive command like all the posts I have read about this error say.
It will not work again until I quit the desktop and then start it again.

It is certainly not the receiving device because that keeps on running during the reloading of python and can be controlled again once python restarts.

My latest thinking is that the python serial library is stuffed so I am looking for alternatives. Gordan's WiringPi has a serial library so I plan to try that next in the absence of any other suggestions here.

User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Bluetooth serial freezing

Sat Apr 05, 2014 11:16 am

Well I installed wiring Pi and modified my code to use it.

Sadly it still freezes :cry:

Now when it freezes a Ctrl C will not recover python and I have to log out of the desktop and then back in.
I made a count of three runs of my program and it froze after 151, 107 and 232 messages being sent. There are only four different messages by the way and all of them are sent successfully. Like previously (using py serial ) the freeze always occurs when sending the message so the fault must lye deeper into the operating system or in the Bluetooth interface module.

So basically it looks like the Raspberry Pi can not reliably use Bluetooth, at least not in all its form of communications.

Anyone know an alternative to bluetooth bluez-utils ?

mkp
Posts: 1
Joined: Tue Sep 04, 2018 6:44 am

Re: Bluetooth serial freezing

Tue Sep 04, 2018 6:57 am

HI,
I am also facing same.
you can echo data to /dev/rfcomm0 only once which has been paired to android device.after that we are not able to send data via echo it will be freezed.
Also after connection i am not able to get below log when i try to listen from android device.
"rfcomm listen /dev/rfcomm0 1"

Connection from 22:22:22:22:22:22 to /dev/rfcomm0
Press CTRL-C for hangup


Please le me know ASAP.

User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Bluetooth serial freezing

Tue Sep 04, 2018 12:27 pm

Please le me know ASAP.
Let you know what. It freezes end of story.
If you want a fix go and ask the people who write Linux for the Pi.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11753
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Bluetooth serial freezing

Tue Sep 04, 2018 1:58 pm

mkp wrote:
Tue Sep 04, 2018 6:57 am


Please le me know ASAP.
You reply to a post from 2014, (four years ago) and then want a reply "ASAP"? probably what is possible is that the original poster isn't looking at this forum no more..... so as soon as possible is equivalent to never!
But yes, grumpy max is still here, and as you can see is a s grumpy as ever. :mrgreen:

your post is equivalent to raising the dead, so we call this "necromancy", and so it seems you are a necromancer in a hurry. LOL.
in the last four years we have had several major updates of Linux, so *nothing* that was true for the original situation then is still valid now.

Please start a new post, (in the android section) and list as much details as you have about your (new) problem.....
locking....

Return to “Python”