Other Projects

Banana-Arduino serial communication - HowTo (headless, no USB)

7 7374
Post Last Edited by MrGlasspoole at 2014-10-13 08:34

Hi,

maybe it helps somebody...
You need a Logic Level Converter because most Arduinos work with 5 Volt and the Banana with 3.3 Volt!
Something like this: https://www.sparkfun.com/products/12009

Connections (Banana <> Converter Low<> Converter High <> Arduino Mega Serial1):
Pin 1 <>  LV <> HV <> 5V
Pin 24 <>  GND <> GND <> GND
Pin 22 (RX) <>  LV1 <> HV1 <> D18 (TX1)
Pin 23 (TX) <>  LV2 <> HV2 <> D19 (RX1)

Arduino Code:
  1. void setup() {

  2.   Serial.begin(9600); // Open serial communications and wait for port to open
  3.     while (!Serial) {
  4.     ; // wait for serial port to connect. Needed for Leonardo only
  5.   }
  6.   Serial1.begin(115200); // Open serial communications and wait for port to open
  7.     while (!Serial1) {
  8.     ; // wait for serial port to connect. Needed for Leonardo only
  9.   }

  10. }

  11. int readline(int readch, char *buffer, int len) {
  12.   static int pos = 0;
  13.   int rpos;

  14.   if (readch > 0) {
  15.     switch (readch) {
  16.       case '\n': // Ignore new-lines
  17.         break;
  18.       case '\r': // Return on CR
  19.         rpos = pos;
  20.         pos = 0;  // Reset position index ready for next time
  21.         return rpos;
  22.       default:
  23.         if (pos < len-1) {
  24.           buffer[pos++] = readch;
  25.           buffer[pos] = 0;
  26.         }
  27.     }
  28.   }
  29.   // No end of line has been found, so return -1.
  30.   return -1;
  31. }

  32. void loop() {
  33.   
  34.     // Do something here that prints:
  35.     Serial1.print("myButtonIsPressed");
  36.     Serial1.print('\n');
  37.   
  38.   static char buffer[80];
  39.   if (readline(Serial1.read(), buffer, 80) > 0) {
  40.     Serial.print("Serial received: >");
  41.     Serial.print(buffer);
  42.     Serial.println("<");
  43.   }

  44. }
Copy the Code
Banana Python example with PySerial:
  1. import serial
  2. import threading
  3. import time

  4. CRLF = '\r\n'

  5. def log(msg):
  6.     print "[",datetime.datetime.now(), "] ", msg
  7.    
  8. def read_serial():

  9.     global last_received
  10.     buffer = ''
  11.     try:
  12.         ser = serial.Serial('/dev/ttyS2', baudrate=115200, bytesize=serial.EIGHTBITS, stopbits=serial.STOPBITS_ONE, parity=serial.PARITY_NONE, timeout=None)
  13.         log("Serial successfully connected to port %r." % ser.port)
  14.     except Exception as e:
  15.         log("Serial error opening port: " + str(e))
  16.         exit()
  17.     # Reset Arduino
  18.     ser.setDTR(False) # set reset signal
  19.     time.sleep(2)     # wait two seconds - Arduino needs some time to reset
  20.     ser.flushInput()  # toss data already received
  21.     ser.setDTR(True)  # remove reset signal - Arduino will restart
  22.     try:
  23.         while True:
  24.             if '\n' in buffer:
  25.                 pass # skip if a line already in buffer
  26.             else:
  27.                 buffer = buffer + ser.read(1).decode("utf-8")  # this will block until one more char or timeout
  28.             buffer = buffer + ser.read(ser.inWaiting()).decode("utf-8")
  29.             lines = buffer.split('\n') # Guaranteed to have at least 2 entries
  30.             last_received = lines[-2]
  31.             buffer = lines[-1]
  32.             log("Serial received: " + last_received)
  33.             if (last_received == "myButtonIsPressed"):
  34.                 # Do something here if the button on the Arduino is pressed
  35.                 ser.write("I got it!" + CRLF) # Sends back a message to the Arduino and shows it in the Arduino Serial Monitor
  36.             else:
  37.                 log("nothing to work with")
  38.                
  39.     except Exception as e:
  40.         log("Serial error communicating...: " + str(e))
  41.         ser.close()
  42.         raise
  43.     ser.close()
  44.               
  45. serialThread = threading.Thread(target = read_serial)
  46. #serialThread.daemon = True
  47. serialThread.start()
Copy the Code
Thank you!

I made a change in the Python code because the while loop was pushing the CPU to 100% all the time.

If the code is not the best then the reason is that i just started with Python.

Thanks for sharing.

@MrGlasspoole

I would like to see your Python mods code.
Thx

harryweb replied at Tue Nov 4, 2014 10:38
@MrGlasspoole

I would like to see your Python mods code.

What do you mean?
The Python code is in my first posting.

Ok ! I understood that you modified code from first version. Sorry.

suhair  
helloo, i want to send some values from Arduino Mega to Banana pi through Uart. Sending 3 values serialy from 3 serial port of Arduino mega . in banana pi , i received data s through /dev/ttyUSB0, but i connected directly to CON3 of Banana pi , and selected /dev/S0- /dev/S3, but values not receiving.
i changed script.bin for enabling tty . but no output .....


Anyone please help ...  

You have to log in before you can reply Login | Sign Up

Points Rules