由于要用树莓派和lte,所有的数据都要经过公网,但是无论是树莓派还是用户终端都没公网ip,所以要用一个服务器中转一下。
socket_transfer.py就是中转服务器。serial_socket_connect.py运行在树莓派上,是socket客户端。socket_decode.py运行在用户终端,也是socket客户端。
现在socket_transfer这个服务器软件能够运行,但是还是不实用,它每次先等树莓派连上,然后再等客户端连上。
实际这树莓派和用户终端这两个客户端都有可能会掉线,需要断线重连,那么服务器就要做到随时在任意时刻都能被这两种客户端接入。
socket_transfer.py
#!/usr/bin/python
# coding=UTF-8
import socket, time
sock_quadcopter = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock_quadcopter.bind(("127.0.0.1", 8000))
sock_quadcopter.listen(2)
sock_client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock_client.bind(("127.0.0.1", 8001))
sock_client.listen(2)
sock_quadcopter.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock_client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
src, src_addr = sock_quadcopter.accept()
print "Source Connected by", src_addr
dst, dst_addr = sock_client.accept()
print "Destination Connected by", dst_addr
while True:
msg = src.recv(50)
if not msg:
break
try:
dst.sendall(msg)
except Exception as ex:
dst, dst_addr = sock_client.accept()
print "Destination Connected Again By", dst_addr
src, src_addr = sock_quadcopter.accept()
print "Source Connected by", src_addr
except KeyboardInterrupt:
print "Interrupted"
break
src.close()
dst.close()
sock_quadcopter.close()
sock_client.close()
socket_decode.py
#!/usr/bin/python
# coding=UTF-8
import serial
import socket, time
HOST, PORT = "127.0.0.1", 8001
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
received_data = 0
receive_buffer_counter = 0
receive_start_detect = 0
receive_buffer = [0 for i in range(50)]
hex_buffer = [0 for i in range(50)]
receive_byte_previous = 0
max_altitude_meters = 0
def hexshow(data):
e = 0
for i in data:
d = ord(i)
e = e*256 + d
return e
def get_data():
global max_altitude_meters
for i in range(34):
hex_buffer[i] = hexshow(receive_buffer[i])
#print hex_buffer
#print receive_buffer
check_byte = 0;
for temp_byte in range(30):
check_byte ^= hex_buffer[temp_byte]
if check_byte == hex_buffer[31]:
error = hex_buffer[0]
flight_mode = hex_buffer[1]
battery_voltage = (float)(hex_buffer[2])/10.0
temperature = hex_buffer[3] | ( hex_buffer[4]<<8)
roll_angle = hex_buffer[5] - 100
pitch_angle = hex_buffer[6] - 100
start = hex_buffer[7]
altitude_meters = (hex_buffer[8] | hex_buffer[9] << 8) - 1000
if altitude_meters > max_altitude_meters:
max_altitude_meters = altitude_meters
takeoff_throttle = hex_buffer[10] | hex_buffer[11] << 8
actual_compass_heading = hex_buffer[12] | hex_buffer[13] << 8
heading_lock = hex_buffer[14]
number_used_sats = hex_buffer[15]
fix_type = hex_buffer[16]
l_lat_gps = hex_buffer[17] | hex_buffer[18] << 8 | hex_buffer[19] << 16 | hex_buffer[20] << 24
l_lon_gps = hex_buffer[21] | hex_buffer[22] << 8 | hex_buffer[23] << 16 | hex_buffer[24] << 24
adjustable_setting_1 = (float)(hex_buffer[25] | hex_buffer[26] << 8) / 100.0;
adjustable_setting_2 = (float)(hex_buffer[27] | hex_buffer[28] << 8) / 100.0;
adjustable_setting_3 = (float)(hex_buffer[29] | hex_buffer[30] << 8) / 100.0;
print error, flight_mode, battery_voltage, temperature, roll_angle, pitch_angle, start, altitude_meters, takeoff_throttle
print actual_compass_heading, heading_lock, number_used_sats, fix_type, l_lat_gps, l_lon_gps, adjustable_setting_1, adjustable_setting_2, adjustable_setting_3
if __name__ == '__main__':
try:
while True:
data = sock.recv(50)
if data:
if received_data == 0:
received_data = 1
for nextByte in data:
if nextByte >= 0:
receive_buffer[receive_buffer_counter] = nextByte
if receive_byte_previous == 'J' and receive_buffer[receive_buffer_counter] == 'B':
receive_buffer_counter = 0
receive_start_detect = receive_start_detect + 1
if receive_start_detect >= 2:
get_data()
else:
receive_byte_previous = receive_buffer[receive_buffer_counter]
receive_buffer_counter = receive_buffer_counter + 1
if (receive_buffer_counter > 48):
receive_buffer_counter = 0
except KeyboardInterrupt:
if sock != None:
sock.close()
serial_socket_connect.py
#!/usr/bin/python
# coding=UTF-8
import serial
import socket, time
serial = serial.Serial('/dev/ttyUSB0', 9600)
print serial
if serial.isOpen():
print("open success")
else:
print("open failed")
HOST, PORT = "127.0.0.1", 8000
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
while True:
nextByte = serial.read(50)
try:
sock.sendall(nextByte)
except KeyboardInterrupt:
print "Interrupted"
if serial != None:
serial.close()
sock.close()
break