Разбираюсь с глобальными переменными, но в loop_a гллобальная переменная не поподает.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from socket import *
import RPi.GPIO as GPIO
import time
import requests
import serial
from multiprocessing import Process
#данные сервера
host = 'localhost'
port = 9999
addr = (host,port)
ser = serial.Serial('/dev/ttyUSB1',9600)
ch = ser.readline()
mag = int(ch.encode('hex'),16)/100000-30
#socket - функция создания сокета
#первый параметр socket_family может быть AF_INET или AF_UNIX
#второй параметр socket_type может быть SOCK_STREAM(для TCP) или SOCK_DGRAM(для UDP)
tcp_socket = socket(AF_INET, SOCK_STREAM)
#bind - связывает адрес и порт с сокетом
tcp_socket.bind(addr)
#listen - запускает прием TCP
tcp_socket.listen(2)
def loop_a():
#Бесконечный цикл работы программы
while True:
GPIO.setmode(GPIO.BOARD)
GPIO.setup(33,GPIO.OUT)
GPIO.setup(35,GPIO.OUT)
GPIO.setup(37,GPIO.OUT)
GPIO.output(33,GPIO.HIGH)
GPIO.output(35,GPIO.HIGH)
GPIO.output(37,GPIO.HIGH)
#accept - принимает запрос и устанавливает соединение, (по умолчанию работает в блокирующем режиме)
#устанавливает новый сокет соединения в переменную conn и адрес клиента в переменную addr
conn, addr = tcp_socket.accept()
#recv - получает сообщение TCP
data = conn.recv(1024)
#если ничего не прислали, завершим программу
if not data:
conn.close()
break
else:
print(data)
conn.send(data.upper())
opcl, prop = data.split(" ", 2)
print(opcl)
print(prop)
if int(opcl) < 8:
if (mag == 6):
ex = 4
elif (mag == 4):
ex = 3
else:
ex = 6
var = 0
if int(opcl) < 8:
while var < 50:
print(ex)
print(mag)
GPIO.output(33,GPIO.LOW)
GPIO.output(35,GPIO.LOW)
time.sleep(0.1)
if (ex == mag):
GPIO.output(33,GPIO.HIGH)
GPIO.output(35,GPIO.HIGH)
GPIO.cleanup()
link = "http://localhost/vd_mg.php?pr="+prop
f = requests.get(link)
print(f.text)
break;
var = var + 1
else:
GPIO.output(33,GPIO.HIGH)
GPIO.output(35,GPIO.HIGH)
GPIO.output(37,GPIO.HIGH)
GPIO.cleanup()
else:
if (mag < 4):
ex = 4
elif (mag > 5):
ex = 3
else:
ex = 6
var = 0
while var < 50:
print(ex)
print(mag)
GPIO.output(33,GPIO.LOW)
GPIO.output(37,GPIO.LOW)
time.sleep(0.1)
if (ex == mag):
GPIO.output(33,GPIO.HIGH)
GPIO.output(37,GPIO.HIGH)
GPIO.cleanup()
link = "http://localhost/vd_mg.php?pr="+prop
f = requests.get(link)
print(f.text)
break;
var = var + 1
else:
GPIO.output(33,GPIO.HIGH)
GPIO.output(35,GPIO.HIGH)
GPIO.output(37,GPIO.HIGH)
GPIO.cleanup()
conn.close()
def loop_b():
while True:
global mag
ch = ser.readline()
mag = int(ch.encode('HEX'),16)/100000-30
if __name__ == '__main__':
Process(target=loop_a).start()
Process(target=loop_b).start()