-
-
Save furikake6000/8ef15d354cc5f1f4acabb5e26aba3d89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import json | |
import sys | |
host = "127.0.0.1" # 実際に通信を行う際は変更してください | |
port = 8810 # ポート番号は8810です | |
position = 0.0 | |
print("Welcome to Furi Robot ver1.0 system!") | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as mysock: | |
mysock.bind((host, port)) | |
mysock.listen(1) # 1は待ち受けキュー数です 通信する相手の数より多くする必要はありません | |
print("Waiting for client...") | |
while True: | |
# 通信を待つ | |
clientsock, clientaddr = mysock.accept() | |
print("Connection established! Addr: {}", clientaddr) | |
while True: | |
raw_data = clientsock.recv(1024) | |
# 受信したJSONを辞書型にしてdictに格納 | |
data = {} | |
try: | |
data = json.loads(raw_data) | |
except json.JSONDecodeError as e: | |
print("JSONのフォーマットが不正です。") | |
sys.exit() | |
except ValueError as e: | |
print("値が不正です。") | |
sys.exit() | |
# dictから速度要素を取得 | |
if 'velocity' in data: | |
try: | |
position += float(data['velocity']) | |
except ValueError as e: | |
print("値が不正です。") | |
sys.exit() | |
# 現在位置を出力 | |
print("現在のFuriRobotの座標: {}".format(position)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment