port python script to TD

hi,

i am trying to make use of an external python lib to play with a thermal printer.

here is the code i found :
github.com/chriszirkel/Python-T … ermal.1.py

From the TD Wiki, it seems i can copy/paste this code into a text DAT and call it from a python script inside TD with a standard import

It seems ok, but this code invokes a serial python class. Now i would like to adapt this code to make use of a serial DAT instead of this serial class (like using serial = op('serial1') instead of import serial ) but i can’t seem to make it work…

Any help on how to proceed with this ?

thanks a lot

The difference is that the serial module ( import serial ) is a python library where as the serial operator ( op(‘serial1’) ) is a TouchDesigner op object. In theory you could probably make the switch but it would be a bit of work and I’m not sure if there would be any real advantage to doing things this way.

Is there any particular reason you need to use the Serial DAT? This code should still work in TD as you can import the python serial library no problem.

well first of all i wasn’t able to make it work with the python serial. I know it is not installed by default with python so i did install it (so i think i did) but it keeps complaining it can’t find serial…

so i tried the serial DAT way but i don’t seem i have the logic right…
And also i found it interesting to not rely on an external python module and being able to port the project everywhere without having to install any dependencies.

First thing to check is that your python 64bit module path parameter is set correctly in TouchDesigner. Instructions for that can be found on this page:

[url]Introduction to Python Tutorial - Derivative

You could always try to run the script outside of TD (or just try importing the module externally) to see if the module is installed correctly.

They key difference with using the Serial DAT vs the serial module are the names of the attributes you are calling. Check out this page here to see what it’s expecting:

[url]https://www.derivative.ca/wiki099old/index.php?title=SerialDAT_Class[/url]

Basically the only thing you should be doing with it is .send() or .sendBytes(). The code you are trying to port creates a class for the thermal printer that inherits Serial from the serial module and does many things with that Serial class that the Serial DAT already handles ( e.g. connecting, settings the baud rate, etc. ).

Anyway, assuming the class will actually let you inherit from a td OP, I think the best way to approach this would be to replace this:

from serial import Serial

with

Serial = op('serial1')

Then replace self.writeBytes with self.sendBytes

That should get you pretty close but I’m sure you’ll need to delete a bunch of other unneeded lines. Compiling the script at this point should tell you where the errors are. In particular, line 85 wouldn’t be needed. That one is just to initialize an instance of the Serial Class.

You might also consider making this a component extension. Usually I would do it this way and instead of inheriting the serial dat op I would set it as a private attribute in the init function ( self.serial = op(‘serial1’) ). Just makes it a little easier to interface with. Here are the docs for that:

[url]https://docs.derivative.ca/index.php?title=Extensions[/url]

hi,

thanks a lot for your help.

I came close to those suggestions but i have some issues i can’t solve :

  • i need to instantiate a printer from this class right ? my test is like so

from modThermal import * printer = Adafruit_Thermal() printer.feed(4)

But it complains like

  • Also the writeBytes() in the thermal class seems based on some write() functions. Is the writeBytes() the lower level function which has to be replaced with sendBytes() ? or could it be the write() one ?

Yeah I think it should be sendBytes().

I think the issue is that you’re passing the Serial DAT into the class. Try changing this:

class Adafruit_Thermal(Serial):

to this:

class Adafruit_Thermal():

and add this under the init() function.

self.serial = op('serial1')