Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Python

NAO.NET, Python Programs to .NET

5.00/5 (8 votes)
19 Jun 2015CPOL3 min read 42K   778  
Workaround to run and exchange data between .NET and other platforms

Image 1

Introduction

Youtube article video

This article mainly shows how to:

  1. Execute any process or file like Python programs using .NET
  2. Exchange data between Python and .NET using XML

Background

I have a robot named NAO V5. This robot version does not support .NET, but it uses Python to operate the robot. If you want to let .NET run command and exchange data with Python for your application, this will be useful for you. If you have NAO Robot and you want to use .NET framework like my case, you can use the DLL attached.

In this example, I use NAO robot v5 only as an example because it does not support .NET and previous versions support .NET.

Files Include

The attached files include:

  • .NET Windows application as interface
  • .NET DLL To interact with Python programs
  • Python example file
  • INI configuration file (you can change its values to your environment)

Using the Code

To use this code, you need:

  • .NET with some knowledge for C#
  • Some knowledge about Python

This code shows you the following tricks:

  • Execute Python files from .NET environment using C#
  • Getting the result data from executing files
  • Write and read XML files
  • Read INI files
  • Exchange data between Python and .NET through XML files
  • Use NAO Robot as example

I'll divide the code into three parts:

  1. Run python code from .NET Environment
  2. Exchange data between Python and .NET. using XML
  3. Use the complete example for NAO Robot

1. Run Python Code from .NET Environment

If you want to Execute python programs from .NET C# program, then you use this script:

C#
myProcess.StartInfo.FileName = fileName; //This is python File Name
myProcess.StartInfo.CreateNoWindow = true;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.Start();

This code in the MSDN also.

2. Exchange Data between Python and .NET using XML

Usually, the previous code does not allow you to get much information you need from the process result or python program, to add more values. You can use many ways, I choose XML files.

Here is how to:

  1. Create and write XML In Python
  2. Read XML in C#

A. Write XML in Python

C#
E = lxml.builder.ElementMaker()
ROOT = E.root
DOC = E.doc
XMLHeadYaw = E.HeadYaw
XMLHeadPitch = E.HeadPitch
XMLLHand = E.LHand
XMLRHand = E.RHand

the_doc = ROOT(
        DOC(
            XMLHeadYaw('', value=str(HeadYaw)),
            XMLHeadPitch('', value=str(HeadPitch)),
            XMLLHand('', value=str(LHand)),
            XMLRHand('', value=str(RHand)),
            )   
        )

B. Read XML in C#

C#
public static string GetNAOValue(string ItemName,string ItemField)
     {
                 XmlDocument doc = new XmlDocument();
                 doc.Load(clsDefinitions.xmlFile);
                 XmlNode ItemNode = doc.GetElementsByTagName(ItemName).Item(0);
                 return ItemNode.Attributes[ItemField].Value;
 }

3. Use the Complete Example for NAO Robot

In my case: I had to adjust the Python files to start with the following statements using the arguments which I will pass from my .NET C# Program.

C#
if __name__ == "__main__":
   robotIP = clsStatics.clsStatics.RobotIP
   PORT = clsStatics.clsStatics.RobotPort
   Hand = "RHand"
   Angle = 1
   if len(sys.argv) > 1: 
        robotIP = sys.argv[1]
        clsFile.clsFile.logInFile(str(robotIP))
        Hand =  sys.argv[2]
        clsFile.clsFile.logInFile(str(Hand))
        Angle= float(sys.argv[3])
        clsFile.clsFile.logInFile(str(Angle))

HandMove(robotIP,PORT,Hand,Angle)

Then, adjust the .NET to execute these Python files like I discussed above, then add INI file to config the Robot information, then add XML to pass more data and results from robot and Python environment to the .NET environment (optional , you can cancel this step as well).

Points of Interest

I found this is a good workaround to interact with other platforms like Python using .NET C#. By wrap C# code to get and pass data while running both programs at the same time. *Note: You can use this code for any other program or execute any process from your .NET program.

History

  • Added more NAO Robot Events (current Have Hand, Head, Also Take picture)
  • Needed to add more Actions to the DLL
  • Caught more events and memory values from Robot
  • There is also OpenCV module for .NET started to develop it (in Progress..), but I may present it in another article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)