Raspberry pi will not send serial data to arduino using either minicom or python

I am trying to get a raspberry pi to communicate with arduino using tx / rx contacts. I have an arduino programmed to send an ASCII code for the letter it received.

code:

byte number = 0; void setup() { Serial.begin(9600); } void loop() { if (Serial.available()) { number = Serial.read(); Serial.print("character recieved: "); Serial.println(number, DEC); } } 

But when I open minicom and it enters into it, nothing happens. If I open the arduino serial monitor and send the minicompany character, the “received character” and ASCII characters will be displayed. I tried to create a python program using py serial,

code:

 import serial ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) ser.open() ser.write("testing") try: while 1: response = ser.readline() print response except KeyboardInterrupt: ser.close() 

but nothing is displayed. I looked all over the Internet, but I did not find a solution. Please help, thanks in advance.

+4
source share
2 answers

First, ser.readline looks for \ r \ n before it returns anything, so sending one byte will just go to the buffer.

So you want to always use ser.println for the last part of the line. This works the same on both Pi and Arduin.

In addition, in Minicom you want to press Enter after each line, so ser.readline () will return the line.

..

You do not need to do anything strange to make the serial port work. However, I am using the actual USB connector and not connecting directly to the TX / RX lines. In fact, I use the same cable that you would use to program it from a PC.

I use Rpi as an Arduino programmer and process the results that it sends back from the analog readings.

The python Rpi program that reads the series should be stopped when loading from the IDE into Arduino, but this is the only real consideration. Two programs cannot simultaneously capture a USB port.

On the rpi side, I originally used

 ls /dev/ttyUSB* 

to find the port that it uses. I even unplugged the cable, and then ran this command again, and it disappeared. Insert it back and it is back. It is very reliable (as opposed to connecting a USB memory).

On the Arduino side, there is always the same serial material that you already have in your program.

You have the right to use minicom for testing.

To find which line to use in shebang, I used this:

 which python 

So here is what I use on Rpi3:

 #!/usr/bin/python import serial from datetime import datetime tab = "\t" ser = serial.Serial("/dev/ttyUSB0",9600) while True : linein = ser.readline() if len(linein)<10 : continue print "/dev/ttyUSB0 input --> " + repr(linein) date = str(datetime.now().date()) date = date[:10] time = str(datetime.now().time()) time = time[:8] outline = date + tab + time + tab + linein if not outline.endswith("Inverter\r\n") : f = open("htv.dat","a") f.write(outline) f.close() print "htv.dat ----> " + repr(outline) print "htv.dat ----> " + outline else: print 

To make it executable, I do this:

 cp ss.py ssx chmod +x ssx sudo cp ssx /usr/sbin 

And here is the code I use on Nano:

 // HTV - Humidity, Temperature, Voltage 2/4/17 // // Voltage measurement variables for the voltage divider A2 float R1 = 1000000.0; float R2 = 147000.0; float constADC=4.59; // Humidity/temp readings D2 #include <Adafruit_Sensor.h> #include "DHT.h" #define DHTPIN 2 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); } void loop() { int value=analogRead(A2); float vout = value * (constADC/1024.0); float vin = vout / (R2/(R1+R2)); float h = dht.readHumidity(); float t = dht.readTemperature(); float df = t*(9.0/5.0)+32; if (isnan(h) || isnan(t)) { h=0; df=0; } Serial.print(h); Serial.print("\t"); Serial.print(df); Serial.print("\t"); Serial.println(vin); delay(60000); } 

Nothing special. I just open a terminal window on Rpi and type ssx and it starts to collect. If I need to load something into Nano from the IDE, I use ^ C to stop SSX at boot time. Then I restart it.

Opening a file for writing and then closing it prevents data corruption.

0
source

Very late to the party, but today I ran into the same problem. What you need to do is configure minicom so as not to use hardware flow control. This is what stops the input you give minicom from going to Arduino.

Press Ctrl+A , then O (letter Oh) and select Serial port setup , and there set the Hardware flow control to No

Once you do this, you can save the new default setting: Press Ctrl+A again, then O (letter Oh), but now select Save setup as dfl

0
source

Source: https://habr.com/ru/post/1493039/


All Articles