Skip to content

Commit

Permalink
started developing the arduino serial interface for the onosCenter
Browse files Browse the repository at this point in the history
  • Loading branch information
marco committed Aug 17, 2016
1 parent 81eebe9 commit 7a4bf9f
Show file tree
Hide file tree
Showing 10 changed files with 1,015 additions and 21 deletions.
405 changes: 405 additions & 0 deletions arduino_code/pcduino2_3.pde

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions onos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. #

cd scripts_folder
ifup lan
ifup wan
while [ true ]

do nice -n -16 python time_zone.py
Expand Down
43 changes: 25 additions & 18 deletions scripts_folder/arduino_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ def __init__(self):



def grepPort(self,port_to_search,port_exluded):
result=os.popen("ls /dev/ | grep -v "+port_exluded+" | grep "+port_to_search).read()

return (result)
def verifyPort(self,port_to_search,port_exluded):
if port_to_search!=port_exluded:
result=os.path.os.path.exists("/dev/"+port_to_search)
else:
return("no")

#result=os.popen("ls /dev/ | grep -v "+port_exluded+" | grep "+port_to_search).read()
if result:
return (port_to_search)
else
return ("no")



Expand All @@ -52,8 +59,8 @@ def connectToPort(self):
port=self.searchForSerialCable("nothing")
if port!="null": # if i found the port then use it
try:
old_port=port[0:len(port)-1]
port='/dev/'+port[0:len(port)-1] #remove /n of ls
old_port=port # [0:len(port)-1]
port='/dev/'+port # [0:len(port)-1] #remove /n of ls
self.ser =arduinoserial.SerialPort(port, 115200)
print "arduino connected corectly to onos system"
return(1)
Expand All @@ -63,7 +70,7 @@ def connectToPort(self):
if port!="null": # if i found the port then use it
try:
old_port=port[0:len(port)-1]
port='/dev/'+port[0:len(port)-1] #remove /n of ls
port='/dev/'+port #[0:len(port)-1] #remove /n of ls
self.ser =arduinoserial.SerialPort(port, 115200)
print "arduino connected corectly to onos system"
return(1)
Expand All @@ -81,28 +88,28 @@ def connectToPort(self):

def searchForSerialCable(self,exluded_port):

port=self.grepPort("ttyUSB0",exluded_port)
port=self.verifyPort("ttyUSB0",exluded_port)
if len (port)<3:
port=self.grepPort("ttyUSB1",exluded_port)
port=self.verifyPort("ttyUSB1",exluded_port)
if len (port)<3:
port=self.grepPort("ttyUSB2",exluded_port)
port=self.verifyPort("ttyUSB2",exluded_port)
if len (port)<3:
port=self.grepPort("ttyUSB3",exluded_port)
port=self.verifyPort("ttyUSB3",exluded_port)
if len (port)<3:
port=self.grepPort("ttyUSB4",exluded_port)
port=self.verifyPort("ttyUSB4",exluded_port)
if len (port)<3:
port=self.grepPort("ttyUSB",exluded_port)
port=self.verifyPort("ttyUSB",exluded_port)

if len (port)<3:
port=self.grepPort("ttyACM0",exluded_port)
port=self.verifyPort("ttyACM0",exluded_port)
if len (port)<3:
port=self.grepPort("ttyACM1",exluded_port)
port=self.verifyPort("ttyACM1",exluded_port)
if len (port)<3:
port=self.grepPort("ttyACM2",exluded_port)
port=self.verifyPort("ttyACM2",exluded_port)
if len (port)<3:
port=self.grepPort("ttyACM3",exluded_port)
port=self.verifyPort("ttyACM3",exluded_port)
if len (port)<3:
port=self.grepPort("ttyACM",exluded_port)
port=self.verifyPort("ttyACM",exluded_port)
if len (port)<3:
return("null")

Expand Down
23 changes: 22 additions & 1 deletion scripts_folder/router_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,33 @@ def read_router_pins(self): # thread function to read changing of pin status

def composeChangeNodeOutputPinStatusQuery(self,pinNumbers,node_obj,objName,status_to_set,node_serial_number,out_type,user,priority,mail_report_list) :

"""
| Compose the correct query to change an output pin status on a remote node.
| The examples are:
|
| "onos_r"+pin0+pin1+"v"+status_to_set+"s"+numeric_serial_number+"_#]" to set a sr_relay to status_to_set (0 or 1)
| onos_r0607v1s0001_#] pin6 and 7 used to control a s/r relay and turn it to set
|
| "onos_d"+pin_number+"v"+"00"+status_to_set+"s"+numeric_serial_number+"_#]" to set a digital pin to 0 or 1
| onos_d07v001s0001_#] set digital pin7 to 1
|
| "onos_s"+pin_number+"v"+status_to_set+"s"+numeric_serial_number+"_#]" to set a servopin from 0 to 180
| onos_s07v180s0001_#] set servo pin7 to 180
|
| "onos_a"+pin_number+"v"+status_to_set+"s"+numeric_serial_number+"_#]" to set a analogpin from 0 to 255
| onos_a05v100s0001_#] set analog pin5 to 100
|
| The numeric_serial_number is a hexadecimal number refering to the node serial number
"""

print "composeChangeNodeOutputPinStatusQuery() executed"

numeric_serial_number=node_serial_number[-4:] # example get 0001 from "ProminiA0001"
address=node_obj.getNodeAddress()
base_query='' #' ''http://'''+address+''':'''+str(node_webserver_port)
base_query='' #' ''http://'''+address+''':'''+str(node_webserver_port) not used anymore
if (out_type=="sr_relay"):
pin1=str(pinNumbers[0])
pin0=str(pinNumbers[1])
Expand Down
181 changes: 181 additions & 0 deletions tests/arduinoserial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#!/usr/bin/env python
#
# Copyright 2007 John Wiseman <jjwiseman@yahoo.com>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
A port of Tod E. Kurt's arduino-serial.c.
<http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/>
"""

import termios
import fcntl
import os
import sys
import time
import getopt


# Map from the numbers to the termios constants (which are pretty much
# the same numbers).

BPS_SYMS = {
4800: termios.B4800,
9600: termios.B9600,
19200: termios.B19200,
38400: termios.B38400,
57600: termios.B57600,
115200: termios.B115200
}


# Indices into the termios tuple.

IFLAG = 0
OFLAG = 1
CFLAG = 2
LFLAG = 3
ISPEED = 4
OSPEED = 5
CC = 6


def bps_to_termios_sym(bps):
return BPS_SYMS[bps]


class SerialPort:

def __init__(self, serialport, bps):
"""Takes the string name of the serial port
(e.g. "/dev/tty.usbserial","COM1") and a baud rate (bps) and
connects to that port at that speed and 8N1. Opens the port in
fully raw mode so you can send binary data.
"""
self.port=serialport
self.fd = os.open(serialport, os.O_RDWR | os.O_NOCTTY | os.O_NDELAY)
attrs = termios.tcgetattr(self.fd)
bps_sym = bps_to_termios_sym(bps)
# Set I/O speed.
attrs[ISPEED] = bps_sym
attrs[OSPEED] = bps_sym

# 8N1
attrs[CFLAG] &= ~termios.PARENB
attrs[CFLAG] &= ~termios.CSTOPB
attrs[CFLAG] &= ~termios.CSIZE
attrs[CFLAG] |= termios.CS8
# No flow control
attrs[CFLAG] &= ~termios.CRTSCTS

# Turn on READ & ignore contrll lines.
attrs[CFLAG] |= termios.CREAD | termios.CLOCAL
# Turn off software flow control.
attrs[IFLAG] &= ~(termios.IXON | termios.IXOFF | termios.IXANY)

# Make raw.
attrs[LFLAG] &= ~(termios.ICANON | termios.ECHO | termios.ECHOE | termios.ISIG)
attrs[OFLAG] &= ~termios.OPOST

# It's complicated--See
# http://unixwiz.net/techtips/termios-vmin-vtime.html
attrs[CC][termios.VMIN] = 0;
attrs[CC][termios.VTIME] = 20;
termios.tcsetattr(self.fd, termios.TCSANOW, attrs)
os.system("cat "+self.port)
def read_until(self, until):
#print "called serial.read_until"
buf = ""
done = False
while not done:
n = os.read(self.fd, 1)
if n == '':
# FIXME: Maybe worth blocking instead of busy-looping?
time.sleep(0.01)
continue
buf = buf + n
if n == until:
done = True
os.close(n)
return buf



def read(self, size):
#print "called serial.read"
buf = ""

#buf= os.read(self.fd, 1)
buf=os.popen("cat < "+self.port).read()

print "input buffer="+buf
#os.close(n)
return buf


def read1(self, size):
#print "called serial.read"
buf = ""
done = False
while not done:
n = os.read(self.fd, 1)
if n == '':
# FIXME: Maybe worth blocking instead of busy-looping?
time.sleep(0.01)
continue
buf = buf + n
if len(buf) == size:
done = True
return buf
os.close(n)
return buf


def write(self, str):
#os.write(self.fd, str)
os.system("echo "+str+" >> "+self.port)

def write_byte(self, byte):
os.write(self.fd, chr(byte))

def isOpen(self):
print " called isOpen"
return(1)

#def open():
# return(1)


# def __del__(self):
# print "class arduinoserial destroyed"
# try:
# os.close(self.fd)
# except:
# print "tried to close serial port"
def close(self):
print "class arduinoserial destroyed"
try:
os.close(self.fd)
except:
print "tried to close serial port"


Binary file added tests/arduinoserial.pyc
Binary file not shown.

0 comments on commit 7a4bf9f

Please sign in to comment.