This past week I have been completing the code to finalize the project The MTrim RS-422 serial interface needed to be available from within the DF1 RS-232RS-232 interface object since the bits in the PLC that are being read by the DF1 object actually trigger the sending of information across the RS-422 serial interface to the MTrim
The final code for the serial communications can be found at: https://github.com/frellwan/SciFy-Pi.git in the serial/DF1 folder.
Code to make the MTrim Accessible from within the DF1 class:
class DF1ClientProtocol(SerialClientProtocol): def __init__(self, logger, ftpEndpoint, mtrimSerial): ''' Initializes our custom protocol :param logger: The local file to store results :param ftpEndpoint: The endpoint to send results to and read recipes from ''' SerialClientProtocol.__init__(self) self.logger = logger self.ftpEndpoint = ftpEndpoint self.mtrimSerial = mtrimSerial self.logFile = logger.getFileName() self.ENQCount = 0 self.lcOEE = LoopingCall(self.startOEEData) self.lcAlarms = LoopingCall(self.startAlarmsData) self.lcFTP = LoopingCall(self.startFTPTransfer) self._reconnecting = False self.config = utilities.optionReader() fLogFile = logfile.LogFile('df1comms.log', '/home/pi/projects/newSLC/logs', maxRotatedFiles=2) fLogObserver = log.FileLogObserver(fLogFile) log.startLogging(logfile.LogFile('df1comms.log', '/home/pi/projects/newSLC/logs', maxRotatedFiles=2)) #log.startLogging(sys.stdout) self.notified = False self.transferred = False self.loaded = False
Then to change parameters when the recipe is changed:
def sendRecipe(recipe): PLCRecipe = self.config.getPLCRecipe() result = self.mtrimSerial.writeParameter(1, 1, float(recipe[-3])) result.addErrback(self.errorHandler, "sendRecipe") result = self.mtrimSerial.writeParameter(1, 20, float(recipe[-2])) result.addErrback(self.errorHandler, "sendRecipe") result = self.mtrimSerial.writeParameter(1, 21, float(recipe[-1])) result.addErrback(self.errorHandler, "sendRecipe") index = 1 # Index 0 is recipe name var = [] for address in PLCRecipe: request = protectedWriteRequest(1, address, [float(recipe[index])]) result = self.sendRequest(request) result.addErrback(self.errorHandler, "sendRecipe") var.append(result) d = defer.gatherResults(var) d.addCallback(clearRecipeBit) d.addErrback(self.errorHandler, 'saving data in StartOEEData failed')
I also need the PiFace code and the serial code to run when the RPi is booted up.
I tried following the istructions found here, but I could not get it to work. I was able to use the basic concept outlined, but had to make changes to the rc.local file in the /etc/init.d directory. When I did that it worked fine.
So to enable the PiFace code and the serial code I created a file - launcher.sh - that looks like this:
#!/bin/sh # launcher.sh cd /home/pi/projects cd /home/pi/projects/PiFace python3 lcdSM.py & cd /home/pi/projects/newSLC python df1.py &
The I used the command:
sudo nano /etc/rc.local
to make the file look like this:
#!/bin/sh ### BEGIN INIT INFO # Provides: rc.local # Required-Start: $network $remote_fs $syslog # Required-Stop: $network $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: # Short-Description: Start df1comms daemon ### END INIT INFO # Install Script Info (sleep 10; sudo /home/pi/projects/launcher.sh)&
Everything is working very well. I will put some videos together to show here shortly.