By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,213 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,213 IT Pros & Developers. It's quick & easy.

Reading from serial port & writing to X console

P: n/a
lec
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()


Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
In article <ma**********************************@python.org >, lec wrote:
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".


No. You can simply read /dev/ttyS0 and write to /dev/tty7:

In shell:

$ (stty 9600 -paren -ixon -ixoff; cat) </dev/ttyS0 >/dev/tty7

Change the stty parameters as desired.

In Python, just open /dev/ttyS0, /dev/tty7. Read from one and
write to the other.

There are a couple different serial port modules that wrap up
serial ports into objects to hide the nasty termios stuff.

Google the group for terms like PosixSeral PySerial.

--
Grant Edwards grante Yow! Why was I BORN?
at
visi.com
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.