Portál AbcLinuxu, 7. května 2025 17:03
echo ahoj > /dev/ttyS2
tak potom jinde v konsoli dam cat /dev/ttyS2
. Ale ten cat vypisuje porad dokola prazdne radky a obcas se tam objevi to ahoj i kdyz jsem ho poslal jen jednou. Myslim si ze mam neco blbe v kernelu ale netusim co. Nevite nekdo co s tim? Dik Vojta
cat /dev/ttyS2
se zapnou vsechny ale ja bych je chtel zapnout zvlast. Diky Vojta
#include <sys/ioctl.h> #include <fcntl.h> #include <unistd.h> int main(int argc, char **argv) { int fd; int iFlags = TIOCM_DTR; fd = open("/dev/ttyS0", O_RDWR | O_SYNC); ioctl(fd, TIOCMBIC, &iFlags); /* Vynuluje DTR */ ioctl(fd, TIOCMBIS, &iFlags); /* Nastavi DTR */ close(fd); return 0; }
usage: setdtrrts.py [options] options: --version show program's version number and exit -h, --help show this help message and exit -d DEVICE, --device=DEVICE serial port device (default /dev/ttyS0) -R, --RTS set RTS -D, --DTR set DTR -t DELAY, --time=DELAY time in seconds to quit (default 5)Takže například
setdtrrts.py -d /dev/ttyS1 --RTS --DTR -t 3Zapne obě stavové linky u portu /dev/ttyS1 na dobu tří sekund. A tady je ten program:
#!/usr/bin/env python import tty, os, fcntl, time, struct from optparse import OptionParser usage = "usage: %prog [options]" parser = OptionParser(usage, version="%prog version 0.1") parser.add_option("-d", "--device", action = "store", default = "/dev/ttyS0", dest="device", help="serial port device (default /dev/ttyS0)") parser.add_option("-R", "--RTS", action = "store_true", default = False, dest="rts", help="set RTS") parser.add_option("-D", "--DTR", action = "store_true", default = False, dest="dtr", help="set DTR") parser.add_option("-t", "--time", action = "store", type = "float", default = 5, dest="delay", help="time in seconds to quit (default 5)") (options, args) = parser.parse_args() TIOCM_DTR_str = struct.pack('I', tty.TIOCM_DTR) TIOCM_RTS_str = struct.pack('I', tty.TIOCM_RTS) fd = os.open(options.device, os.O_RDWR | os.O_SYNC) if options.dtr: print "setting DTR" fcntl.ioctl(fd, tty.TIOCMBIS, TIOCM_DTR_str) else: print "clearing DTR" fcntl.ioctl(fd, tty.TIOCMBIC, TIOCM_DTR_str) if options.rts: print "setting RTS" fcntl.ioctl(fd, tty.TIOCMBIS, TIOCM_RTS_str) else: print "clearing RTS" fcntl.ioctl(fd, tty.TIOCMBIC, TIOCM_RTS_str) time.sleep(options.delay)... a měl bys začít programovat v nějakém kloudném programovacím jazyce, který není odkázaný na volání externích programů.
#!/usr/bin/env python import tty, os, fcntl, time, struct, subprocess programname = 'date' delay = 3 TIOCM_DTR_str = struct.pack('I', tty.TIOCM_DTR) TIOCM_RTS_str = struct.pack('I', tty.TIOCM_RTS) fd = os.open('/dev/ttyS0', os.O_RDWR | os.O_SYNC) while True: print "setting DTR" fcntl.ioctl(fd, tty.TIOCMBIS, TIOCM_DTR_str) print "setting RTS" fcntl.ioctl(fd, tty.TIOCMBIS, TIOCM_RTS_str) subprocess.Popen([programname]) time.sleep(delay) print "clearing DTR" fcntl.ioctl(fd, tty.TIOCMBIC, TIOCM_DTR_str) print "clearing RTS" fcntl.ioctl(fd, tty.TIOCMBIC, TIOCM_RTS_str) subprocess.Popen([programname]) time.sleep(delay)
#!/usr/bin/env python import tty, os, fcntl, time, struct, subprocess def runcommand(command): process = subprocess.Popen([command]) process.wait() def wait_period(period): sec = time.time() time_to_wait = period - sec % period print "Waiting for %g seconds" %time_to_wait time.sleep(time_to_wait) class Relay: def __init__(self, devpath, initstate = True): self.fd = os.open(devpath, os.O_RDWR | os.O_SYNC) self.TIOCM_DTR_str = struct.pack('I', tty.TIOCM_DTR) self.TIOCM_RTS_str = struct.pack('I', tty.TIOCM_RTS) self.state = initstate self.switch(self.state) def switch(self, state): if state: print "setting DTR" fcntl.ioctl(self.fd, tty.TIOCMBIS, self.TIOCM_DTR_str) print "setting RTS" fcntl.ioctl(self.fd, tty.TIOCMBIS, self.TIOCM_RTS_str) else: print "clearing DTR" fcntl.ioctl(self.fd, tty.TIOCMBIC, self.TIOCM_DTR_str) print "clearing RTS" fcntl.ioctl(self.fd, tty.TIOCMBIC, self.TIOCM_RTS_str) self.state = state def negate(self): self.state = not self.state #negate self.switch(self.state) rele = Relay('/dev/ttyS0', initstate = False) while True: wait_period(3) rele.negate() runcommand('date')Je to trochu lepší verze než ta předchozí. Jednak je to objektové a pak to časování je udělané tak, že se systematicky nepředbíhá ani nezpožďuje. Navíc se čas počítá absolutně, takže lze předem říct, že se kamery budou přepínat vždy v celou minutu a v půl.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.