Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket.error
#1
hi, I tried to control raspberrypi with julius and python, but i got socket.error as follows.
Error:
Traceback (most recent call last): File "led-flashing-chess.py", line 106, in <module> main() File "led-flashing-chess.py", line 20, in main client.connect((host, port)) # サーバーモードで起動したjuliusに接続 File "/usr/lib/python2.7/socket.py", line 228, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 111] Connection refused
My code is here.
# -*- coding: utf-8 -*-

import socket
import xml.etree.ElementTree as ET
import subprocess
import time
import RPi.GPIO as GPIO

host = '192.168.11.51'   # localhost
port = 10500    # juliusサーバーモードのポート
GPIO.setmode(GPIO.BCM)

def main():

    p = subprocess.Popen(["sh julius-start.sh"], stdout=subprocess.PIPE, shell=True)
    # julius起動スクリプトを実行→juliusはサーバー側として待ち受ける
    pid = str(p.stdout.read().decode('utf-8'))     # juliusのプロセスIDを取得
    time.sleep(3)    # 3秒間スリープ
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((host, port))     # サーバーモードで起動したjuliusに接続

    try:
        data = ''   # dataの初期化
        killword = ''    # 前回認識した言葉を記憶するための変数
        while 1:
            # print(data)    # 認識した言葉を表示して確認
            if '</RECOGOUT>\n.' in data:

                root = ET.fromstring('<?xml version="1.0"?>\n' + data[data.find('<RECOGOUT>'):].replace('\n.', ''))
                for whypo in root.findall('./SHYPO/WHYPO'):

                    word = whypo.get('WORD')    # juliusで認識したWORDをwordに入れる
                    if word == u'ルーク':
                        # 18番ピンを出力モードで初期化する(モード指定前はどちらにもなりうる状態)
                        GPIO.setup(18, GPIO.OUT)
                        # LED点滅(2秒おき)を10回繰り返す
                        for x in range(5):
                            GPIO.output(18, True)   # ON
                            time.sleep(2)   # 2秒そのまま
                            GPIO.output(18, False)  # OFF
                            time.sleep(2)
                        GPIO.cleanup()
                        killword = ('ルーク')

                    elif word == u'ビショップ':
                        GPIO.setup(25, GPIO.OUT)
                        for x in range(5):
                            GPIO.output(25, True)
                            time.sleep(2)
                            GPIO.output(25, False)
                            time.sleep(2)
                        GPIO.cleanup()
                        killword = ('ビショップ')

                    elif word == u'クイーン':
                        GPIO.setup(16, GPIO.OUT)
                        for x in range(5):
                            GPIO.output(16, True)
                            time.sleep(2)
                            GPIO.output(16, False)
                            time.sleep(2)
                        GPIO.cleanup()
                        killword = ('クイーン')

                    elif word == u'ナイト':
                        GPIO.setup(18, GPIO.OUT)
                        for x in range(5):
                            GPIO.output(18, True)
                            time.sleep(2)
                            GPIO.output(18, False)
                            time.sleep(2)
                        GPIO.cleanup()
                        killword = ('ナイト')

                    elif word == u'ポーン':
                        GPIO.setup(25, GPIO.OUT)
                        for x in range(5):
                            GPIO.output(25, True)
                            time.sleep(2)
                            GPIO.output(25, False)
                            time.sleep(2)
                        GPIO.cleanup()
                        killword = ('ポーン')

                    elif word == u'キング':
                        GPIO.setup(16, GPIO.OUT)
                        for x in range(5):
                            GPIO.output(16, True)
                            time.sleep(2)
                            GPIO.output(16, False)
                            time.sleep(2)
                        GPIO.cleanup()
                        killword = ('キング')
                    print(word)     # wordを表示
                    data = ''   # dataの初期化

            else:
                data += str(client.recv(1024).decode('utf-8'))      # dataが空のときjuliusからdataに入れる
                print('NotFound')   # juliusに認識する言葉がない。認識していない。
    except KeyboardInterrupt:
        p.kill()
        subprocess.call(["kill " + pid], shell=True)    # juliusのプロセスを終了する。
        client.close()

if __name__ == "__main__":
    main()
How can i solve it??
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Raw Socket Error therealherby 1 3,747 Oct-24-2019, 06:33 AM
Last Post: markfilan
  socket programming ConnectionRefusedError error srm 3 12,799 May-16-2019, 08:07 PM
Last Post: LavaCreeperKing
  Python socket : Error receive data quanglnh1993 1 12,953 Mar-14-2018, 11:59 AM
Last Post: avorane

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020