Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. import bluetooth
  2. import serial
  3. import time
  4. import select
  5. import Queue
  6. import binascii
  7.  
  8. ser = serial.Serial('/dev/ttyACM0',115200, timeout = 1)#parity = serial.PARITY_EVEN,
  9. #time.sleep(2)
  10.  
  11. if ser.isOpen():
  12. print ("ACM0 Port open")
  13.  
  14. print ("Waiting for Bluetooth connection")
  15. server_sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
  16. port = 1
  17. server_sock.bind(("",port))
  18. server_sock.listen(1)
  19. print("Listening on port",port)
  20. client_sock,address = server_sock.accept()
  21. print ("Accepted connection from ",address)
  22.  
  23. while 1:
  24.  
  25. print "Waiting for data"
  26. try:
  27.  
  28. recvdata = client_sock.recv(10)
  29. if (recvdata == "9"):
  30. print ("Exiting...")
  31. break
  32. else:
  33. print ("Received \"%s\" through Bluetooth" % recvdata)
  34. #test = 0x35
  35. #print (recvdata == test)
  36. ser.write(bytearray(recvdata))#.encode('hex')) #sending data to ACM0
  37. print ("Sent \"%s\" to ACM0" % recvdata)
  38.  
  39. #time.sleep(0.1)
  40. print ("ACM0 response:")
  41. x = ser.read(ser.inWaiting()).encode('hex') #read form acm0
  42.  
  43. #time.sleep(0.3)
  44. print ("Read before conversion ", x)
  45. ##
  46. ## string x --> wyslal jako byte / hex
  47. ##
  48. print type(x)
  49. new = bytearray.fromhex(x)
  50. print type(new)
  51. new = str(new)
  52. print type(new)
  53. print ("After conversion " , new )
  54. client_sock.send(new) #send data back to computer
  55. del recvdata
  56. x= ""
  57. new = ""
  58.  
  59. except:
  60. print "Error"
  61. time.sleep(0.1)
  62.  
  63. client_sock.close()
  64. server_sock.close()
  65. ser.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement