I'm working on a communication system with RFD868x module, which is supporting mavlink. I'm writing the programs in python with pymavlink. First I test the system with this code:
import json
import time
from pymavlink import mavutil
my_dict = {
"lng": 12.34567898910,
"lat": 12.34567898910
}
# Establish connection to serial port
master = mavutil.mavlink_connection('/dev/serial0', baud=57600)
while True:
text = json.dumps(my_dict)
msg=master.mav.statustext_encode(
mavutil.mavlink.MAV_SEVERITY_INFO,
text.encode()
)
master.mav.send(msg)
print("sent")
time.sleep(1)
And on the other side:
import json
import time
from pymavlink import mavutil
# Establish connection to serial port
master = mavutil.mavlink_connection('com7', baud=57600)
print("begin")
while True:
msg = master.recv_match(blocking=True, timeout=.1)
if msg!=None:
if msg.get_type() != 'BAD_DATA':
print (msg)
Sometimes I got some BAD_DATA or UNKNOW_DATA, but this informations are delivered. I don't use any other types in this dialect, becouse they were worked horrible (just 1-2 message arrived, and then the tho communication freezed.)
Ofc, I want to communicate not just one direction, so I write a code to send and read datas.
import json
import time
from pymavlink import mavutil
master = mavutil.mavlink_connection('/dev/serial0', baud=57600)
def send_message(dats:dict):
text = json.dumps(dats)
msg=master.mav.statustext_encode(
mavutil.mavlink.MAV_SEVERITY_INFO,
text.encode()
)
master.mav.send(msg)
print("sent")
time.sleep(.1)
my_dict = {
"lng": 12.34567898910,
"lat": 12.34567898910
}
t=time.time()
while True:
if time.time()-t>1:
send_message(my_dict)
t=time.time()
mess = master.recv_match(blocking=True, timeout=1)
if mess!=None:
print(mess)
send_message(my_dict)
And tho other side:
import json
import time
from pymavlink import mavutil
# Establish connection to serial port
master = mavutil.mavlink_connection('com7', baud=57600)
def cmdLong(dats:list):
cmd = master.mav.command_long_encode(
0,
0, # target component ID
mavutil.mavlink.MAV_CMD_USER_1, # command ID
0, # confirmation
dats[0], # parameter 1 (not used)
dats[1], # parameter 2 (not used)
dats[2], # parameter 3 (not used)
dats[3], # parameter 4 (not used)
dats[4], # parameter 5 (string)
dats[5], # parameter 6 (not used)
dats[6]
)
master.mav.send(cmd)
print("writed")
time.sleep(.1)
t=time.time()
print("begin")
while True:
print("Waiting for message...")
if time.time()-t>20:
cmdLong([0,0,0,0,0,0,0])
t=time.time()
msg = master.recv_match(blocking=True, timeout=1)
if msg!=None:
if msg.get_type() != 'BAD_DATA':
print (msg)
Now the text messages are arrived until the command message sended. After the command message sended, which isn't detecteed by the reader, the communication was frozen. After this I test the master-slave type communication with this code, but it doesn't work either.
import json
import time
from pymavlink import mavutil
# Establish connection to serial port
master = mavutil.mavlink_connection('/dev/serial0', baud=57600)
def send_message(dats:dict):
text = json.dumps(dats)
msg=master.mav.statustext_encode(
mavutil.mavlink.MAV_SEVERITY_INFO,
text.encode()
)
master.mav.send(msg)
print("sent")
time.sleep(.1)
my_dict = {
"lng": 12.34567898910,
"lat": 12.34567898910
}
print("begin")
while True:
mess = master.recv_match(blocking=True, timeout=.1)
if mess!=None:
if mess.get_type() != 'BAD_DATA':
print(mess)
send_message(my_dict)
The other side:
import json
import time
from pymavlink import mavutil
# Establish connection to serial port
master = mavutil.mavlink_connection('com7', baud=57600)
def cmdLong(dats:list):
cmd = master.mav.command_long_encode(
0,
0, # target component ID
mavutil.mavlink.MAV_CMD_USER_1, # command ID
0, # confirmation
dats[0], # parameter 1 (not used)
dats[1], # parameter 2 (not used)
dats[2], # parameter 3 (not used)
dats[3], # parameter 4 (not used)
dats[4], # parameter 5 (string)
dats[5], # parameter 6 (not used)
dats[6]
)
master.mav.send(cmd)
print("writed")
time.sleep(.1)
print("begin")
cmdLong([0,0,0,0,0,0,0])
while True:
msg = master.recv_match(blocking=True, timeout=.1)
if msg!=None:
if msg.get_type() != 'BAD_DATA':
print (msg)
cmdLong([0,0,0,0,0,0,0])
Here I've started the programs in the right order, but it doesn't detect anything, (and then of course it doesn't send anything)
I don't know, what is the reason of this errors. If may somebady can help, I will so thankful!
The error was not in the codes, it was a wrong setting.
If you want to use RFD868x with raspberry pi's GPIO serial port, you have to enable the serial interface, and have to disable the serial login shell in raspi-config
.
After the sudo raspi-config
command in Interface Options and in Serial Port the following settings should be used:
I hope I have helped others.