11 added 178 characters in body
source | link

Yes I keep my ports open continuously. UNLESS I am running the IDE. The IDE requires that I not have any other programs trying to use the same serial port.


The code I use on the Arduino to open the port is much more simple than you're showing.

Test it out in the serial monitor from your IDE to make sure the Arduino is working correctly before moving on to the Python program onon the Raspberry Pi.

The code I use on the Arduino to open the port is much more simple than you're showing.

Test it out in the serial monitor from your IDE to make sure the Arduino is working correctly before moving on to the Python program on the Raspberry Pi.

Yes I keep my ports open continuously. UNLESS I am running the IDE. The IDE requires that I not have any other programs trying to use the same serial port.


The code I use on the Arduino to open the port is much more simple than you're showing.

Test it out in the serial monitor from your IDE to make sure the Arduino is working correctly before moving on to the Python program on the Raspberry Pi.

10 added 470 characters in body
source | link
Serial.begin(9600);
while (Serial.available()>0) serIn=Serial.read();

then use just regular print and prinln statements.

And you can read fromto open it inthen flush the normal waybuffer using a char variable.  

Note that the serial monitor will always send an end-of-line \n - so to read single-character commands the best way is like so.

if (Serial.available()>0) {
  serIn=Serial.read()
  if (SerIn=='A') { 
    ...do something...
  }
  while (Serial.available()>0) serIn=Serial.read();
}     

On the Raspberry Pi in python, talking to a Nano, here is the Python code to open the port:

Very simple. Nano

Nano mounted on a breadboard so there is room for voltage dividers and such, then linked to the Rpi3 by the programming cable to send the readings to the Raspberry Pi and to receive instructions to toggle digital outputs which can switch relays.

(I always use single-character instructions. Arduino char 'X' means to shut off the inverter, for instance.)

Python code to process input from the serial port:

while True :
    linein = ser.readline()
    ...etc - to process input from the Arduino.  

...etc - to process input from the Arduino.

If you want Python code to write a single character out to the Arduino you use serial port:

to sendThis sends the single letter Acharacter "A", which is received on the Arduino as a char 'A' in athe read loop shown above.

Note that on the Raspberry Pi inIn Python you use double-quotes to denote a character for writing. It is received on the Arduino as a   charser.write does not add an end- the data type which is denoted by singleof-quotes for use in if comparisons of input from a serial readline.

If you are using an Uno the open sequence will instead be this because they tend to use a different port: in Jessie.

You can see which ports are there with this bash command :

#ls /dev/tty*

ThatThis is criticalimportant but rarely isnot much of an issue once you have it tuned up and don't use the IDE as often. My

My systems run 24x7 for days on end. I rarely need to change the programs now that they are stable.

You You can see some of the output generated by these systems here:
   https://www.sdsolarblog.com/montage

Serial.begin(9600);

then use just regular print and prinln statements.

And you can read from it in the normal way.  

On the Pi in python, talking to a Nano here is the Python code to open the port:

Very simple. Nano mounted on a breadboard so there is room for voltage dividers and such, then linked to the Rpi3 by the programming cable to send the readings to the Raspberry Pi and to receive instructions to toggle digital outputs which can switch relays.

(I always use single-character instructions. Arduino char 'X' means to shut off the inverter, for instance.)

while True :
    linein = ser.readline()

...etc - to process input from the Arduino.

If you want to write out to the Arduino you use

to send the single letter A, which is received on the Arduino as a char in a read loop.

Note that on the Raspberry Pi in Python you use double-quotes to denote a character for writing. It is received on the Arduino as a char - the data type which is denoted by single-quotes for use in if comparisons of input from a serial read.

If you are using an Uno the open sequence will instead be this port:

That is critical but rarely is an issue once you have it tuned up. My systems run 24x7 for days on end. I rarely need to change the programs now that they are stable.

You can see some of the output generated by these systems here:
 https://www.sdsolarblog.com/montage

Serial.begin(9600);
while (Serial.available()>0) serIn=Serial.read();

to open it then flush the buffer using a char variable.

Note that the serial monitor will always send an end-of-line \n - so to read single-character commands the best way is like so.

if (Serial.available()>0) {
  serIn=Serial.read()
  if (SerIn=='A') { 
    ...do something...
  }
  while (Serial.available()>0) serIn=Serial.read();
}     

On the Raspberry Pi in python, talking to a Nano, here is the Python code to open the port:

Very simple.

Nano mounted on a breadboard so there is room for voltage dividers and such, then linked to the Rpi3 by the programming cable to send the readings to the Raspberry Pi and to receive instructions to toggle digital outputs which can switch relays.

(I always use single-character instructions. Arduino char 'X' means to shut off the inverter, for instance.)

Python code to process input from the serial port:

while True :
    linein = ser.readline()
    ...etc - to process input from the Arduino.  

Python code to write a single character out the serial port:

This sends the single character "A", which is received on the Arduino as a char 'A' in the read loop shown above.

In Python you use double-quotes to denote a character for writing.   ser.write does not add an end-of-line.

If you are using an Uno the open sequence will instead be this because they tend to use a different port in Jessie.

You can see which ports are there with this bash command :

#ls /dev/tty*

This is important but not much of an issue once you have it tuned up and don't use the IDE as often.

My systems run 24x7 for days on end. I rarely need to change the programs now that they are stable. You can see some of the output generated by these systems here:  https://www.sdsolarblog.com/montage

9 added 470 characters in body
source | link

I do this all the time. Two-way communications works just fine.

The code I use on the Arduino to open the port is much more simple than you're showing.

Serial.begin(9600);

then use just regular print and prinln statements.

And you can read from it in the normal way.

Test it out in the serial monitor from your IDE to make sure the Arduino is working correctly before moving on to the Python program on the Raspberry Pi.


On the Pi in python, talking to a Nano here is the Python code to open the port:

import serial
ser = serial.Serial("/dev/ttyUSB0",9600)
ser.flushInput()

Very simple. Nano mounted on a breadboard so there is room for voltage dividers and such, then linked to the Rpi3 by the programming cable to send the readings to the Raspberry Pi and to receive instructions to toggle digital outputs which can switch relays.

(I always use single-character instructions. Arduino char 'X' means to shut off the inverter, for instance.)

while True :
    linein = ser.readline()

...etc - to process input from the Arduino. 

If you want to write out to the Arduino you use

ser.write("A")

to send the single letter A, which is received on the Arduino as a char in a read loop.

Note that on the Raspberry Pi in Python you use double-quotes to denote a character for writing. It is received on the Arduino as a char - the data type which is denoted by single-quotes for use in if comparisons of input from a serial read.


If you are using an Uno itthe open sequence will instead be this codeport:

import serial
ser = serial.Serial("/dev/ttyACM0",9600)
ser.flushInput()

No magic involved. Just very straightforward and simple.


There's only one gotcha: If you are running your Arduino IDE on the Raspberry Pi then you need to make sure you don't interfere with the serial port during program uploading to the Arduino.

That is critical but rarely is an issue once you have it tuned up. My systems run 24x7 for days on end. I rarely need to change the programs now that they are stable.

You can see some of the output generated by these systems here:
https://www.sdsolarblog.com/montage

I do this all the time. Two-way communications works just fine.

The code I use on the Arduino to open the port is much more simple than you're showing.

Serial.begin(9600);

then use just regular print and prinln statements.

And you can read from it in the normal way.

Test it out in the serial monitor from your IDE to make sure the Arduino is working correctly before moving on to the Python program on the Raspberry Pi.


On the Pi in python, talking to a Nano here is the Python code:

import serial
ser = serial.Serial("/dev/ttyUSB0",9600)
ser.flushInput()

Very simple. Nano mounted on a breadboard so there is room for voltage dividers and such, then linked to the Rpi3 by the programming cable to send the readings to the Pi and to receive instructions to toggle digital outputs which can switch relays.


If you are using an Uno it will instead be this code:

import serial
ser = serial.Serial("/dev/ttyACM0",9600)
ser.flushInput()

No magic involved. Just very straightforward and simple.


There's only one gotcha: If you are running your Arduino IDE on the Raspberry Pi then you need to make sure you don't interfere with the serial port during program uploading to the Arduino.

That is critical but rarely is an issue once you have it tuned up. My systems run 24x7 for days on end. I rarely need to change the programs now that they are stable.

You can see some of the output generated by these systems here:
https://www.sdsolarblog.com/montage

I do this all the time. Two-way communications works just fine.

The code I use on the Arduino to open the port is much more simple than you're showing.

Serial.begin(9600);

then use just regular print and prinln statements.

And you can read from it in the normal way.

Test it out in the serial monitor from your IDE to make sure the Arduino is working correctly before moving on to the Python program on the Raspberry Pi.


On the Pi in python, talking to a Nano here is the Python code to open the port:

import serial
ser = serial.Serial("/dev/ttyUSB0",9600)
ser.flushInput()

Very simple. Nano mounted on a breadboard so there is room for voltage dividers and such, then linked to the Rpi3 by the programming cable to send the readings to the Raspberry Pi and to receive instructions to toggle digital outputs which can switch relays.

(I always use single-character instructions. Arduino char 'X' means to shut off the inverter, for instance.)

while True :
    linein = ser.readline()

...etc - to process input from the Arduino. 

If you want to write out to the Arduino you use

ser.write("A")

to send the single letter A, which is received on the Arduino as a char in a read loop.

Note that on the Raspberry Pi in Python you use double-quotes to denote a character for writing. It is received on the Arduino as a char - the data type which is denoted by single-quotes for use in if comparisons of input from a serial read.


If you are using an Uno the open sequence will instead be this port:

import serial
ser = serial.Serial("/dev/ttyACM0",9600)
ser.flushInput()

No magic involved. Just very straightforward and simple.


There's only one gotcha: If you are running your Arduino IDE on the Raspberry Pi then you need to make sure you don't interfere with the serial port during program uploading to the Arduino.

That is critical but rarely is an issue once you have it tuned up. My systems run 24x7 for days on end. I rarely need to change the programs now that they are stable.

You can see some of the output generated by these systems here:
https://www.sdsolarblog.com/montage

8 added 470 characters in body
source | link
7 added 470 characters in body
source | link
6 deleted 58 characters in body
source | link
5 Clarification
source | link
4 added 176 characters in body
source | link
3 added 246 characters in body
source | link
2 added 246 characters in body
source | link
1
source | link