Skip to content

Instantly share code, notes, and snippets.

@brendanjerwin
Created March 27, 2011 18:57
Show Gist options
  • Save brendanjerwin/889471 to your computer and use it in GitHub Desktop.
Save brendanjerwin/889471 to your computer and use it in GitHub Desktop.
temp hal component
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Brendan Erwin on 2008-05-21.
Copyright (c) 2008 Brendan Erwin. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
import serial
import hal
import sys
import time
PORT = "/dev/ttyUSB0"
#allow the port to be overridden
if len(sys.argv) > 1:
PORT = sys.argv[1]
#Establish serial link
ser = serial.Serial(PORT, 115200, timeout=2)
#Setup the HAL component and pins
c = hal.component("reprap-extruder")
c.newpin("temperature", hal.HAL_FLOAT, hal.HAL_IN)
time.sleep(1)
c.ready()
print >> sys.stdout, "READY"
data = ""
try:
while True:
time.sleep(.5)
ser.write("M105\n")
time.sleep(.5)
data += ser.read()
while ser.inWaiting():
data += ser.read()
if len(data) > 0:
start = data.find("T:")
if start <> -1:
start += 2
end = data.index("\n",start) - 1
print >> sys.stdout, "TEMP: " + data[start:end]
c["temperature"] = float(data[start:end])
data = ""
except KeyboardInterrupt:
raise SystemExit
@brendanjerwin
Copy link
Author

Loaded with

loadusr -W /home/brendanjerwin/emc2/configs/my-mill/reprap-extruder /dev/ttyUSB0

@brendanjerwin
Copy link
Author

When it runs I see:

brendanjerwin@mill:~/emc2/configs/my-mill$ halrun
halcmd: loadusr -W /home/brendanjerwin/emc2/configs/my-mill/reprap-extruder /dev/ttyUSB0
READY
Waiting for component '/home/brendanjerwin/emc2/configs/my-mill/reprap-extruder' to become     ready...TEMP: 73.00
..........TEMP: 16.00
..........TEMP: 16.00
..........TEMP: 16.00
.........^C
<stdin>:1: /home/brendanjerwin/emc2/configs/my-mill/reprap-extruder exited without becoming ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment