Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@posborne
Created October 30, 2013 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save posborne/7225894 to your computer and use it in GitHub Desktop.
Save posborne/7225894 to your computer and use it in GitHub Desktop.
Example showing a simple usage of PTY without use of fork()
#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);
}
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