简体   繁体   中英

Arduino with C doesn't receive Python input

So, I have a school project where we have to send commands to an Arduino Uno, when it receives the command it will send data from a sensor back. Now i have it working to the part where if I send a command over putty it will react to it. But here is the problem: When I send a command over Python it won't do anything with it. To be clear; I can receive output of my sensor with Python, but then I have a C program continuously pushing me data.

Here is my Python code:

import serial,time
ComPort = serial.Serial('COM3')
ComPort.baudrate = 19200
ComPort.bytesize = 8
ComPort.parity = 'N'
ComPort.stopbits = 1
data = "4"
data = data.encode("utf-8")
time.sleep(1.6)
ComPort.write(data)
out = " "
i = 0
while i < 1:
   while ComPort.inWaiting() > 0: 
      out +=ComPort.read(1).decode()
      i = i + 1
ComPort.close()

And here is my C code; I left some parts out because that had to do with the sensor, the sensor works fine

#define F_CPU 16E6
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>

#define UBBRVAL 51
void uart_init() {
    // set the baud rate
    UBRR0H = 19200;
    UBRR0L = UBBRVAL;
    // disable U2X mode
    UCSR0A = 0;
    // enable transmitter and receiver
    UCSR0B = _BV(TXEN0)|_BV(RXEN0);
    // set frame format : asynchronous, 8 data bits, 1 stop bit, no parity
    UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
}

void UART_Putstring(char* eenstring)
{
    while(*eenstring != 0X00)
    {
        transmit(*eenstring);
        eenstring++;
    }
}

unsigned char receive( void )
{
    /* Wait for data to be received */
    while ( !(UCSR0A & (1<<RXC0)) );
    /* Get and return received data from buffer */
    return UDR0;
}
void transmit(uint8_t data)
{
    // wait for an empty transmit buffer
    // UDRE is set when transmit buffer is empty
    loop_until_bit_is_set(UCSR0A, UDRE0);
    // send the data
    UDR0 = data;
}

int main(void)
{
    char tot_string[6];
    uint8_t tijdelijk;
    float tijdelijk_float;
    char input;

    DDRD |= 1<<5; // Setup HC-SR04 Trigger as an output
    DDRD &= ~(1<<4); // Setup HC-SR04 Echo a an input
    _delay_ms(50);
    uart_init();

    while(1)
    {   
        input = receive();
        if(input==0x34)
        {
            // This is converting sensor Data
            tijdelijk = hcsr04();
            tijdelijk_float = (float)(tijdelijk) * 40;
            tijdelijk_float = tijdelijk_float/58; 
            dtostrf(tijdelijk_float, 2, 2, tot_string);
            UART_Putstring(tot_string);
        }               
    }
} 

So i found the answer, it is really simple. Just simply do this in C:

if(input() == '4')
{
    tijdelijk = hcsr04();
    tijdelijk_float = (float)(tijdelijk) * 40;
    tijdelijk_float = tijdelijk_float/58; 
    dtostrf(tijdelijk_float, 2, 2, tot_string);
    UART_Putstring(tot_string);
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM