[PYTHON] Remote terminal RiT

Introduction

Introducing a remote terminal using UDP communication of MH Software & Service. Some factory control devices and image sensors support UDP output. The signals output from these devices can be monitored remotely.

What is UDP

Abbreviation for User Datagram Protocol, from Wikipedia

** Unlike TCP, UDP is connectionless and is a protocol that keeps sending data without waiting for a response from the other party. ** **

What is TCP

Abbreviation for Transmission Control Protocol. Connect In other words, if there is no partner, an error will occur.

Benefits of UDP

It ’s just my feeling,

  1. Overall communication time is shorter than TCP
  2. Error handling can be simplified because it can be sent even if there is no other party.
  3. Because it can be simplified, you can easily send and receive even with a smartphone.

Remote terminal RiT

Created using Python. Since it is Python, it works on Windows PCs and Android smartphones. Of course, I think it will work in Mac and iPhone environments, but I don't have any equipment, so I haven't confirmed it yet. The GUI is tkinter.

Windows Python 3.8.6 windows.jpg

Android Pydroid 3 4.01_arm64 android.jpg

In Pydroid 3, the icon does not appear. I can't use Japanese for the title. There may be a setting, but I'm not sure. I would appreciate it if you could give me some advice.

Raspberry Pi 4 and Kuman K82

K82.JPG

Introducing the Thermal Camera (Thermo AI Device TiD)](https://qiita.com/MH-SS/items/c3d12abe871bfd62d1df), just put the Raspberry Pi in the Physical Computing Lab / Raspberry Pi4 Model B DIY metal case and insert the Kuman K82. , It is a simple structure.

Root class

Form initialization

class Root(mhtk.Form):
    def __init__(self):
        self.__version__ = __version__

        self.root = self
        self.ini = Ini(fullpath=__file__)
        self.LANG = 'ja'

        self.id_name = self.ini.udp.id_name
        self.udp_port = self.ini.udp.port
        self.destination = self.ini.udp.destination

        minsize = (400, 180)
        super().__init__(
            dialog=True,
            font=('system'),
            icon=self.icon_file,
            minsize=minsize,
            name='main',
            position=self.ini.get_geometry(
                self.ini,
                self.ini.config,
                minsize),
            title=(lambda: 'Remote Terminal'
                   if self.LANG == 'en' else 'Remote Terminal')(),
            udp_port=self.udp_port,
            )

        # NOTE: Output Bit. This value is binary.
        self.output = tk.IntVar(value=0)
        self.output.trace('w', self.output_changed)

        self.create_io()

        self.menu = Menu(self)

        self.indicator = Indicator(self)
        self.indicator.pack(anchor=tk.CENTER)

        # NOTE: Logo
        self.logo = mhtk.IconFramePack(self)
        self.logo.pack(anchor=tk.CENTER)

Specifying minsize, the Form class (form.py) introduced in Thermal Camera (Thermo AI Device TiD) Python Form Edition is used.

I/O In create_io, I/O is created from the class for GPIO (mhrp.gpio).

    def create_io(self):
        """
        for K82.
        CH1 GPIO19 PIN35
        CH2 GPIO26 PIN37
        CH3 GPIO20 PIN38
        CH4 GPIO21 PIN40
        ================
        self.force_* value is
            -1: Not forced.
             0: Forced off.
             1: Forced on.
        """
        #self.input_list = [29, 31, 33, 35, 37]
        #self.output_list = [16, 18, 32, 36, 38, 40]

        self.input_list = [11, 13, 16, 18]
        self.force_input = []
        for _ in range(len(self.input_list)):
            self.force_input.append(-1)

        self.output_list = self.ini.config.output_io_list
        self.force_output = []
        for _ in range(len(self.output_list)):
            self.force_output.append(-1)

        self.io = mhrp.gpio.IO(
            'PIN',
            self.input_list,
            self.output_list,
            mhrp.gpio.BOTH,
            )
        self.io.input.bin.trace('w', self.io_callback)

mhrp.gpio -> input/outpu.py -> bit.py mhrp is a library for ** MH ** Software & Services ** R ** aspberry ** P ** i. Within this, each class is instantiated from input/output.py. In addition, input/output.pu instantiates the Bit class of bit.py.

bit.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import tkinter as tk


class Bit(list):
    def __init__(self, count, callback):
        self._count = count
        self._callback = callback
        for bit in range(count):
            self.append(tk.BooleanVar(value=False))
            self[bit].trace('w', self.BitChanged(bit, self._bit_changed))

    def _bit_changed(self, *args):
        self._callback(*args)

    class BitChanged():
        def __init__(self, index, callback):
            self._index = index
            self._callback = callback

        def __call__(self, *args):
            self._callback(self._index)

The Bit class inherits from list. Instantiate and add BitChanged to list as many as the number of inputs and outputs. Each BitChanged class has its own index. When it is switched on/off, the callback function is executed.

tkinter trace strongest!

The Python GUI in MH Software & Services is primarily tkinter. There is a .trace that can execute a function when a variable changes.

In bit.py

self[bit].trace('w', self.BitChanged(bit, self._bit_changed))

there is.

self._bit_changed is the part of self.BitChanged (bit, self._bit_changed) that is instantiated and has a trace attribute.

This means that self._bit_changed will be executed when the bit is changed. self._bit_changed is a BitChanged class, and you can execute an instance like a function by call.

class BitChanged():
    def __init__(self, index, callback):
        self._index = index
        self._callback = callback

    def __call__(self, *args): #Here is executed
        self._callback(self._index)

It's a simple class, but it's like remembering your bit and callback function when instantiating and returning the bit to input/output.py when the bit changes.

In addition, trace can also be a character variable.


YouTube: Remote Terminal RiT web: RiT Remote Terminal (URL is subject to change)

Recommended Posts

Remote terminal RiT