Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reading from serial port & writing to X console

Reply
Thread Tools

Reading from serial port & writing to X console

 
 
lec
Guest
Posts: n/a
 
      07-17-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()




 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      07-17-2003
In article <mailman.1058453987.15227.python->, 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writing to serial port (COMM) Andreas Java 6 11-16-2010 11:49 AM
Reading console output and writing to console jan.rebada@gmail.com C++ 5 03-04-2008 09:00 AM
Preventing the console window from closing when writing a console program in C++ Miktor C++ 7 08-09-2005 09:22 PM
Serial Port programming - Reading DSR from port msalerno Perl Misc 3 07-14-2005 12:58 PM
Can I connect router Serial interface directly to a PC serial port? Faustino Dina Cisco 2 08-18-2004 02:30 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57