Forum Archive

Sending info from python to avr-c

ailee

okay so my situation is that i am working an avr chip which is the Atmega328p and what i want to do is i want to lets say have a user input in python lets say the user types in a string and from there i want my avr c code to interpret the code.

The reason i am doing this is because i am making a basic cnc gui so the user will input a stepper motor axis like x y or z then the direction "-" or "+".

Then a distance which is a number into python and then from python i want to send this info to the avr-c code which will then use the info to control steppers.

I want to avoid using the default serial communication thing in avr c because it will be harder to do since the user will input strings like "x-20 y+10" and etc.

It is much easier to use python to interpret the text then to use avr-c so how would i go about doing this ////////AFTER SOMETIME//////////////////////////
okay so after much confusion and researching I came up with a code based on the syntax of pyserial as well as the struct modules, here is the code:
```
import serial
import struct
import binascii

        #Function to Initialize the Serial Port
        def init_serial():

            global ser       #Must be declared in Each Function
            ser = serial.Serial()
            ser.baudrate = 9600

            ser.port = '/dev/ttyACM0' #com port of avr

            ser.timeout = 10
            ser.open()          #Opens SerialPort
            # print port open or closed
            if ser.isOpen():
                print 'Open: ' + ser.portstr

                init_serial() 
                values = (1)

                s = struct.Struct('I')  
                packed_data = s.pack(*values)

       ser.write(packed_data)    #Writes to the SerialPort

        while 1:

            bytes = ser.readline()  #Read from Serial Port
            packed_data = binascii.unhexlify(bytes)
            s = struct.Struct('I') 
            unpacked_data = s.unpack(packed_data)
            print 'You sent: ', unpacked_data      #Print What is Read from Port

        #Ctrl+C to Close Python Window```

Please inform me if I am doing something wrong or right, or if this code work actually work at all, if so this is the python side of the transmission of python data to avr-c, how would I then handle the avr-c side of this communication with python over serial communication?

zmitchell

Setting aside the code for a minute, are you saying that you want someone to control a CNC mill with their phone? On top of that you want them to control it manually? Doesn't that defeat the purpose of a CNC mill?