Commit 5812907a authored by Oleg Nikulin's avatar Oleg Nikulin

Переделана логика обмена (пк=мастер, коды команд); Rewrite скрипта и прошивки

parent fd983901
import serial
import time
def open_serial(port, baudrate):
try: #пытаемся открыть serial порт
serial_port = serial.Serial(
port = port,
baudrate = baudrate,
bytesize = 8,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE
)
except OSError:
return None
print('Delay before starting serial...')
time.sleep(2)
serial_port.reset_input_buffer()
serial_port.reset_output_buffer()
return serial_port
def serial_exchange(serial_port, transmit_bytes, required_response_length, timeout = 1):
if serial_port is None:
raise OSError
serial_port.reset_input_buffer()
serial_port.reset_output_buffer()
serial_port.write(bytearray(transmit_bytes))
if required_response_length == 0:
return bytes([])
#ждем ответ
steps = 10
for i in range(steps):
if serial_port.in_waiting >= required_response_length: break
time.sleep(timeout / steps)
if serial_port.in_waiting < required_response_length:
return None
response = serial_port.read(required_response_length)
return response
{
"device": "/dev/ttyUSB0",
"baudrate": 19200,
"pollIntervalSec": 2,
"arduinoTimeoutSec": 0.25,
"controlGroups":
[
{
"targetTemperature": 35,
"copyFromGroupIndex": 255
},
{
"targetTemperature": 40,
"copyFromGroupIndex": 255
},
{
"targetTemperature": 45,
"copyFromGroupIndex": 255
}
]
}
\ No newline at end of file
import subprocess
import time
import serial
import argparse
from argparse import RawTextHelpFormatter
import datetime
import threading
from threading import Event
import json
class pc_command_codes:
initialize = 1
poll = 2
class arduino_response_codes:
initialize_ok = 1
poll_noinit = 2
poll_ok = 3
fail = 255
POLL_SLEEP_TIME = 0.1 #Задержка между проверками наличия запроса от ардуины (cек)
RECONNECT_SLEEP_TIME = 1 #Задержка между попытками открыть serial порт при потере связи (cек)
TIMEOUT_TIME = 3 #Считается что ардуина перестала отвечать, если от нее не было запросов в течение этого времени (cек)
config = None
def load_json(filename):
try:
jsonFile = open(filename, mode='r', encoding='utf-8')
except:
print(f'Failed to open {filename}')
return None
try:
config = json.load(jsonFile)
except:
print(f'Failed to read json from {filename}')
return None
jsonFile.close()
return config
def cmdline_args_parse():
parser = argparse.ArgumentParser(
description = 'This script sends PC disk temperature via a serial port',
formatter_class=RawTextHelpFormatter
)
parser.add_argument('-m', '--manual',
required = False,
action='store_true',
help = 'send temperature from user input instead of hddtemp',
)
parser.add_argument('device',
type = str,
nargs = '?',
help = 'serial device to commuicate with. Example: /dev/ttyUSB0',
metavar = 'SERIAL'
)
parser.add_argument('speed',
type = int,
nargs = '?',
help = 'serial port baud rate',
metavar = 'SPEED'
)
args = parser.parse_args()
return args
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment