Jump to content

Recommended Posts

Posted (edited)
27 minutes ago, Gunners TekZone said:

FYI... Using the CLI, I run into times when nothing happens.  But I have found simply typing Lego1.IsRunning without the print() will show True or False.  When false, it seems though only way to get it back running is to close down the CLI and start over.

Ideas?

I did not run into this situation yet but I'm not testing as much as you do I guess...

I'm using Python 3.11.

 

...
>>> from LegoClassB import LegoInterfaceB
>>> Lego1=LegoInterfaceB()
>>> Lego1.ComPort="COM14"
>>> Lego1.StartLego()
>>> Lego1.Inp[1].On
False
>>> Lego1.Out[1].On
False
>>> Lego1.Out[1].On=True
>>> Lego1.Out[1].On=False
>>> Lego1.Out[2].On=True
>>> Lego1.Out[2].On=False
>>> Lego1.IsRunning
True

 

Edited by Bliss
Posted

I just installed Python 3.13 and uninstalled 3.11...  But there are still traces from old versions...   The "Path" environment variable was still set with 3.10 ...  I corrected this so pip is now back fonctionning...

So make sure your PATH is set correctly...

 

Posted (edited)

@Gunners TekZone

So I installed Python 3.13 and uninstalled older versions and updated manually the PATH system variable in windows (Control Panel, in the serch bar, entre "Path", click modify environment variables, in the next window click at the bottom Env. Variables...)

After that I reinstalled with a Dos Window, the python net using pip install pythonnet.

I checked with the Python 3.13 CLI that I was able to import clr, AddReference to my LegoClassB and use LegoInterfaceB...  Everything was OK...

 

Then in Thonny now

- Thonny uses its own Python exe...  So, You have to go in menu run, Configure Interpreter, Interpreter Tab and specify the path and exe of the python 3.13 which is the one that has python net installed...

 

- Now, in the Thonny Shell, it will show Python 3.13.1.

- If you enter import clr at the >>> prompt it should work

 

Edited by Bliss
Posted (edited)
16 minutes ago, Gunners TekZone said:

Ah Ha... I missed that part... I took Local Python 3 as literally the one Windows recognised.  I'll try that.  Thanks

I'm guessing it is possible to install the pythonnet library (Which allow the use of import clr) in the thonny environment.

First go back in the menu Run, Configure Interpreter and select the Thonny local python.exe as the python executable.

In the menu tools, click on open system shell.  I think it is possible from there...

From the shell, I entered: pip install pythonnet and it did install in thonny libraries...

Then from the Thonny local python 3.10, I was able to use import clr etc...

 

 

Edited by Bliss
Posted (edited)
On 1/6/2025 at 9:41 PM, Gunners TekZone said:

 think, just putting in the correct path to the python.exe worked for that... since now I stall out here instead:


import time
import clr
clr.AddReference(r"C:\Users\Gunner\Desktop\LegoClassB\bin\Release\LegoClassB")
from LegoClassB import LegoInterfaceB
Lego1 = LegoInterfaceB()

And, Yes... that is the correct path, as it works in the CLI (well, not as a coherent program, but line by line...)

When I paste the content inside the code box into NotePad++, it shows a stange character at the end of:  from LegoClassB import LegoInterfaceB

 

It happens often in the forum Code Window...  I have to double check some time...  (These character are not visible in the final post but we can see them in notepad++ and sometime, in the code edition window.

 

Edited by Bliss
Posted

@Gunners TekZone

But at some point you said it was working no?

Is it working in the Python CLI now or not?

Is it only in Thonny that you have problems?

Is it working in Windows 10 but not in windows 7 ?  (I think I read somewhere, pythonnet would not work well on win 7)

 

Posted (edited)

@Gunners TekZone

I copied the LegoClassB.dll (ONLY This DLL) in a Python work Folder along with the .py file (Same ones than in the dropbox link).

In Thonny, on the Files pane on the Left, I selected this Pyton Folder and see listed the Dll and the .py files.
This becomes the working directory.

In the LegoTrain_01.py, the path was simply the name of the dll:

import clr
clr.AddReference(r"LegoClassB")
from LegoClassB import LegoInterfaceB
Lego1=LegoInterfaceB()

Then I hit the "Play" button in Thonny and this is working here very well too.

 

Edited by Bliss
Posted (edited)

@Gunners TekZone

If we forget about win7 for a moment and come back to win10,

I think the unknown location problem is related to:

https://github.com/pythonnet/pythonnet/discussions/2420

If you put the whole directory of the LegoClassB.zip inside your python working directory, then it appears the PATH starting with the LegoClassB folder is already seen by Python because Python automatically uses the working path in its seach path for modules... 
The folder named LegoClassB conflicts with the DLL file Name. 

So in "from LegoClassB import LegoInterfaceB" line, Python prioritize the Folder as a package at first...

So, remove the LegoClassB folder from your working directory and leave only the LegoClassb.dll in your working directory along with your .py files and just do like my second previous post.

OR, rename the LegoClasB folder to make it different than the Dll file name.

 

>>> import clr
>>> clr.AddReference(r"C:\Users\sengx\source\repos\Python\LegoClassB\bin\Release\LegoClassB")
<System.Reflection.RuntimeAssembly object at 0x000001B853287740>
>>> from LegoClassB import LegoInterfaceB
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'LegoInterfaceB' from 'LegoClassB' (unknown location)
>>> 

 

Edited by Bliss
Posted
8 hours ago, Gunners TekZone said:

AH HA!... Could this be the first time a LEGO DACTA Interface-B has been used to communication over a network via MQTT???

Congrats!

One small step for man, one giant leap for old lego fans ;-)

We can now think of using Smart Home Hubs and devices like Home Assistant, Hubitat, SmartThings, name it, that supports MQTT, to start/stop an "Old 9V Train" automatic sequence that uses Lego Interface B.
We could add a button on Home Assistant (HA) UI (I'm using Home Assistant) or use HA Automation, or Node-Red Plugin that will use MQTT to interact with the Lego Box...

That certainly gives some ideas...

 

Posted (edited)
6 minutes ago, Gunners TekZone said:

FYI, after some experimenting, I found that with having both my script and your .dll in the same folder, all I needed for the clr path is:


clr.AddReference(r'LegoClassB') # LegoClassB.dll needs to be in same folder as this script

 

I did the same observation as I mentionned in a previous post that I suspect you have not seen as we posted almost at the same time.

 

 

Edited by Bliss
Posted

I had a look to Shamlian 10 years old Python program.
It was not working at all. I guess Python versions changed a lot over the years.
So I modified it to make it work under Python 3.13.  Took few hours of debugging, googling etc...

Original Link that has been already posted few time here.

Then I decided to rewirte some part of the driver and made quite a few changes and I renamed the dacta.py file to legob file.

LINK TO NEW LEGO B Python Driver

All you have to do is to put the legob.py file in the same working folder as your project.
For example, in Thonny python editor, open your working folder on the left pane and this file should copied there.

Then you can test with Thonny Shell by entering the following at the >>> prompt:

>>> from legob import LegoB
>>> Lego1 = LegoB('COM1')

You should get "Got confirmation string." if everything works

>>> Lego1.out(1).on() 
this should Activate output A

if you want to use letter for output port then enter the following:

>>> A, B, C, D, E, F, G, H = 1, 2, 3, 4, 5, 6, 7, 8
Then:
>>> Lego1.out(A).off()
should turn off output A

To get input states or values etc:

>>> Lego1.inp(1).on  # will print True or False.
>>> Lego1.inp(8).val
>>> Lego1.inp(6).rot
to reset the rotation count to 0 or other value:
>>> Lego1.inp(6).rot=0
There are also temperature:
>>> Lego1.inp(3).tempc
>>> Lego1.inp(3).tempf

>>> Lego1.inp(0).on is the state of the Red Stop Button on the lego box.

You can set alias to inp ou out ports:
>>> MotorA = Lego1.out(A)
Then to start the motor right at power 5:
>>> MotorA.pow(5)
>>> MotorA.onr

All output commands: on(), onl() (on left), onr() (on right), off() (coast to stop), brk() (Brake stop), rev() (reverse), pow(#) set power where #=0 to 7, onfor(t) output will turn on for t tenth of seconds, l() (Set direction left), r() (Set direction right)

To stop lego box: Lego1.close()

I did not test thoroughly.  Let me know if you find any issues.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...