How to bridge RS232 to TCP/IP

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "argtable.h"
 
#define PORT 9034
 
fd_set master;
fd_set read_fds;
int fdmax;
struct sockaddr_in myaddr;
struct sockaddr_in remoteaddr;
int listener;
int fd_serial;
char buf[256];
int nbytes;
int current_connection;
 
 
/* Speed not implemented yet as an argument.  Default id 9600 */
 
int open_serialdev(char *serial_dev, int serial_speed)
{
    int fd;                /* Port File Descriptor */
    struct termios options;        /* Structure for setting port options */
 
    fd = open(serial_dev, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1)
    {
        /* Could not open the port */
        perror("open_port: Unable to open Serial Device");
    }
    else {
        fcntl(fd, F_SETFL, FNDELAY);
    }
 
    /* Get the current port options */
 
    tcgetattr(fd, &options);
 
    /* Set the Baud Rate */
 
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);
 
    /* Set the receiver and set local mode and mode of port to 81N */
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_iflag |= INLCR;
    options.c_oflag &= ~OPOST;
 
    /* Set the new options */
 
    tcsetattr(fd, TCSANOW, &options);
 
    return (fd);
}
 
void process_serial_send(void)
{
    if(write(fd_serial, buf, nbytes) == -1) {
        perror("process_serial_send: Serial Send Failed");
    }
 
}
 
void process_socket_send(localecho)
int localecho;
{
    int j;
 
    /* Loop through active sockets and send what has been keyed
     * This also echos what was keyed back to the originating socket if localecho is turned on */
 
    for(j=0; j<= fdmax; j++) {
        if (FD_ISSET(j, &master)) {
            if(j != listener && j != fd_serial && j != current_connection) {
                if (send(j, buf, nbytes, 0) == -1) {
                    perror("process_socket_send: Client Send Failed");
                }
            }
        }
    }
    if(localecho == 1 && current_connection != fd_serial){
        if (send(current_connection, buf, nbytes, 0) == -1) {
            perror("process_socket_send: Client Send Failed");
        }
    }
}
 
void process_new_connection(int debug)
{
    int addrlen;
    int newfd;
 
    addrlen = sizeof(remoteaddr);
    if ((newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen)) == -1) {
        perror("process_new_connection: Accept Connection Failed");
    } else {
        FD_SET(newfd, &master);
        if (newfd > fdmax) {
            fdmax = newfd;
        }
        if(debug == 1) {
            printf("Server Process: New Connection from %s on "
                   "socket %d\n", inet_ntoa(remoteaddr.sin_addr), newfd);
        }
    }
}
 
 
 
int main (int argc, char **argv)
{
    int yes=1;
 
    static int debug,port,localecho;
    static double serial_speed;
    static char serial_dev[20];
 
    arg_rec argtable[] =
    {
    {"-debug ", "<0 or 1>", arg_int, &debug, "0", "\t\t\t 1=Yes 0=No"},
    {"-port ", "<TCP/IP Port>", arg_int, &port, "0", "\t\t\t TCP/IP Listener Port"},
    {"-device ", "<Serial Device>", arg_str, serial_dev, "/dev/ttyS0", "\t\t Serial Port to be Bridged /dev/ttySx"},
    {"-speed ", "<Serial Speed>", arg_dbl, &serial_speed, "9600", "\t\t\t Serial Port Communication Speed"},
    {"-local ", "<0 or 1>", arg_int, &localecho, "0", "\t\t\t Local Port Echo 1=Yes 0=No"}
    };
 
    const size_t narg = sizeof(argtable)/sizeof(arg_rec);
 
    if (argc==1)
    {
        printf("Usage:  %s %s\n", argv[0], arg_syntax(argtable,narg));
        printf("%s\n", arg_glossary(argtable, narg, "  "));
        return 0;
    }
    else
    {
        char cmdline[200], errmsg[200], errmark[200];
        if(!arg_scanargv(argc, argv, argtable, narg, cmdline, errmsg, errmark))
        {
            printf("ERROR: %s\n", cmdline);
            printf("       %s %s\n", errmark, errmsg);
            return 1;
        }
    }
 
 
    FD_ZERO(&master);
    FD_ZERO(&read_fds);
 
    if ((listener = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }
 
    if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
        perror("setsockopt");
        exit(1);
    }
 
    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = INADDR_ANY;
    myaddr.sin_port = htons(port);
    memset(&(myaddr.sin_zero), '\0', 8);
 
    if (bind(listener, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1) {
        perror("bind");
        exit(1);
    }
 
    if (listen(listener, 10) == -1) {
        perror("listen");
        exit(1);
    }
 
    if ((fd_serial = open_serialdev(serial_dev, serial_speed)) == -1) {
        exit(1);
    }
 
    FD_SET(fd_serial, &master);
    FD_SET(listener, &master);
 
    fdmax = listener;  /* (fd_serial > listener ? fd_serial : listener); */
 
    for(;;) {
        read_fds = master;
        if (select(fdmax+1,&read_fds, NULL, NULL, NULL) == -1) {
            perror("select");
            exit(1);
        }
 
        for(current_connection=0; current_connection<= fdmax; current_connection++) {
            if (FD_ISSET(current_connection, &read_fds)) {
 
                if (current_connection == listener) {
                    process_new_connection(debug);
                } else {
                    if (current_connection == fd_serial) {
                        nbytes = read(current_connection, buf, sizeof(buf));
                        buf[nbytes] = 0;
                        if(debug == 1) {
                            printf("Received %s from serial port\n", buf);
                        }
                        if (nbytes > 0) {
                            process_socket_send(localecho);
                        }
                    } else {
                        if ((nbytes = recv(current_connection, buf, sizeof(buf), 0)) <= 0) {
                            if (nbytes == 0 && debug == 1) {
                                printf("Server Process: Socket %d hung up\n", current_connection);
                            } else {
                                perror("recv");
                            }
                            close(current_connection);
                            FD_CLR(current_connection, &master);
                        } else {
                            process_socket_send(localecho);
                            process_serial_send();
                        }
                    }
                }
            }
        }
    }
    close(fd_serial);
    return(0);
}

Go back one level

howto/how_to_bridge_tcp_rs232_in_c.txt · Last modified: 2010/08/19 02:55 (external edit)
Public Domain www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0