PySerial dataSend不接收数据

时间:2015-06-24 09:56:50

标签: python c avr pyserial atmel

我想在python脚本和atxmega128a3u avr微控制器之间建立连接。

Python脚本:

#!/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()

所以我应该收到&#34;〜&#34;但输出是:

连接到/ dev / ttyACM10 1 B&#39; \ XFF&#39; 关闭

在我的微控制器上,我有代码:

    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();
    }

前段时间我可以建立联系,我错了什么?

0 个答案:

没有答案