Created
October 30, 2013 01:38
-
-
Save posborne/7225894 to your computer and use it in GitHub Desktop.
Example showing a simple usage of PTY without use of fork()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _XOPEN_SOURCE 600 | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
#include <string.h> | |
#include <unistd.h> | |
/* | |
* Simple Echo Server that repeats back characters, with a spin | |
*/ | |
int parrot(int master_fd, int iterations) | |
{ | |
int i; | |
char rxbuf[1]; | |
char txbuf[20]; | |
int bytes_read = 0; | |
for (i = 0; i < iterations; i++) { | |
bytes_read = read(master_fd, rxbuf, 1); | |
if (bytes_read == 1) { | |
printf("[Rx]: %c [%02X]\n", rxbuf[0], rxbuf[0]); | |
sprintf(txbuf, "Eh! '%c'\n", rxbuf[0]); | |
write(master_fd, txbuf, strlen(txbuf)); | |
} | |
} | |
return 0; | |
} | |
int main(int argc, char **argv) | |
{ | |
char *slavepath; | |
int pty_master_fd = posix_openpt(O_RDWR); | |
(void)grantpt(pty_master_fd); | |
(void)unlockpt(pty_master_fd); | |
slavepath = ptsname(pty_master_fd); | |
printf("Slave Path: %s\n", slavepath); | |
return parrot(pty_master_fd, 20); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial | |
s = serial.Serial(port="/dev/pts/5", timeout=0.01) | |
s.write("Testing!") | |
print s.read(1024) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment