Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PySerial dataSend不';无法接收数据_Python_C_Avr_Pyserial_Atmel - Fatal编程技术网

Python PySerial dataSend不';无法接收数据

Python PySerial dataSend不';无法接收数据,python,c,avr,pyserial,atmel,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

我想在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()
#/usr/bin/python3
从termcolor导入彩色
导入系统
导入序列号
导入结构
usbComm类:
“USB通信类”
def _u初始(自身,端口):
'尝试打开端口并初始化对象'
尝试:
self.PortObj=serial.serial(端口9600,超时=1)
除O错误为e外:
打印(彩色(“错误:无法打开{0}\n错误:{1}”。格式(端口,str(e)),“红色”))
出口(1)
打印(彩色(“连接到”+self.PortObj.name,“绿色”))
def SendData(自我,数据):
“将原始数据发送到3D打印机”
'返回写入的总字节数'
返回self.PortObj.write(数据)
def读取数据(自身、计数):
返回self.PortObj.read(计数)
定义(自我):
尝试:
self.PortObj.close()
打印(彩色(“闭合”、“绿色”))
除属性错误为e外:
打印(彩色(“错误:+str(e),“红色”))
def main():
打印(彩色(“+-----------------------------------------+”,“蓝色”))
打印(彩色(“3D打印机USB通信工具”,“蓝色”))
打印(彩色(“+-----------------------------------------+”,“蓝色”))
如果len(sys.argv)!=三:
打印(彩色(“错误:参数数量无效。”,“红色”))
打印(“用法:main.py”)
出口(1)
usbCommObj=usbComm(sys.argv[1])
打印(usbCommObj.SendData(b“\x7e”))
打印(USBComObj.ReadData(1))
如果名称=“\uuuuu main\uuuuuuuu”:
main()
所以我应该收到“~”但是输出是:

已连接到/dev/ttyACM10 1. b'\xff' 封闭的

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

    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();
    }
while(1){
//字符s[7]=“”;
//对于(int i=0;i<7;i++){
//s[i]=fgetc(USBtty0);
// }
//对于(int i=0;i<7;i++){
//fputc(s[i],USBtty0);
// }
int c=fgetc(USBtty0);
//c=0x7e;
如果(c==0x7e)端口b.OUT |=PIN1_bm;
fputc(c,USBtty0);
//开关(c){
//案例0:{
//fputc(c,USBtty0);
//中断;
//     }
//案例1:{
////如果(电机(10000,0,0)==1)
//fputc(c,USBtty0);
//中断;
//     }
// }
如果(!(端口B.IN和PIN6_bm))
端口B.OUT |=PIN1|U bm;
其他的
端口b.OUT&=~PIN1_bm;
USBPoll();
}

不久前,我可以获得一个工作连接,我做错了什么?

您确定微控制器使用的波特率与您的计算机相同吗?是的,如果我从microocontroller接收到数据,则首先确保python脚本工作。断开AVR并重新连接PC输出上的TX和RX。然后尝试从此脚本传输,并收听您传输的内容。啊,抱歉,我已经解决了它