I have an uno connected directly to a raspberry pi with a usb cable, I wish to send from the pi this line....
/ /c#004400GREEN /c#7E0000RED/c#000D7DBLUE /f9/c#004400/.GREEN /c#7E0000 /c#7E0000
which is written on a file on the pi located here...
/home/pi/Desktop/SerPython/read_it.txt
I am having some partial success with this python script I obtained elewhere on this forum, this script deilvers via serial to the uno my line..
/ /c#004400GREEN /c#7E0000RED/c#000D7DBLUE /f9/c#004400/.GREEN /c#7E0000 /c#7E0000
but isnt reading it from my file location.
The second block of code below can read from the file location but doesnt send it out to serial so what I,m asking is can these two scripts be merged to achieve my read from file and output to uno ambition.
I am using python version 3.7
Any help would be greatly appreciated.
#=====================================
# Function Definitions
#=====================================
def sendToArduino(sendStr):
ser.write(sendStr.encode('utf-8')) # change for Python3
#======================================
def recvFromArduino():
global startMarker, endMarker
ck = ""
x = "z" # any value that is not an end- or startMarker
byteCount = -1 # to allow for the fact that the last increment will be one too many
# wait for the start character
while ord(x) != startMarker:
x = ser.read()
# save data until the end marker is found
while ord(x) != endMarker:
if ord(x) != startMarker:
ck = ck + x.decode("utf-8") # change for Python3
byteCount += 1
x = ser.read()
return(ck)
#============================
def waitForArduino():
# wait until the Arduino sends 'Arduino Ready' - allows time for Arduino reset
# it also ensures that any bytes left over from a previous message are discarded
global startMarker, endMarker
msg = ""
while msg.find("Arduino is ready") == -1:
while ser.inWaiting() == 0:
pass
msg = recvFromArduino()
print (msg) # python3 requires parenthesis
print ()
#======================================
def runTest(td):
numLoops = len(td)
waitingForReply = False
n = 0
while n < numLoops:
teststr = td[n]
if waitingForReply == False:
sendToArduino(teststr)
print ("Sent from PC -- LOOP NUM " + str(n) + " TEST STR " + teststr)
waitingForReply = True
if waitingForReply == True:
while ser.inWaiting() == 0:
pass
dataRecvd = recvFromArduino()
print ("Reply Received " + dataRecvd)
n += 1
waitingForReply = False
print ("===========")
time.sleep(5)
#======================================
# THE DEMO PROGRAM STARTS HERE
#======================================
import serial
import time
print ()
print ()
# NOTE the user must ensure that the serial port and baudrate are correct
# serPort = "/dev/ttyS80"
serPort = "/dev/ttyUSB0"
baudRate = 9600
ser = serial.Serial(serPort, baudRate)
print ("Serial port " + serPort + " opened Baudrate " + str(baudRate))
startMarker = 60
endMarker = 62
waitForArduino()
testData = []
#testData.append("<LED1,200,0.2>")
#testData.append("<LED1,800,0.7>")
testData.append(" /c#004400GREEN /c#7E0000RED/c#000D7DBLUE /f9/c#004400/GREEN /c#7E0000/bRED/c#000D7DBLUE /c#7E0000/bRED/c#000D7DBLUE/c#000000..")
#testData.append("<LED2,200,0.2>")
#testData.append("<LED1,200,0.7>")
runTest(testData)
ser.close
import serial
import time
print ()
print ()
# NOTE the user must ensure that the serial port and baudrate are correct
# serPort = "/dev/ttyS80"
serPort = "/dev/ttyUSB0"
baudRate = 9600
ser = serial.Serial(serPort, baudRate)
#print ("Serial port " + serPort + " opened Baudrate " + str(baudRate))
print("/ /c#004400GREEN /c#7E0000RED/c#000D7DBLUE /f9/c#004400/.GREEN /c#7E0000/bRED/c#000D7DBLUE /c#7E0000/bRED/c#000D7DBLUE/c#000000..")
text_file = open("/home/pi/Desktop/SerPython/read_it.txt", "r")
text_file.seek(0)
print(text_file.read())
#print ("Output of Read function is ")
print (text_file.read())
print
text_file.seek(0)
#print ("Output of Readline function is ")
print (text_file.readline())
print
text_file.seek(0)
print ("Output of Readline(9) function is ")
print (text_file.readline(9))
text_file.seek(0)
# readlines function
print ("Output of Readlines function is ")
print (text_file.readlines())
print
ser.write("This is my second arduino message\n")
text_file.close()