I want to make a connection between python script and atxmega128a3u avr microcontroller.
Python script:
#!/usr/bin/python3
from termcolor import colored
import sys
import serial
import struct
class usbComm:
'USB Communication Class'
def __init__(self, Port):
'Try to open the port and initialize the object'
try:
self.PortObj = serial.Serial(Port, 9600, timeout=1)
except OSError as e:
print(colored("ERROR: Failed to open {0}\nERROR: {1}".format(Port, str(e)), "red"))
exit(1)
print(colored("Connected to " + self.PortObj.name, "green"))
def SendData(self, Data):
'Send raw data to 3D Printer'
'Returns total number of bytes written'
return self.PortObj.write(Data)
def ReadData(self, count):
return self.PortObj.read(count)
def __del__(self):
try:
self.PortObj.close()
print(colored("Closed", "green"))
except AttributeError as e:
print(colored("ERROR: " + str(e), "red"))
def main():
print(colored("+-----------------------------------+", "blue"))
print(colored("| 3D Printer USB Communication Tool |", "blue"))
print(colored("+-----------------------------------+", "blue"))
if len(sys.argv) != 3:
print(colored("ERROR: Invalid number of arguments.", "red"))
print("Usage: main.py <PORT> <DATA>")
exit(1)
usbCommObj = usbComm(sys.argv[1])
print(usbCommObj.SendData(b"\x7e"))
print(usbCommObj.ReadData(1))
if __name__ == "__main__":
main()
So i should receive "~" But the output is:
Connected to /dev/ttyACM10 1 b'\xff' Closed
At my microcontroller i have the code:
while(1){
// char s[7] = "";
// for(int i = 0; i< 7; i++){
// s[i] = fgetc(USBtty0);
// }
// for(int i = 0; i< 7; i++){
// fputc(s[i], USBtty0);
// }
int c = fgetc(USBtty0);
//c=0x7e;
if (c==0x7e)PORTB.OUT |= PIN1_bm;
fputc(c, USBtty0);
// switch(c) {
// case 0:{
// fputc(c, USBtty0);
// break;
// }
// case 1:{
// //if(motor(10000,0,0)==1)
// fputc(c, USBtty0);
// break;
// }
// }
if(!(PORTB.IN & PIN6_bm))
PORTB.OUT |= PIN1_bm;
else
PORTB.OUT &= ~PIN1_bm;
USBPoll();
}
Some time ago i could get an working connection, what did I wrong?