Skip to main content

Gurux serial media is used to commmunication with serial port connections.

Project description

See An Gurux for an overview.

Join the Gurux Community or follow @Gurux for project updates.

With gurux.serial component you can send data easily syncronously or asyncronously using serial port connection.

Open Source GXNet media component, made by Gurux Ltd, is a part of GXMedias set of media components, which programming interfaces help you implement communication by chosen connection type. Gurux media components also support the following connection types: serial port.

For more info check out Gurux.

We are updating documentation on Gurux web page.

If you have problems you can ask your questions in Gurux Forum.

You can get source codes from http://www.github.com/gurux or intall package:

pip install gurux-common
pip install gurux-serial

Simple example

Before use you must set following settings:

  • PortName
  • BaudRate
  • DataBits
  • Parity
  • StopBits

It is also good to add listener to listen following events.

  • onError
  • onReceived
  • onMediaStateChange
  • onTrace
  • onPropertyChanged

This example sends data to the serial port and waits reply. Change serial port before use.

import time
from gurux_common import ReceiveParameters
from gurux_common import IGXMediaListener
from gurux_common.enums.TraceLevel import TraceLevel
from gurux_serial import GXSerial

#pylint: disable=no-self-argument
class sampleclient(IGXMediaListener):

    def __init__(self):
        #List available serial ports.
        print("Available ports:")
        print(str(GXSerial.getPortNames()))
        #Define End Of Packet char.
        eop = '\r'
        #TODO: Update correct port and serial port settings.
        media = GXSerial("SERIAL PORT TO USE")
        #Start to listen events from the media.
        media.addListener(self)
        #Show all traces.
        media.trace = TraceLevel.VERBOSE
        #Set EOP for the media.
        media.eop = eop
        try:
            #Open the connection.
            media.open()
            r = ReceiveParameters()
            r.eop = eop
            #Minimium amount of bytes to receive.
            r.count = 5
            #Wait reply for 2 seconds.
            r.waitTime = 2000
            ############################
            #Send data synchronously.
            with media.getSynchronous():
                media.send("Hello world!")
                #Send EOP
                media.send('\r')
                ret = media.receive(r)
                if ret:
                    print(str(r.reply.decode("ascii")))
                else:
                    raise Exception("Failed to receive reply from the server.")
            ############################
            #Send async data.
            media.send("Notify from the meter!\r")
            #Wait 1 second to receive reply from the server.
            time.sleep(1)
        except Exception as ex:
            print(ex)
        media.close()
        media.removeListener(self)

    def onError(self, sender, ex):
        """
        Represents the method that will handle the error event of a Gurux
        component.

        sender :  The source of the event.
        ex : An Exception object that contains the event data.
        """
        print("Error has occured. " + str(ex))

    def onReceived(self, sender, e):
        """Media component sends received data through this method.

        sender : The source of the event.
        e : Event arguments.
        """
        print("New data is received. " + str(e))

    def onMediaStateChange(self, sender, e):
        """Media component sends notification, when its state changes.
        sender : The source of the event.
        e : Event arguments.
        """
        print("Media state changed. " + str(e))

    def onTrace(self, sender, e):
        """Called when the Media is sending or receiving data.

        sender : The source of the event.
        e : Event arguments.
        """
        print("trace:" + str(e))

    def onPropertyChanged(self, sender, e):
        """
        Event is raised when a property is changed on a component.

        sender : The source of the event.
        e : Event arguments.
        """
        print("Property {!r} has hanged.".format(str(e)))

if __name__ == '__main__':
    sampleclient()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gurux_serial-1.0.20.tar.gz (24.5 kB view details)

Uploaded Source

Built Distribution

gurux_serial-1.0.20-py3-none-any.whl (29.7 kB view details)

Uploaded Python 3

File details

Details for the file gurux_serial-1.0.20.tar.gz.

File metadata

  • Download URL: gurux_serial-1.0.20.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.13

File hashes

Hashes for gurux_serial-1.0.20.tar.gz
Algorithm Hash digest
SHA256 7d4a575f6b3db942c183d92eabe954c35c45105dd639c34cdba0a6d441e36c85
MD5 2ce6cc7e2152840dea2cbe6e7732cd34
BLAKE2b-256 34e07367edf616b3f0265e5929e998a709739099694779288c9196ec7ac4b96f

See more details on using hashes here.

File details

Details for the file gurux_serial-1.0.20-py3-none-any.whl.

File metadata

  • Download URL: gurux_serial-1.0.20-py3-none-any.whl
  • Upload date:
  • Size: 29.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.13

File hashes

Hashes for gurux_serial-1.0.20-py3-none-any.whl
Algorithm Hash digest
SHA256 16a1e88d368e871fc2aa12471fad1da5a79906ff84644e957ce1225411dad1c5
MD5 372ef2cdcc7eb282c81e9cd9c460eea0
BLAKE2b-256 032df6dc16c7ffe581c5e77da7333e27263b3ab2b19d35192c19e64a18b1a6a5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page