не съм много запознат с езика питон и за това с доста четене и труд успях да си свърша работата става въпрос за измервателен уред който първоначално работеше с УСБ кабел и pyserial библиотеката стана отлично, но се наложи да може да работи и безжично при което с доста пак гуглене успях да го пренапиша за bluetooth. Всичко беше идеално докато не реши че повече няма да се свърже, Пробвах на 5 линукс базирани PC-та с различни дистрибуции и версии на ядрата но ефекта беше същия. Пробвам ли да чета /dev/rfcomm2
На снимката е 3.8
Код:
#!/usr/bin/env python
import serial
import datetime
import os
print '\nTalevtehnology DTM v1.0\n'
print 'Contecting...\n'
bluetoothSerial = serial.Serial( "/dev/rfcomm2", baudrate=57600 )
print '...Connected to DTM v1.0!'
os.system("sleep 1;")
bluetoothSerial.write('b')
print (str('\nBattery level: ') + str (bluetoothSerial.readline()))
os.system("hcitool rssi 00:06:66:67:4D:E0")
os.system("sleep 1;")
bluetoothSerial.write('r')
res = raw_input('\nChoice resolution: \n1 - 0.00 C\n2 - 0 C\n3 - floor 0.00 C\n')
if res == '1':
bluetoothSerial.write('1')
elif res == '2':
bluetoothSerial.write('2')
elif res == '3':
bluetoothSerial.write('3')
val = raw_input('\nChoice type value: \n1 - Normal \n2 - Max\n3 - Min\n')
if val == '1':
bluetoothSerial.write('0')
elif val == '2':
bluetoothSerial.write('x')
elif val == '3':
bluetoothSerial.write('n')
xString = input('\nEnter the number samples: ')
numberSamples = int(xString)
now = datetime.datetime.now()
os.system("mkdir -p /home/nikolai/DTM/$(date +%Y-%m-%d_%H:%M)")
os.system("cp /home/nikolai/plot.sh /home/nikolai/DTM/$(date +%Y-%m-%d_%H:%M)/plot.sh")
dataFile = open(now.strftime("/home/nikolai/DTM/%Y-%m-%d_%H:%M/data.dat"), 'w',0)
bluetoothSerial.flushInput()
bluetoothSerial.flush()
bluetoothSerial.flushOutput()
os.system("sleep 0.2;")
bluetoothSerial.write('1')
for i in range(0,numberSamples+1):
reading0 = bluetoothSerial.readline()
dataFile.writelines(str(i) + ' ' + str(reading0))
print (str(i) + ' ' + str(reading0))
bluetoothSerial.write('0')
dataFile.flush()
dataFile.close()
bluetoothSerial.close()
print 'Done!\n'
import serial
import datetime
import os
print '\nTalevtehnology DTM v1.0\n'
print 'Contecting...\n'
bluetoothSerial = serial.Serial( "/dev/rfcomm2", baudrate=57600 )
print '...Connected to DTM v1.0!'
os.system("sleep 1;")
bluetoothSerial.write('b')
print (str('\nBattery level: ') + str (bluetoothSerial.readline()))
os.system("hcitool rssi 00:06:66:67:4D:E0")
os.system("sleep 1;")
bluetoothSerial.write('r')
res = raw_input('\nChoice resolution: \n1 - 0.00 C\n2 - 0 C\n3 - floor 0.00 C\n')
if res == '1':
bluetoothSerial.write('1')
elif res == '2':
bluetoothSerial.write('2')
elif res == '3':
bluetoothSerial.write('3')
val = raw_input('\nChoice type value: \n1 - Normal \n2 - Max\n3 - Min\n')
if val == '1':
bluetoothSerial.write('0')
elif val == '2':
bluetoothSerial.write('x')
elif val == '3':
bluetoothSerial.write('n')
xString = input('\nEnter the number samples: ')
numberSamples = int(xString)
now = datetime.datetime.now()
os.system("mkdir -p /home/nikolai/DTM/$(date +%Y-%m-%d_%H:%M)")
os.system("cp /home/nikolai/plot.sh /home/nikolai/DTM/$(date +%Y-%m-%d_%H:%M)/plot.sh")
dataFile = open(now.strftime("/home/nikolai/DTM/%Y-%m-%d_%H:%M/data.dat"), 'w',0)
bluetoothSerial.flushInput()
bluetoothSerial.flush()
bluetoothSerial.flushOutput()
os.system("sleep 0.2;")
bluetoothSerial.write('1')
for i in range(0,numberSamples+1):
reading0 = bluetoothSerial.readline()
dataFile.writelines(str(i) + ' ' + str(reading0))
print (str(i) + ' ' + str(reading0))
bluetoothSerial.write('0')
dataFile.flush()
dataFile.close()
bluetoothSerial.close()
print 'Done!\n'
След проблемите попаднах на pybluez и пренаписах кода с него но се натъкнах на няколко проблема
Код:
#!/usr/bin/env python
import sys
import datetime
import os
import bluetooth
bdaddr = '00:06:66:67:4D:E0'
port = 1
print '\nTalevtehnology DTM v1.0\n'
print 'Contecting...\n'
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM)
sock.connect((bdaddr, port))
print '...Connected to DTM v1.0!'
sock.settimeout(2.0)
sock.send('m')
os.system("sleep 1;")
#tuka sock.flush()
sock.send('b')
print (str('\nBattery level: ') + str (sock.recv(256)))
os.system("hcitool rssi 00:06:66:67:4D:E0")
os.system("sleep 1;")
sock.send('r')
res = raw_input('\nChoice resolution: \n1 - 0.00 C\n2 - 0 C\n3 - floor 0.00 C\n')
if res == '1':
sock.send('1')
elif res == '2':
sock.send('2')
elif res == '3':
sock.send('3')
val = raw_input('\nChoice type value: \n1 - Normal \n2 - Max\n3 - Min\n')
if val == '1':
sock.send('0')
elif val == '2':
sock.send('x')
elif val == '3':
sock.send('n')
xString = input('\nEnter the number samples: ')
numberSamples = int(xString)
now = datetime.datetime.now()
os.system("mkdir -p /home/nikolai/DTM/$(date +%Y-%m-%d_%H:%M)")
os.system("cp /home/nikolai/plot.sh /home/nikolai/DTM/$(date +%Y-%m-%d_%H:%M)/plot.sh")
dataFile = open(now.strftime("/home/nikolai/DTM/%Y-%m-%d_%H:%M/data.dat"), 'w',0)
#tuka sock.flush()
sock.send('1')
for i in range(0,numberSamples+1):
reading0 = sock.recv(256)
dataFile.writelines(str(i) + ' ' + str(reading0))
print (str(i) + ' ' + str(reading0))
sock.send('0')
dataFile.flush()
dataFile.close()
sock.close()
print 'Done!\n'
import sys
import datetime
import os
import bluetooth
bdaddr = '00:06:66:67:4D:E0'
port = 1
print '\nTalevtehnology DTM v1.0\n'
print 'Contecting...\n'
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM)
sock.connect((bdaddr, port))
print '...Connected to DTM v1.0!'
sock.settimeout(2.0)
sock.send('m')
os.system("sleep 1;")
#tuka sock.flush()
sock.send('b')
print (str('\nBattery level: ') + str (sock.recv(256)))
os.system("hcitool rssi 00:06:66:67:4D:E0")
os.system("sleep 1;")
sock.send('r')
res = raw_input('\nChoice resolution: \n1 - 0.00 C\n2 - 0 C\n3 - floor 0.00 C\n')
if res == '1':
sock.send('1')
elif res == '2':
sock.send('2')
elif res == '3':
sock.send('3')
val = raw_input('\nChoice type value: \n1 - Normal \n2 - Max\n3 - Min\n')
if val == '1':
sock.send('0')
elif val == '2':
sock.send('x')
elif val == '3':
sock.send('n')
xString = input('\nEnter the number samples: ')
numberSamples = int(xString)
now = datetime.datetime.now()
os.system("mkdir -p /home/nikolai/DTM/$(date +%Y-%m-%d_%H:%M)")
os.system("cp /home/nikolai/plot.sh /home/nikolai/DTM/$(date +%Y-%m-%d_%H:%M)/plot.sh")
dataFile = open(now.strftime("/home/nikolai/DTM/%Y-%m-%d_%H:%M/data.dat"), 'w',0)
#tuka sock.flush()
sock.send('1')
for i in range(0,numberSamples+1):
reading0 = sock.recv(256)
dataFile.writelines(str(i) + ' ' + str(reading0))
print (str(i) + ' ' + str(reading0))
sock.send('0')
dataFile.flush()
dataFile.close()
sock.close()
print 'Done!\n'
Проблема тук е че не мога да намеря аналог на bluetoothSerial.flush(). Прибора работи и със смартфони ползва bluetooth terminal и затова ми трябва начин за изчистването на sock
Другият проблем е
Код:
for i in range(0,numberSamples+1):
reading0 = bluetoothSerial.readline()
dataFile.writelines(str(i) + ' ' + str(reading0))
print (str(i) + ' ' + str(reading0))
reading0 = bluetoothSerial.readline()
dataFile.writelines(str(i) + ' ' + str(reading0))
print (str(i) + ' ' + str(reading0))
Че тук данните се записват правилно дори сигнала да е слаб данните пристигат правилно и без разместване
докато тук
Код:
for i in range(0,numberSamples+1):
reading0 = sock.recv(256)
dataFile.writelines(str(i) + ' ' + str(reading0))
print (str(i) + ' ' + str(reading0))
reading0 = sock.recv(256)
dataFile.writelines(str(i) + ' ' + str(reading0))
print (str(i) + ' ' + str(reading0))
са разбъркани и... сякаш липсва буфериране
Свързването с pybluez е без проблемно. Моля ви помагайте изчетох какво ли не но не успявам да се справя.