Hello all,
I'm trying to do Ethernet socket messaging communication between an Arduino and a FANUC Robot.
I've gotten this application to work with Python application in the past. I'm struggling rewriting it to Arduino code. The examples provided in the playground aren't great for what I'm trying to accomplish.
It's a very simple application that sends data through a socket in an Identifier+Value format.
If anyone can help point me in the right direction it would be greatly appreciated!
Here is my Python connecting code:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(('', 2000))
except socket.error as e:
print(str(e))
s.listen(5)
print('waiting for a connection...')
conn, addr = s.accept()
print('connected to:'+addr[0]+':'+str(addr[1]))
Here is my Python application code:
# DATA TAGS
# 1 = OVERRIDE VALUE
# 2 = ABORT ALL
# 3 = FAULT RESET
# 4 = CYCLE START
# 5 = PROGRAM NAME
# 6 = GET PROGRAM NAMES
# 7 = ...
# Package and send Override data
packet = struct.pack('!ii', 1, OverRideValue)
conn.send(packet)
# Package and send AbortAll data
packet = struct.pack('!ii', 2, AbortAll)
conn.send(packet)
AbortAll = 0
# Package and send Fault Reset data
packet = struct.pack('!ii', 3, Reset)
conn.send(packet)
Reset = 0
# Package and send Program selection data (Format (integer(datatag), integer(stringlength), string)
INTproglen = len(Prog)
STRproglen = str(INTproglen)
RawData = str.encode(Prog)
packet = struct.pack('!ii'+STRproglen+'s', 5, INTproglen, RawData)
conn.send(packet)
Reset = 0
# Package and send Cycle Start data
packet = struct.pack('!ii', 4, CycleStart)
conn.send(packet)
CycleStart = 0
# Package and send 'Get Program list' command data
packet = struct.pack('!ii', 6, GetProgs)
conn.send(packet)
GetProgs = 0
sendchange = 0
def receiver():
global s
global GetProgs
while True:
time.sleep(.25)
datatag = conn.recv(4) # We are expecting a 4 byte data tag from the robot, the value of this will determine what to do with the data
received = GetVal(datatag)
if received == 10: # Fault light status
temp = conn.recv(4)
fault_stat = GetVal(temp)
fault_stat = int(fault_stat)
# print(fault_stat)
if fault_stat == 1:
Faulted_Label.configure(bg='red')
Faulted_Label.configure(text='FAULTED')
else:
Faulted_Label.configure(bg='green')
Faulted_Label.configure(text='No Faults')
elif received == 11:
temp = conn.recv(4)
TP_Stat = GetVal(temp)
TP_Stat = int(TP_Stat)
# print(TP_Stat)
if TP_Stat == 1:
TP_Label.configure(bg='red')
TP_Label.configure(text='TP Enabled')
else:
TP_Label.configure(bg='green')
TP_Label.configure(text='TP Off')
elif received == 12:
temp = conn.recv(4)
Key_Stat = GetVal(temp)
Key_Stat = int(Key_Stat)
# print(Key_Stat)
if Key_Stat == 1:
Key_Label.configure(bg='green')
Key_Label.configure(text='In Auto')
else:
Key_Label.configure(bg='red')
Key_Label.configure(text='NOT in Auto')
elif received == 13:
temp = conn.recv(4)
Step_Stat = GetVal(temp)
Step_Stat = int(Step_Stat)
# print(Step_Stat)
if Step_Stat == 1:
Step_Label.configure(bg='red')
Step_Label.configure(text='Step Enabled')
else:
Step_Label.configure(bg='green')
Step_Label.configure(text='Step Disabled')
time.sleep(.25)
elif received == 15: # Here's where things get tricky, we are receiving a sequence of TP Programs.
temp = conn.recv(4) # Here is where we read how many files to get.
filecount = GetVal(temp)
filecount = int(filecount)
ProgramSelect.delete(0, END)
i = 0
while filecount != i:
i = i + 1 # Increment counter
temp = conn.recv(4)
strlength = GetVal(temp)
strlength = int(strlength)
# print(strlength)
Prog = conn.recv(strlength)
# print(Prog)
ProgramSelect.insert(i, Prog)
def GetVal(datatag):
temp = struct.unpack('!i', datatag)
temp = temp[0]
return temp
# print(temp)