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
python c atmelstudio
add a comment |
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
python c atmelstudio
UBRR0H = 19200;should beUBRR0H = 0;? But it most likely makes no difference.
– Osiris
Nov 9 at 19:31
add a comment |
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
python c atmelstudio
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
python c atmelstudio
edited Nov 10 at 11:25
halfer
14.2k757105
14.2k757105
asked Nov 9 at 18:57
Ries Bezemer
11
11
UBRR0H = 19200;should beUBRR0H = 0;? But it most likely makes no difference.
– Osiris
Nov 9 at 19:31
add a comment |
UBRR0H = 19200;should beUBRR0H = 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
add a comment |
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);
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.
add a comment |
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);
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.
add a comment |
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);
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.
add a comment |
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);
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);
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.
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.
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
UBRR0H = 19200;should beUBRR0H = 0;? But it most likely makes no difference.– Osiris
Nov 9 at 19:31