Cụ [@TVPL;118299] ngó qua đoạn code này và nghiên cứu giúp cháu xem liệu có vấn đề gì về code không ạ? Cháu làm cái Python đọc các mesage trên canbus để điều khiển vô lăng cho XBMC trên Raspberry Pi. Hiện tại cháu mới nghiên cứu lý thuyết mà chưa có thực nghiệm. Cháu tạm giả sử xác định được các nút trên vô lăng có các Message như sau:
up='3DA2'
down= '3DA1'
right='3DA2'
left='3DA3'
select='3DA4'
back='3DA5'
play_pause='3DA6'
home='3DA7'
shuthdown='3DA8'
Và đoạn mã của cháu sử dụng JSON-PRC API để điều khiển XBMC như sau (rất tiếc là điễn đàn đã bỏ chế độ nhập dòng code trên bài viết):
import serial
import time
import requests
import json
import urllib
# Thiet lap ket noi JSON-PRC
#Required header for XBMC JSON-RPC calls, otherwise you'll get a
#415 HTTP response code - Unsupported media type
headers = {'content-type': 'application/json'}
#Host name where XBMC is running, leave as localhost if on this PC
#Make sure "Allow control of XBMC via HTTP" is set to ON in Settings ->
#Services -> Webserver
xbmc_host = 'localhost'
#Configured in Settings -> Services -> Webserver -> Port
xbmc_port = 8888
#Base URL of the json RPC calls. For GET calls we append a "request" URI
#parameter. For POSTs, we add the payload as JSON the the HTTP request body
xbmc_json_rpc_url = "http://" + xbmc_host + ":" + str(xbmc_port) + "/jsonrpc"
#//Thiet lap ket noi JSON-PRC
# Gan cac nut tren vo lang
up='3DA2'
down= '3DA1'
right='3DA2'
left='3DA3'
select='3DA4'
back='3DA5'
play_pause='3DA6'
home='3DA7'
shuthdown='3DA8'
# //Gan cac nut tren vo lang
def readlineCR(port):
rv = ""
while True:
ch = port.read()
rv += ch
if ch=='\r' or ch=='':
return rv
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0)
# Kiem tra xem da co ket noi chua, neu chua cho 5s sau kiem tra lai
while (port.isOpen() == False):
print("serial is not connected!")
time.sleep(5)
# Gui lenh dump CANBUS
port.write("ATL1\r")
port.write("ATH1\r")
port.write("ATSP0\r")
port.write("ATS1\r")
port.write("ATAL\r")
port.write("ATMA\r")
#if port.isOpen():
#print("serial is open!")
while True:
rcv = readlineCR(port)
if (rcv == play_pause):
#Payload for the method to get the currently playing / paused video or audio
payload = {"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}
url_param = urllib.urlencode({'request': json.dumps(payload)})
response = requests.get(xbmc_json_rpc_url + '?' + url_param, headers=headers)
#response.text will look like this if something is playing
#{"id":1,"jsonrpc":"2.0","result":[{"playerid":1,"type":"video"}]}
#and if nothing is playing:
#{"id":1,"jsonrpc":"2.0","result":[]}
data = json.loads(response.text)
#result is an empty list if nothing is playing or paused.
if data['result']:
#We need the specific "playerid" of the currently playing file in order
#to pause it
player_id = data['result'][0]["playerid"]
payload = {"jsonrpc": "2.0", "method": "Player.PlayPause","params": {"playerid": player_id }, "id": 1}
response = requests.post(xbmc_json_rpc_url, data=json.dumps(payload),headers=headers)
#response.text will look like this if we're successful:
#{"id":1,"jsonrpc":"2.0","result":{"speed":0}}
if (rcv == up):
payload = {"jsonrpc": "2.0", "method": "Input.Up", "id": 1}
response = requests.post(xbmc_json_rpc_url, data=json.dumps(payload),headers=headers)
if (rcv == down):
payload = {"jsonrpc": "2.0", "method": "Input.Down", "id": 1}
response = requests.post(xbmc_json_rpc_url, data=json.dumps(payload),headers=headers)
if (rcv == right):
payload = {"jsonrpc": "2.0", "method": "Input.Right", "id": 1}
response = requests.post(xbmc_json_rpc_url, data=json.dumps(payload),headers=headers)
if (rcv == left):
payload = {"jsonrpc": "2.0", "method": "Input.Left", "id": 1}
response = requests.post(xbmc_json_rpc_url, data=json.dumps(payload),headers=headers)
if (rcv == select):
payload = {"jsonrpc": "2.0", "method": "Input.Select", "id": 1}
response = requests.post(xbmc_json_rpc_url, data=json.dumps(payload),headers=headers)
if (rcv == back):
payload = {"jsonrpc": "2.0", "method": "Input.Back", "id": 1}
response = requests.post(xbmc_json_rpc_url, data=json.dumps(payload),headers=headers)
if (rcv == home):
payload = {"jsonrpc": "2.0", "method": "Input.Home", "id": 1}
response = requests.post(xbmc_json_rpc_url, data=json.dumps(payload),headers=headers)
if (rcv == shuthdown):
payload = {"jsonrpc": "2.0", "method": "System.Shutdown ", "id": 1}
response = requests.post(xbmc_json_rpc_url, data=json.dumps(payload),headers=headers)