Arduino with C doesn't receive Python input









up vote
0
down vote

favorite












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() _BV(RXEN0);
// set frame format : asynchronous, 8 data bits, 1 stop bit, no parity
UCSR0C = _BV(UCSZ01)

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









share|improve this question























  • UBRR0H = 19200; should be UBRR0H = 0;? But it most likely makes no difference.
    – Osiris
    Nov 9 at 19:31















up vote
0
down vote

favorite












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() _BV(RXEN0);
// set frame format : asynchronous, 8 data bits, 1 stop bit, no parity
UCSR0C = _BV(UCSZ01)

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









share|improve this question























  • UBRR0H = 19200; should be UBRR0H = 0;? But it most likely makes no difference.
    – Osiris
    Nov 9 at 19:31













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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() _BV(RXEN0);
// set frame format : asynchronous, 8 data bits, 1 stop bit, no parity
UCSR0C = _BV(UCSZ01)

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









share|improve this question















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() _BV(RXEN0);
// set frame format : asynchronous, 8 data bits, 1 stop bit, no parity
UCSR0C = _BV(UCSZ01)

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






python c atmelstudio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 11:25









halfer

14.2k757105




14.2k757105










asked Nov 9 at 18:57









Ries Bezemer

11




11











  • UBRR0H = 19200; should be UBRR0H = 0;? But it most likely makes no difference.
    – Osiris
    Nov 9 at 19:31

















  • UBRR0H = 19200; should be UBRR0H = 0;? But it most likely makes no difference.
    – Osiris
    Nov 9 at 19:31
















UBRR0H = 19200; should be UBRR0H = 0;? But it most likely makes no difference.
– Osiris
Nov 9 at 19:31





UBRR0H = 19200; should be UBRR0H = 0;? But it most likely makes no difference.
– Osiris
Nov 9 at 19:31













1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










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






share|improve this answer








New contributor




Ries Bezemer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231797%2farduino-with-c-doesnt-receive-python-input%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










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






    share|improve this answer








    New contributor




    Ries Bezemer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      0
      down vote



      accepted










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






      share|improve this answer








      New contributor




      Ries Bezemer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






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






        share|improve this answer








        New contributor




        Ries Bezemer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









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







        share|improve this answer








        New contributor




        Ries Bezemer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Ries Bezemer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered Nov 14 at 9:24









        Ries Bezemer

        11




        11




        New contributor




        Ries Bezemer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Ries Bezemer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Ries Bezemer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231797%2farduino-with-c-doesnt-receive-python-input%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus