Skip to content

Instantly share code, notes, and snippets.

@Justasic
Created May 16, 2018 00:53
Show Gist options
  • Save Justasic/58ba46854360bdae21e7cb10b6e49418 to your computer and use it in GitHub Desktop.
Save Justasic/58ba46854360bdae21e7cb10b6e49418 to your computer and use it in GitHub Desktop.
A dumb program to interpret the serial protocol for my Simplex fire panel
/*
* Main.cpp
*
* Created on: 9-Jan-2009
* Author: root
*/
/////////////////////////////////////////////////
// Serial port interface program //
/////////////////////////////////////////////////
#include <stdio.h> // standard input / output functions
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitionss
#include <time.h> // time calls
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <vector>
#include <sys/socket.h>
#include <sys/epoll.h>
typedef struct epoll_event epoll_t;
int open_port(void)
{
int fd = -1; // file description for the serial port
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
printf("Opened socket %d\n", fd);
if(fd == -1) // if open is unsucessful
{
//perror("open_port: Unable to open /dev/ttyS0 - ");
printf("open_port: Unable to open /dev/ttyS0. \n");
}
else
{
//fcntl(fd, F_SETFL, 0);
printf("port is open.\n");
}
return(fd);
} //open_port
int configure_port(int fd) // configure the port
{
printf("Setting baud rates and shit\n");
struct termios port_settings; // structure to store the port settings in
bzero(&port_settings, sizeof(struct termios));
cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);
//port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
//port_settings.c_cflag &= ~CSTOPB;
//port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
//port_settings.c_cflag &= ~CRTSCTS;
port_settings.c_cc[VMIN] = 1; // no flow control
// port_settings.c_cc[VTIME] = 5; // 0.5 seconds read timeout
port_settings.c_cflag |= CLOCAL;//CREAD | CLOCAL;
//cfmakeraw(&port_settings);
//tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
return(fd);
} //configure_port
typedef union {
char a[4];
int32_t b;
} char2int_t;
int query_modem(int fd) // query modem with an AT command
{
char buf[1024] = {0};
// check if an error has occured
size_t len = read(fd, buf, sizeof(buf));
//printf("Errno: %s (%d)\n", strerror(errno), errno);
if (len == -1U)
{
perror("read()");
return 0;
}
if (len > 0)
{
char c = buf[0];
printf("0x%-8X (%c)\n", c, c);
//fwrite(buf, len, 1, stdout);
}
return 0;
} //query_modem
std::vector<epoll_t> events;
int main(void)
{
printf("Hello!\n");
int fd = open_port();
printf("Configuring port...\n");
fd = configure_port(fd);
events.resize(10);
int epollfd = epoll_create(9001);
if (epollfd == -1)
{
perror("epoll_create");
return EXIT_FAILURE;
}
printf("Opened EPoll socket %d\n", epollfd);
epoll_t ev;
bzero(&ev, sizeof(epoll_t));
ev.events = EPOLLIN;
ev.data.fd = fd;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ev.data.fd, &ev) == -1)
{
perror("epoll_ctl");
return EXIT_FAILURE;
}
printf("Entering event loop.\n");
while(true)
{
int total = epoll_wait(epollfd, &events.front(), events.capacity(), 3000);
if (total == -1)
{
if (errno != EINTR)
{
perror("epoll_wait");
return EXIT_FAILURE;
}
}
for(int i = 0; i < total; ++i)
{
epoll_t &ev = events[i];
if (ev.events & (EPOLLHUP | EPOLLERR))
{
perror("epoll_wait");
if (ev.events & EPOLLERR)
{
int error = 0;
socklen_t errlen = sizeof(error);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == 0)
{
printf("EPoll error: %s (%d)\n", strerror(error), error);
return EXIT_FAILURE;
}
}
else if (ev.events & EPOLLHUP)
printf("Epoll sent EPOLLHUP\n");
else
printf("idfk\n");
}
if (ev.events & EPOLLIN)
{
query_modem(fd);
}
if (ev.events & EPOLLOUT)
{
printf("We're writing to the socket???\n");
}
}
}
close(fd);
close(epollfd);
return 0;
} //main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment