-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethernet.py
117 lines (101 loc) · 4.02 KB
/
ethernet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Brian Lesko
# Ethernet communication class
import socket
import pandas as pd
class ethernet:
def __init__(self, name, IP, PORT, SUBNETMASK = "MASK"):
self.name = name
self.IP = IP
self.PORT = PORT
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connected = False
def connect(self):
messages = []
try:
self.s.connect((self.IP, self.PORT))
messages.append(f'The connection with *{self.name}* established.')
self.connected = True
except socket.error as e:
messages.append(f'Sorry, the connection with *{self.name}* was not established.')
messages.append(e)
return messages
def is_connected(self):
if self.s.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) == 0:
self.connected = True
else:
self.connected = False
return self.connected
def disconnect(self):
self.s.close()
return f"The connection with *{self.name}* was closed."
def receive(self):
response = self.s.recv(1024) # Max amount of bytes to receive
if response:
return response.decode()
else:
return None
def send_and_receive(self, command):
bytes = command.encode()
self.s.sendall(bytes)
response = self.s.recv(1024) # Max amount of bytes to receive
if response:
return response.decode()
else:
return None
def to_df(self):
df = pd.DataFrame({'name': [self.name], 'ipv4': [self.IP], 'port': [self.PORT]})
return df
# For the keyence instance of an ethernet connection
def prime_print(self,print):
command_2 = 'PR' + ',1' + '\r' # set the program number
command = 'SB' + '\r' # we acquire the system status to check if we are ready to print
command_3 = 'BK' + ',1,0,' + print + '\r' # change the string to print
commands = [command, command_2, command_3]
responses = []
messages = []
for command in commands:
message = f"Sending to *{self.name}*: {command}"
response = self.send_and_receive(command)
message2 = f"The response from *{self.name}* is: "
messages.append(message + "\n" + message2)
responses.append(response)
return responses, messages
@classmethod
def from_excel(cls, excel_file_name):
# To create a list of ethernet objects from an excel file
ethernets = []
data_types = {
'ipv4': str,
'port': int,
'subnetmask': str
}
external_connections = pd.read_excel(excel_file_name, index_col=None)
external_connections = external_connections.astype(data_types)
for i in range(len(external_connections)):
name = external_connections['name'][i]
IP = external_connections['ipv4'][i]
PORT = external_connections['port'][i]
ethernet = cls(name, IP, PORT)
ethernets.append(ethernet)
return ethernets
# WIP - need to test this
def get_local_ip():
# This will only work on Linux
import subprocess
IP= subprocess.run(["hostname", "-I"],stdout=subprocess.PIPE,text=True).stdout
return IP
def get_subnet_mask():
netmask = "255.255.255.0"
return netmask
def check_connection_compatibility(self):
if self.get_local_ip().split('.')[0:3] == self.IP.split('.')[0:3]:
response = 'Your computer and *{self.name}* are ready to connect.'
errors = False
else:
response = 'Your host computer and *{self.name}* cannot communicate.' + "\n" + """
- Are the IP addresses in the same range?
- Is the printer connected on the same ethernet as the computer?
- Do the port and IP specified above match the printer settings?
"""
errors = True
return response, errors