Giter VIP home page Giter VIP logo

pysemisecs's Introduction

pysemisecs

Introduction

This package is SEMI-SECS-communicate implementation on Python3.

Supports

  • SECS-I (SEMI-E4)
  • SECS-II (SEMI-E5)
  • GEM (SEMI-E30, partially)
  • HSMS-SS (SEMI-E37.1)
  • SML (PEER Group)

Setup

  • Most simple way, Download secs.py file

    import secs
    or append codes in if __name__ == '__main__':

    If use Secs1OnPySerialCommunicator, must install pySerial

  • pip install

  $ pip install git+https://github.com/kenta-shimizu/pysemisecs

Notes: this pip also installs pySerial.

Create Communicator instance and open

  • For use HSMS-SS-Passive example
    passive = secs.HsmsSsPassiveCommunicator(
        ip_address='127.0.0.1',
        port=5000,
        session_id=10,
        is_equip=True,
        timeout_t3=45.0,
        timeout_t6=5.0,
        timeout_t7=10.0,
        timeout_t8=5.0,
        gem_mdln='MDLN-A',
        gem_softrev='000001',
        gem_clock_type=secs.ClockType.A16,
        name='equip-passive-comm')

    passive.open()
  • For use HSMS-SS-Active example
    active = secs.HsmsSsActiveCommunicator(
        ip_address='127.0.0.1',
        port=5000,
        session_id=10,
        is_equip=False,
        timeout_t3=45.0,
        timeout_t5=10.0,
        timeout_t6=5.0,
        timeout_t8=5.0,
        gem_clock_type=secs.ClockType.A16,
        name='host-acitve-comm')

    active.open()
  • For use SECS-I-on-pySerial

    For use, must install pySerial

    secs1p = secs.Secs1OnPySerialCommunicator(
        port='/dev/ttyUSB0',
        baudrate=9600,
        device_id=10,
        is_equip=True,
        is_master=True,
        timeout_t1=1.0,
        timeout_t2=15.0,
        timeout_t3=45.0,
        timeout_t4=45.0,
        gem_mdln='MDLN-A',
        gem_softrev='000001',
        gem_clock_type=secs.ClockType.A16,
        name='equip-master-comm')
    
    secs1p.open()
  • For use SECS-I-on-TCP/IP
    # This is connect/client type connection.
    # This and 'Secs1OnTcpIpReceiverCommunicator' are a pair.
   
    secs1c = secs.Secs1OnTcpIpCommunicator(
        ip_address='127.0.0.1',
        port=23000,
        device_id=10,
        is_equip=True,
        is_master=True,
        timeout_t1=1.0,
        timeout_t2=15.0,
        timeout_t3=45.0,
        timeout_t4=45.0,
        gem_mdln='MDLN-A',
        gem_softrev='000001',
        gem_clock_type=secs.ClockType.A16,
        name='equip-master-comm')

    secs1c.open()
  • For use SECS-I-on-TCP/IP-Receiver
    # This is bind/server type connection.
    # This and 'Secs1OnTcpIpCommunicator' are a pair.

    secs1r = secs.Secs1OnTcpIpReceiverCommunicator(
        ip_address='127.0.0.1',
        port=23000,
        device_id=10,
        is_equip=False,
        is_master=False,
        timeout_t1=1.0,
        timeout_t2=15.0,
        timeout_t3=45.0,
        timeout_t4=45.0,
        gem_clock_type=secs.ClockType.A16,
        name='host-slave-comm')

    secs1r.open()

Notes: To shutdown communicator, .close() or use a with statement.

Send Primary-Message and receive Reply-Message

    # Send
    # S5F1 W
    # <L
    #   <B 0x81>
    #   <U2 1001>
    #   <A "ON FIRE">
    # >.

    reply_msg = passive.send(
        strm=5,
        func=1,
        wbit=True,
        secs2body=('L', [
            ('B' , [0x81]),
            ('U2', [1001]),
            ('A' , "ON FIRE")
        ])
    )

.send() is blocking-method.
Blocking until Reply-Message received.
Reply-Message has value if W-Bit is True, otherwise None.
If T3-Timeout, raise SecsWaitReplyMessageError.

Received Primary-Message, parse, and send Reply-Message

  1. Add listener to receive Primary-Message
    def recv_primary_msg(primary_msg, comm):
        # something...

    active.add_recv_primary_msg_listener(recv_primary_msg)
  1. Parse Message
    # Receive
    # S5F1 W
    # <L
    #   <B 0x81>
    #   <U2 1001>
    #   <A "ON FIRE">
    # >.

    >>> primary_msg.strm
    5
    >>> primary_msg.func
    1
    >>> primary_msg.wbit
    True
    >>> primary_msg.secs2body.type
    L
    >>> primary_msg.secs2body[0].type
    B
    >>> primary_msg.secs2body[0].value
    b'\x81'
    >>> primary_msg.secs2body.get_value(0)
    b'\x81'
    >>> primary_msg.secs2body[0][0]
    129
    >>> primary_msg.secs2body.get_value(0, 0)
    129
    >>> primary_msg.secs2body[1].type
    U2
    >>> primary_msg.secs2body[1].value
    (1001,)
    >>> primary_msg.secs2body[1][0]
    1001
    >>> primary_msg.secs2body.get_value(1, 0)
    1001
    >>> primary_msg.secs2body[2].type
    A
    >>> primary_msg.secs2body[2].value
    ON FIRE
    >>> primary_msg.secs2body.get_value(2)
    ON FIRE
    >>> len(primary_msg.secs2body)
    3
    >>> [v.value for v in primary_msg.secs2body]
    [b'\x81', (1001,), 'ON FIRE']
  1. Send Reply-Message
    # Reply S5F2 <B 0x0>.

    comm.reply(
        primary=primary_msg,
        strm=5,
        func=2,
        wbit=False,
        secs2body=('B', [0x0])
    )

Detect Communicatable-state changed

  1. Add listener
    def _comm_listener(communicatable, comm):
        if communicatable:
            print('communicated')
        else:
            print('discommunicated')
    
    passive.add_communicate_listener(_comm_listener)

SML

  • Send Primary-Message
    reply_msg = active.send_sml('S1F1 W.')
  • Send Reply-Message
    passive.reply_sml(
        primary_msg,
        'S1F2          ' +
        '<L            ' +
        '  <A "MDLN-A">' +
        '  <A "000001">' +
        '>.            '
    )

Notes: Don't forget a period(.) of ends message.

GEM

Access from .gem property.

Clock

  • Send S2F17 and receive reply, parse to datetime
    clock = passive.gem.s2f17()
    dt = clock.to_datetime()
  • Reply S2F18 Now examples
    active.gem.s2f18_now(primary_s2f17_msg)
    active.gem.s2f18(primary_s2f17_msg, secs.Clock.now())
    active.gem.s2f18(primary_s2f17_msg, secs.Clock(datetime.datetime.now()))
  • Send S2F31 Now examples
    tiack = active.gem.s2f31_now()
    tiack = active.gem.s2f31(secs.Clock.now())
    tiack = active.gem.s2f31(secs.Clock(datetime.datetime.now()))
  • Receive S2F31, parse to datetime, reply S2F32
    clock = secs.Clock.from_ascii(primary_s2f31_msg.secs2body)
    dt = clock.to_datetime()
    passive.gem.s2f32(primary_s2f31_msg, secs.TIACK.OK)

TimeFormat (A[12] or A[16]) can be set from .clock_type property

    passive.gem.clock_type = secs.ClockType.A12
    passive.gem.clock_type = secs.ClockType.A16

Others

    commack = active.gem.s1f13()
    oflack  = active.gem.s1f15()
    onlack  = active.gem.s1f17()

    passive.gem.s1f14(primary_msg, secs.COMMACK.OK)
    passive.gem.s1f16(primary_msg)
    passive.gem.s1f18(primary_msg, secs.ONLACK.OK)

    passive.gem.s9f1(ref_msg)
    passive.gem.s9f3(ref_msg)
    passive.gem.s9f5(ref_msg)
    passive.gem.s9f7(ref_msg)
    passive.gem.s9f9(ref_msg)
    passive.gem.s9f11(ref_msg)

pysemisecs's People

Contributors

kenta-shimizu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pysemisecs's Issues

active.open_and_wait_until_communicating return False

 active.open()
r = active.open_and_wait_until_communicating(5)
active.add_recv_all_msg_listener(self._recv_all_msg_listener)
# self._send_hand_shake(active)
if not r:
    get_logger().warn('connect open_and_wait_until_communicating return false')

The method open_and_wait_until_communicating returns false. What is the general reason for this

CallbackQueuing invalid syntax

This is my first time entering an issue on GitHub, so please excuse my lack of experience. I tried to use the secs.py code in the simple directory. On line 2362 I get an invalid syntax for CallbackQueuing(self._rpm_cb) as pmq. PyCharm is suggesting that CallbackQueuing doesn't specify the return value of the CallbackQueuing init method?

trying to create simulator using this package, which should run in two different window

Hi, @kenta-shimizu

First of all thank you for creating this project. I am trying to create a very simple simulator using tkinter and your package. I have written this code, but I want to know what should be change so that it should be send and reply message?

I know it should reply to the message but I am not figure out in the code how should it would reply.

raise HsmsSsTimeoutT3Error("HsmsSs-Timeout-T3", msg)
secs.hsmssscommunicator.HsmsSsTimeoutT3Error: HsmsSsTimeoutT3Error('HsmsSs-Timeout-T3',[00 0A|85 01|00 00|00 00 00 02])
import tkinter as tk
from tkinter import ttk
import threading
import secs

class CommunicationApp:
    def __init__(self, root):
        self.root = root
        root.title("Communication App")

        self.active_mode = tk.IntVar()
        self.active_mode.set(1)  # Default to Active mode

        self.device_active = None
        self.device_passive = None

        self.connect_button = ttk.Button(root, text="Connect", command=self.connect)
        self.disconnect_button = ttk.Button(root, text="Disconnect", command=self.disconnect)
        self.action_button = ttk.Button(root, text="Send/Reply", command=self.send_or_reply)

        self.connect_button.grid(row=0, column=0, padx=10, pady=10)
        self.disconnect_button.grid(row=0, column=1, padx=10, pady=10)
        self.action_button.grid(row=0, column=2, padx=10, pady=10)

        self.active_radio = ttk.Radiobutton(root, text="Active", variable=self.active_mode, value=1)
        self.passive_radio = ttk.Radiobutton(root, text="Passive", variable=self.active_mode, value=0)

        self.active_radio.grid(row=1, column=0, padx=10, pady=10)
        self.passive_radio.grid(row=1, column=1, padx=10, pady=10)

        self.output_text = tk.Text(root, height=10, width=40)
        self.output_text.grid(row=2, columnspan=3, padx=10, pady=10)

        self.receive_text = tk.Text(root, height=10, width=40)
        self.receive_text.grid(row=3, columnspan=3, padx=10, pady=10)

        # Create variables to store the received Primary-Message and active device
        self.received_primary_msg = None
        self.active_device = None

    def connect(self):
        if self.active_mode.get() == 1:
            self.device_active = secs.HsmsSsActiveCommunicator(
                ip_address="127.0.0.1",
                port=5000,
                session_id=10,
                is_equip=False,
                timeout_t3=45,
                timeout_t6=5,
                timeout_t7=10,
                timeout_t8=5,
                gem_mdln="MDLN-A",
                gem_softrev="000001",
                gem_clock_type=secs.ClockType.A16,
                name="hsms-active",
            )
            self.device_active.open()
            self.active_device = self.device_active
            self.active_device.add_recv_primary_msg_listener(self.recv_primary_msg)
        else:
            self.device_passive = secs.HsmsSsPassiveCommunicator(
                ip_address="127.0.0.1",
                port=5000,
                session_id=10,
                is_equip=True,
                timeout_t3=45,
                timeout_t6=5,
                timeout_t7=10,
                timeout_t8=5,
                gem_mdln="MDLN-A",
                gem_softrev="000001",
                gem_clock_type=secs.ClockType.A16,
                name="hsms-passive",
            )
            self.device_passive.open()
            self.active_device = self.device_passive
            self.active_device.add_communicate_listener(self._comm_listener)

        self.output_text.insert(tk.END, "Connected\n")

    def disconnect(self):
        if self.active_device:
            self.active_device.close()
            self.active_device = None

        self.output_text.insert(tk.END, "Disconnected\n")

    def send_or_reply(self):
        if self.active_mode.get() == 1:
            # Active mode: Send
            threading.Thread(target=self.send_primary_message).start()
        else:
            # Passive mode: Reply
            threading.Thread(target=self.send_reply_message).start()

    def send_primary_message(self):
        if self.active_device:
            # Send a Primary-Message
            primary_msg = self.active_device.send(
                strm=5,
                func=1,
                wbit=True,
                secs2body=("L", [("B", [0x81]), ("U2", [1001]), ("A", "ON FIRE")]),
            )

            # Store the Primary-Message
            self.received_primary_msg = primary_msg

            # Display the sent message in the output_text widget
            self.output_text.insert(tk.END, "Sent Primary-Message\n")

    def send_reply_message(self):
        if self.active_device and self.received_primary_msg:
            # Send a Reply-Message
            reply_msg = self.active_device.reply(
                primary=self.received_primary_msg,
                strm=5,
                func=2,
                wbit=False,
                secs2body=("B", [0x0]),
            )

            # Display the sent Reply-Message in the output_text widget
            self.output_text.insert(tk.END, "Sent Reply-Message\n")

    def recv_primary_msg(self, primary_msg, comm):
        # Handle received Primary-Message
        self.receive_text.insert(tk.END, f"Received Primary-Message: {primary_msg}\n")

    def _comm_listener(self, communicatable, comm):
        if communicatable:
            self.output_text.insert(tk.END, "Communicated\n")
        else:
            self.output_text.insert(tk.END, "Discommunicated\n")

if __name__ == "__main__":
    root = tk.Tk()
    app = CommunicationApp(root)
    root.mainloop()

Try to send unformatted data along with 'A' format tag

Dear, thank you for your incredible work. This library worked fine until I got trying to send unformatted data which are not only ASCII characters, but also the possible char between 0..255.
The following is the result from the device that received the message S18F7.

[Equipment] received primary message from:
{
"baudrate": 9600,
"device_id": 0,
"is_equip": true,
"is_master": true,
"name": "equip-master",
"port": "/dev/pts/12",
"protocol": "SECS-I-on-pySerial"
}
[Equipment] primary message:
[00 00|92 07|00 00|00 00 00 01]
S18F7 W
<L [4]
<A [2] "01" >
<A [3] "S01" >
<U4 [1] 8 >
<A [11] "b'UUUUUUUU'" > # <-- Unformatted data

.

You might see that the data are sent as a sequence of code, not the byte array.
Thus, my question is:

  1. Does it conform to the standard if sending the "unformatted data" message that is not pure ASCII ?
  2. How could we send the sequence of bytes via SECS protocol ?

How do I query a single-data body instead of an array

body =  ('U2', [1010])
response = active.send(1, 3, True,   body)

I need to send is single-data body rather than an array. I tried this, but it seemed wrong.

Traceback (most recent call last):
  File xxxxxxxxxxxxx/secs/hsmssscommunicator.py", line xx, in send
    raise HsmsSsTimeoutT3Error("HsmsSs-Timeout-T3", msg)
secs.hsmssscommunicator.HsmsSsTimeoutT3Error: HsmsSsTimeoutT3Error('HsmsSs-Timeout-T3',[00 00|81 03|00 00|00 00 00 04])

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.