Python talks to a tty port... how?

Raymond E. Suorsa (grendel@fen.arc.nasa.gov)
11 Apr 1995 23:18:46 GMT

I would like to replace some old C code with Python. This old C code
opens up a /dev/tty and then sets the parity, stop bits, etc etc...

If you could show me how to write replace these two functions with
some slick Python code I and all my descendants will revere your name
with rituals and moments-of-silence for all time... well, a couple
days at least. :-)

int ttyopen(VideoUnit *V)
{
char command[80];

strcpy(command,"stty 9600 -parenb cs8 -cstopb raw -echo > ");
strcat(command,V->ttyport);

if ((V->handel = open(V->ttyport, O_RDWR)) == -1)
{
printf("ERROR: cannot open %s\n",V->ttyport);
return(1);
}

if (system(command))
{
printf ("\nERROR: The port was NOT initialized.\n");
printf ("Please see that the protection on %s is lifted.\n",V->ttyport);
return(1);
}

/* flush the port */
(void)ioctl(V->handel, TCFLSH, 0);

return(0);
}

ttywrite(VideoUnit *V, char *out_string)
{
int len;


len = strlen(out_string);

if (write(V->handel, out_string, len) != len) {
printf("ERROR in ttywrite: Unable to write to device!\n");
return(1);
}

return(0);
}

--

__o Ray Suorsa \<, grendel@windchime.arc.nasa.gov (pgp capable) ()/ () NASA/Ames:USA (Lab) +1 415-604-6334 (Office) +1 415-604-5451