Package: sacn
sACN / E1.31 moduleThis module is a simple sACN library that support the standard DMX message of the protocol.
It is based on the2016version of the official ANSI E1.31 standard.
It has support for sending out DMX data and receiving it. Multiple and multicast universes are supported.
For full blown DMX support useOLA.Currently missing features:discovery messages (receiving)E1.31 sync feature (on the receiver side)custom ports (because this is not recommended)Features:out-of-order packet detection like theE1.316.7.2multicastauto flow control (seeThe Internals/Sending)E1.31 sync feature (see manual_flush)SetupThis Package is in thepypi. To install the package usepip install sacn. Python 3.6 or newer required!
To use the libraryimport sacn.
If you want to install directly from source, download or clone the repository and executepip install .where the setup.py is located.
For more information on pip installation see:https://packaging.python.org/tutorials/installing-packages/#installing-from-a-local-src-treeThe InternalsSendingYou can create a newsACNsenderobject and provide the necessary information. Then you have to usestart().
This creates a new thread that is responsible for sending out the data. Do not forget tostop()the thread when
finished! If the data is not changed, the same DMX data is sent out every second.The thread sends out the data every1/fpsseconds. This reduces network traffic even if you give the sender new data
more often than thefps.
A simple description would be to say that the data that you give the sACNsender is subsampled by thefps.
You can tweak thisfpsby simply change it when creating thesACNsenderobject.This function works according to theE1.31. See 6.6.1 for more information.Note: Since Version 1.4 there is a manual flush feature available. See Usage/Sending for more info.
This feature also uses the sync feature of the sACN protocol (see page 36 onE1.31).
Currently this is not implemented like the recommended way (this does not wait before sending out the sync packet), but
it should work on a normal local network without too many latency differences.
When theflush()function is called, all data is send out at the same time and immediately a sync packet is send out.ReceivingA very simple solution, as you just create asACNreceiverobject and usestart()a new thread that is running in
the background and calls the callbacks when new sACN data arrives.UsageSendingTo use the sending functionality you have to use thesACNsender.importsacnimporttimesender=sacn.sACNsender()# provide an IP-Address to bind to if you want to send multicast packets from a specific interfacesender.start()# start the sending threadsender.activate_output(1)# start sending out data in the 1st universesender[1].multicast=True# set multicast to True# sender[1].destination = "192.168.1.20"  # or provide unicast information.# Keep in mind that if multicast is on, unicast is not usedsender[1].dmx_data=(1,2,3,4)# some test DMX datatime.sleep(10)# send the data for 10 secondssender.stop()# do not forget to stop the senderYou can activate an output universe viaactivate_output(<universe>)and then change the attributes of this universe
viasender[<universe>].<attribute>. To deactivate an output usedeactivate_output(<universe>). The output is
terminated like theE1.31describes it on page 14.If you want to flush manually and the sender thread should not send out automatic, use thesACNsender.manual_flushoption. This is useful when you want to use a fixture that is using more than one universe
and all the data on multiple universes should send out at the same time.Tip: you can get the activated outputs withget_active_outputs()and you can move an output with all its settings
from one universe to another withmove_universe(<from>, <to>).Available Attributes forsender[<universe>].<attribute>are:destination: str: the unicast destination as string. (eg "192.168.1.150") Default: "127.0.0.1"multicast: bool: set whether to send out via multicast or not. Default: False
If True the data is send out via multicast and not unicast.ttl: int: the time-to-live for the packets that are send out via multicast on this universe. Default: 8priority: int: (must be between 0-200) the priority for this universe that is send out. If multiple sources in a
network are sending to the same receiver the data with the highest priority wins. Default: 100preview_data: bool: Flag to mark the data as preview data for visualization purposes. Default: Falsedmx_data: tuple: the DMX data as a tuple. Max length is 512 and for legacy devices all data that is smaller than
512 is merged to a 512 length tuple with 0 as filler value. The values in the tuple have to be [0-255]!sACNsenderCreates a sender object. A sender is used to manage multiple sACN universes and handles their output.
DMX data is send out every second, when no data changes. Some changes may be not send out, because the fps
setting defines how often packets are send out to prevent network overuse. So if you change the DMX values too
often in a second they may not all been send. Vary the fps parameter to your needs (Default=30).bind_address: str: the IP-Address to bind to.
Provide an IP-Address to bind to if you want to send multicast packets from a specific interface.bind_port: int: optionally bind to a specific port. Default=5568. It is not recommended to change the port.
Change the port number if you have trouble with another program or the sACNreceiver blocking the portsource_name: str: the source name used in the sACN packets. See thestandardfor more information.cid: tuple: the cid. If not given, a random CID will be generated. See thestandardfor more information.fps: intthe frames per second. See explanation above. Has to be >0. Default: 30universeDiscovery: boolif true, universe discovery messages are send out via broadcast every 10s. Default: Truesync_universe: intset a specific universe used in the sync-packets. Default: 63999When manually flushed, the E1.31 sync feature is used. So all universe data is send out, and after all data was send out
a sync packet is send to all receivers and then they are allowed to display the received data. Note that not all
receiver implemented this feature of the sACN protocol.Example for the usage of the manual_flush:importsacnimporttimesender=sacn.sACNsender()sender.start()sender.activate_output(1)sender.activate_output(2)sender[1].multicast=Truesender[2].multicast=Truesender.manual_flush=True# turning off the automatic sending of packetssender[1].dmx_data=(1,2,3,4)# some test DMX datasender[2].dmx_data=(5,6,7,8)# by the time we are here, the above data would be already send out,# if manual_flush would be False. This could cause some jitter# so instead we are flushing manualtime.sleep(1)# let the sender initialize itselfsender.flush()sender.manual_flush=False# keep manual flush off as long as possible, because if it is on, the automatic# sending of packets is turned off and that is not recommendedsender.stop()# stop sending outReceivingTo use the receiving functionality you have to use thesACNreceiver.importsacnimporttime# provide an IP-Address to bind to if you want to send multicast packets from a specific interfacereceiver=sacn.sACNreceiver()receiver.start()# start the receiving thread# define a callback function@receiver.listen_on('universe',universe=1)# listens on universe 1defcallback(packet):# packet type: sacn.DataPacketprint(packet.dmxData)# print the received DMX data# optional: if multicast is desired, join with the universe number as parameterreceiver.join_multicast(1)time.sleep(10)# receive for 10 seconds# optional: if multicast was previously joinedreceiver.leave_multicast(1)receiver.stop()The usage of the receiver is way more simple than the sender.
ThesACNreceivercan be initialized with the following parameters:bind_address: str: Provide an IP-Address to bind to if you want to receive multicast packets from a specific interface.bind_port: int: Default: 5568. It is not recommended to change this value!
Only use when you are know what you are doing!Please keep in mind to not use the callbacks for time consuming tasks!
If you do this, then the receiver can not react fast enough on incoming messages!Functions:join_multicast(<universe>): joins the multicast group for the specific universe.leave_multicast(<universe>): leave the multicast group specified by the universe.get_possible_universes(): Returns a tuple with all universes that have sources that are sending out data and this
data is received by this machineregister_listener(<trigger>, <callback>, **kwargs): register a listener for the given trigger.
You can also use the decoratorlisten_on(<trigger>, **kwargs). Possible trigger so far:availability: gets called when there is no data for a universe anymore or there is now data
available. This gets also fired if a source terminates a stream via the stream_termination bit.
The callback should get two arguments:callback(universe, changed)universe: int: is the universe where the action happenedchanged: str: can be 'timeout' or 'available'universe: registers a listener for the given universe. The callback gets only one parameter, the DataPacket.
You can also use the decorator@listen_on('universe', universe=<universe>).
The callback should have one argument:callback(packet)packet: DataPacket: the received DataPacket with all informationremove_listener(<callback>): removes a previously registered listener regardless of the trigger.
This means a listener can only be removed completely, even if it was listening to multiple universes.
If the function never was registered, nothing happens.
Note: if a function was registered multiple times, this remove function needs to be called only once.remove_listener_from_universe(<universe>): removes all listeners from the given universe.
This does only have effect on the 'universe' listening trigger.
If no function was registered for this universe, nothing happens.DataPacketThis is an abstract representation of an sACN Data packet that carries the DMX data. This class is used internally by
the module and is used in the callbacks of the receiver.The DataPacket provides following attributes:sourceName: str: a string that is used to identify the source. Only the first 64 bytes are used.priority: int: the priority used to manage multiple DMX data on one receiver. [1-200] Default: 100universe: int: the universe for the whole message and its DMX data. [1-63999]sequence: int: the sequence number. Should increment for every new message and can be used to check for wrong
order of arriving packets.option_StreamTerminated: bool: True if this packet is the last one of the stream for the given universe.option_PreviewData: bool: True if this data is for visualization purposes.option_ForceSync: bool: True if this should only function in a synchronized state.dmxStartCode: int: the start code for the data tuple. [1-255] Default: 0x00 for streaming level data. SeeAlternate START Codesfor more information.dmxData: tuple: the DMX data as tuple. Max length is 512 and shorter tuples getting normalized to a length of 512.
Filled with 0 for empty spaces.DevelopmentSome tools are used to help with development of this library. These areflake8,pytestandcoverage.py.Install those tools with pip:pip install flake8 pytest pytest-timeout coverageflake8checks for formatting issues and can be run withflake8orpython -m flake8in the root directory of this repository.pytestis used for unit testing and can be executed withpytestorpython -m pytestin the root directory of this repository.
By default, this skips the integration test, which uses real hardware I/O and might not run in every configuration.
Use the flag--run-integration-teststo run the additional tests (e.g.python -m pytest --run-integration-tests)It is useful to check if the test coverage changed withcoverage run -m pytestand thencoverage html, which generates ahtmlcov/index.htmlfile with all the information.Changelog1.9.0: The behavior of multicast sending and receiving is now unified across most operating systems. This means Windows no longer requires to set abind_addressto be able to use multicast or universe discovery. (Thanks to mthespian! See #42 for more information)1.8.1: Callingstopon a sender or receiver now closes the underlying socket too. Note: after stopping a sender or receiver, it can not be started again withstart. (See #39 for more information)1.8.0: Added function for removing a listener on a receiver by universe. SeesACNreceiver.remove_listener_from_universe(<universe>)for more information.1.7.1: Small changes that might improve timing on the sender. (Thanks to mthespian! See #36 for more information)1.7.0: Added function for removing a listener on a receiver. SeesACNreceiver.remove_listener(<callback>)for more information.1.6.4: Functionality related to sending of sACN data is now mostly covered by tests. Removed undocumented parameters forsACNsender.start().1.6.3: Functionality related to receiving sACN data is now mostly covered by tests. Fixed a bug, where an exception was thrown on the firstDataPacketwhen the stream-termination option was set. (Additional thanks to mthespian! See #31 for more information)1.6.2: Test coverage of sub-modulemessagesis now 100%. Fixed a bug where a too long source name did not throw an exception.
Fixed a bug where invalid DMX data could be set on theDataPacket. (Thanks to mthespian! See #30 for more information)1.6.1: Fixed a bug, where the DMX start code was not set on received packets (Thanks to mthespian! See #29 for more information)1.6: Added dmxStartCode property to DataPacket (Thanks to mthespian! see #27 for more information)1.5: Performance improvement: Deleted debugging statements in hot path of packet sending and receiving (Thanks to shauneccles! See #25 for more information)1.4.6: Fix: When creating a DataPacket with invalid DMX start codes (i.e. not0x00) an exception is thrown (Thanks to niggiover9000! See #11 for more information)1.4.5: When using a manual flush, only a specified list of universes can be flushed (Thanks to CrazyIvan359! See #22 for more information)1.4.4: The universe used for the sACN-sync messages can now be set when creating asACNsender(Thanks to CrazyIvan359! See #21 for more information)1.4.3: The sequence number of the sync-packet when using manual flush was not increased (Thanks to @BlakeGarner ! See #19 for more information)1.4.2: The internal logging of the receiver_thread and output_thread was using the root logger instead of its module-logger. (Thanks to @mje-nz ! See #18 for more information)1.4: Added a manual flush feature for sending out all universes at the same time. Thanks to ahodges9 for the idea.