Reading from serial port & writing to X console

lec englim at pc.jaring.my
Thu Jul 17 10:58:41 EDT 2003


Hi,

I'm trying to write a program to read from the serial port & write
whatever that is read to the X console (/dev/tty7).
For X to recognize the characters sent, I believe you have to send
"scancodes".

Any suggestion is appreciated.

This is my attempt which doesn't work:

#!/usr/bin/python

scancodes = 
[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0x1c,0x9c,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0x39,0xb9,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0, 
0x0b, 0x8b, 0],[0, 2, 130, 0],[0, 3, 131, 0],[0, 4, 132, 0],[0, 5, 133, 
0],[0, 6, 134, 0],[0, 7, 135, 0],[0, 8, 136, 0],[0, 9, 137, 0],[0, 10, 
138, 
0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0x2A,0x1E,0x9E,0xAA],[0x2A,0x30,0xb0,0xAA],[0x2A,0x2e,0xae,0xAA],[0x2A,0x20,0xa0,0xAA],[0x2A,0x12,0x92,0xAA],[0x2A,0x21,0xa1,0xAA],[0x2A,0x22,0xa2,0xAA],[0x2A,0x23,0xa3,0xAA],[0x2A,0x17,0x97,0xAA],[0x2A,0x24,0xa4,0xAA],[0x2A,0x25,0xa5,0xAA],[0x2A,0x26,0xa6,0xAA],[0x2A,0x32,0xb2,0xAA],[0x2A,0x31,0xb1,0xAA],[0x2A,0x18,0x98,0xAA],[0x2A,0x19,0x99,0xAA],[0x2A,0x10,0x90,0xAA],[0x2A,0x13,0x93,0xAA],[0x2A,0x1f,0x9f,0xAA],[0x2A,0x14,0x94,0xAA],[0x2A,0x16,0x96,0xAA],[0x2A,0x2f,0xaf,0xAA],[0x2A,0x11,0x91,0xAA],[0x2A,0x2d,0xad,0xAA],[0x2A,0x15,0x95,0xAA],[0x2A,0x2c,0xac,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0x1E,0x9E,0],[0,0x30,0xb0,0],[0,0x2e,0xae,0],[0,0x20,0xa0,0],[0,0x12,0x92,0],[0,0x21,0xa1,0],[0,0x22,0xa2,0],[0,0x23,0xa3,0],[0,0x17,0x97,0],[0,0x24,0xa4,0],[0,0x25,0xa5,0],[0,0x26,0xa6,0],[0,0x32,0xb2,0],[0,0x31,0xb1,0],[0,0x18,0x98,0],[0,0x19,0x99,0],[0,0x10,0x90,0],[0,0x13,0x93,0],[0,0x1f,0x9f,0],[0,0x14,0x94,0],[0,0x16,0x96,0],[0,0x2f,0xaf,0],[0,0x11,0x91,0],[0,0x2d,0xad,0],[0,0x15,0x95,0],[0,0x2c,0xac,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]


def readserial(dev='/dev/ttyS0'):
    p = open(dev)
    line = ''
    line = char = ''
    while char <> chr(13):
        char = p.read(1)
        if char <> '':
            line = line + char
    print line

    # write to /dev/tty7 which is console for X
    tty7 = open('/dev/tty7', 'w')
    #import FCNTL, fcntl
    #import termios, TERMIOS

    for ch in line:
        for ch1 in scancodes[ ord(ch)]:
            if ch1:
                tty7.write("%c"%chr(ch1))

    tty7.close()
    p.close()
   


if __name__ == '__main__':

    while 1:
        readserial()








More information about the Python-list mailing list