我似乎无法从我的串行COM11(我使用的是atmega328p)到我的python cod中获取数据

2024-10-02 20:36:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个跟线机器人。我从我的红外传感器在我的端口C引脚的数字信号。这辆车开得很好。现在我需要通过从我使用的C代码向python代码发送一个变量来绘制一个图。我试过使用USART\u传输和使用read函数。我似乎无法将数据输入到我的python代码中。这是我的密码。你知道吗

#define F_CPU 16000000UL
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1
#include <avr/io.h>
#include <util/delay.h>

void USART_Init( unsigned int ubrr);
unsigned char USART_Read();
void USART_Transmit( unsigned char data );

int main(void){
    USART_Init(MYUBRR);
    initIRSensor();
    int check = 0;
    while(1){

        if(PINC == 0b00000001){
            USART_Transmit(1);

        }

    }

    return 0;
}

void USART_Init( unsigned int ubrr)
{
    /*Set baud rate */
    /* UBRR0H contains the 4 most significant bits of the
    baud rate. UBRR0L contains the 8 least significant
    bits.*/  
    UBRR0H = (unsigned char)(ubrr>>8);
    UBRR0L = (unsigned char)ubrr;


    /*Enable transmitter */
    UCSR0B = (1<<RXEN0)  |(1<<TXEN0);

    /* Set frame format: 8data */
    UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}
void USART_Transmit( unsigned char data )
{
    /* Wait for empty transmit buffer */
    while ( !( UCSR0A & (1<<UDRE0)) );

    /* Put data into buffer, sends the data */
    UDR0 = data;
}

unsigned char USART_Read()
{
    /* Wait for empty transmit buffer */
    while ( !( UCSR0A & (1<<RXC0)) );

    /* Put data into buffer, sends the data */
    //UDR0 = data;
    return UDR0;
}

我希望将值“1”发送到我的python代码中,以便绘制绘图。这是我的python代码。只要灯泡1高,我就给一个增量。我的问题是read函数不能正常工作。串行通信工作正常,因为我使用了序列号写入功能和它的工作很好。那个我的问题是read函数。希望有人能帮忙:)

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import serial
import keyboard

bulb1=0
ser = serial.Serial('COM11')
print(ser.name)
while 1:
    if(ser.read() == 1):
        bulb1+=1

Tags: the函数代码importreaddatabufferint