Code Pyton to

Hi people!

I want to convert this program pyton to VB 2010.

#!/usr/bin/env python
# created 19 December 2007
# copyleft 2007 Brian D. Wendt
# code adapted from:
# NOTE: This script requires the following Python modules:
# pyserial -
# pygame -
# Win32 users may also need:
# pywin32 -

import serial
import pygame

# allow multiple joysticks
joy = []

# Arduino USB port address (try "COM5" on Win32)
usbport = "/dev/ttyUSB0"

# define usb serial connection to Arduino
ser = serial.Serial(usbport, 9600)

# handle joystick event
def handleJoyEvent(e):
if e.type == pygame.JOYAXISMOTION:
axis = "unknown"
if (e.dict['axis'] == 0):
axis = "X"

if (e.dict['axis'] == 1):
axis = "Y"

if (e.dict['axis'] == 2):
axis = "Throttle"

if (e.dict['axis'] == 3):
axis = "Z"

if (axis != "unknown"):
str = "Axis: %s; Value: %f" % (axis, e.dict['value'])
# uncomment to debug
#output(str, e.dict['joy'])

# Arduino joystick-servo hack
if (axis == "X"):
pos = e.dict['value']
# convert joystick position to servo increment, 0-180
move = round(pos * 90, 0)
if (move < 0):
servo = int(90 - abs(move))
servo = int(move + 90)
# convert position to ASCII character
servoPosition = chr(servo)
# and send to Arduino over serial connection
# uncomment to debug
#print servo, servoPosition

elif e.type == pygame.JOYBUTTONDOWN:
str = "Button: %d" % (e.dict['button'])
# uncomment to debug
#output(str, e.dict['joy'])
# Button 0 (trigger) to quit
if (e.dict['button'] == 0):
print "Bye!\n"

# print the joystick position
def output(line, stick):
print "Joystick: %d; %s" % (stick, line)

# wait for joystick input
def joystickControl():
while True:
e = pygame.event.wait()
if (e.type == pygame.JOYAXISMOTION or e.type == pygame.JOYBUTTONDOWN):

# main method
def main():
# initialize pygame
if not pygame.joystick.get_count():
print "\nPlease connect a joystick and run again.\n"
print "\n%d joystick(s) detected." % pygame.joystick.get_count()
for i in range(pygame.joystick.get_count()):
myjoy = pygame.joystick.Joystick(i)
print "Joystick %d: " % (i) + joy[i].get_name()
print "Depress trigger (button 0) to quit.\n"

# run joystick listener loop

# allow use as a module or standalone script
if __name__ == "__main__":

Ok, I try some moves and some help to other vb forums where I really want to convert the code (in underline) to
This line is to Arduino joystick servo hack. Its just Im need.
' Variables needed:
Dim axis As String
Dim pos As Double
Dim move As Integer
Dim servo As Integer

' Ported code
If axis = "X" Then
pos = ???
move = CInt(Math.Round(pos * 90, 0))
If move < 0 Then
servo = 90 - Math.Abs(move)
servo = 90 + move
End If
End IF))
But not I didnt not find some library or function similar in

if e.type == pygame.JOYAXISMOTION:
Can some one help?

Or please, tell me the code in VB or C#  to communicate arduino from joystick via serial?


You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –


  • Guys, its me again...
    Sorry to borry...but I need help with this.

    import serial

    usbport = '/dev/ttyUSB0'
    ser = serial.Serial(usbport, 9600, timeout=1)
    #print ser

    def move(servo, angle):
    '''Moves the specified servo to the supplied angle.

    the servo number to command, an integer from 1-4
    the desired servo angle, an integer from 0 to 180

    (e.g.) >>> servo.move(2, 90)
    ... # "move servo #2 to 90 degrees"'''

    if (0 <= angle <= 180):
    print "Servo angle must be an integer between 0 and 180.\n"

    This code is more simple:
    I Try to to convert to VB and look like this:

    Imports Microsoft.DirectX
    Imports Microsoft.DirectX.DirectInput

    Public Class Form1
    Dim servo As Integer
    Dim RotationX As Integer

    Public Function GetSliders(ByVal servo, ByVal X) As Integer()

    If (0 <= X <= 180) Then


    End If

    End Function

    End Class

    My question is, why i have this warning?

    Warning 1 Function 'GetSliders' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.

    I understood the GetSliders dont return any value, but I put servo and X why this?

    and this line:
    def move(servo, angle):
    In google I find this information:
    In the above code there is really only one new keyword: def. def is the keyword used when defining functions. Arguments are passed in paranthesis just like C, and the return statement can't return multiple values. However, since lists, tuples, and dictionaries are basic types they can be returned instead. I'll illustrate this later. A quick thing to note about Python functions: They always return a value. However, if no return value is specified, or you don't use the return keyword, Python automatically returns the value None.

    Ok, so the code similiar in is the "Function" right?

    Thanks and sorry to borrow!
    VB.NET Shop
  • Hey people!

    I use Iron Pyton to work in 2010 and I have this windows to show:

    If you see, there are handlejoyevent and joystick control.

    Can I know ou find information about this two event? If I discovery, I
    can find some similar in write?

    Thaks in advance!!
    VB.NET Shop
  • sharpdevelop has built-in capability to convert between .net languages. Maybe it can do the trick.
  • Another option: Iron Python runs on the .NET framework, if you ran that python code in there, you should be able to call your functions from your existing VB code without having to re-write anything. Its not that much code, but if you get this working you can easily add more external python in the future without having to re-write.
  • I think that it's quite possible
    I can help with this code if you like

    Here's where I think that can be a good start:
This reply was deleted.


Shivchand Jaysaval liked Shivchand Jaysaval's profile
Aug 25