EP9302 ethernet to serial gateway

Thread Starter

athenaabc

Joined Mar 19, 2013
1
Hi I'm totally new in programming for embedded systems and i must write program which realize serial to ethernet gateway using UDP protocol. For first host i write client program and second program must be server which must be compiled and run on the EP9302 board and received data must be send to the second host. How must i change my source code to be able to run?
Rich (BB code):
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>


#define MAX_BUF 1024
#define port 25

int main(int argc, char* argv[])
{
    int sockd;
    struct sockaddr_in my_name, cli_name;
    char buf[MAX_BUF];
    int status;
    int addrlen;
    char InStr[MAX_BUF];

    char inStr[30];                // String that we will send
    char outStr[30];            // Strig where will be the received result
    int wordsWritten, wordsRead = 0;

    sockd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockd == -1)
    {
        perror("Socket creation error");
        exit(1);
    }

    /* server address */
    my_name.sin_family = AF_INET;
    my_name.sin_addr.s_addr = INADDR_ANY;
    my_name.sin_port = port; //htons(atoi(argv[1]));

    status = bind(sockd, (struct sockaddr*)&my_name, sizeof(my_name));

    addrlen = sizeof(cli_name);
    status = recvfrom(sockd, buf, MAX_BUF, 0,
            (struct sockaddr*)&cli_name, &addrlen);

    strcpy(inStr,buf);
    int len = strlen(inStr);            // The length of inStr

    inStr[len] = '\r';                // Carriage return
    inStr[len + 1] = '\0';                // Symbol for the end of the string
    
int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);    
if (fd == (-1)) {                      
                printf("Unable to open /dev/ttyS0");       
                return 1;
}
else{                                                       
                fcntl(fd, F_SETFL, 0);            
}                                                    
struct termios options;                
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);            
cfsetospeed(&options, B9600);          
options.c_cflag &= ~PARENB;            
options.c_cflag &= ~CSTOPB;           
options.c_cflag &= ~CSIZE;       
options.c_cflag |= CS8;               
tcsetattr(fd, TCSANOW, &options);        
    
wordsWritten = write(fd, inStr, strlen(inStr));     
if(wordsWritten != strlen(inStr)){               
    printf("\nERROR! Different count of bytes.");
}
while(wordsRead == 0){                  
    wordsRead = read(fd, outStr, strlen(inStr));
    if(wordsRead != strlen(outStr)){          
        printf("\nERROR! Different count of bytes.");
    }
    outStr[wordsRead] = '\0';              
}
    strcpy(buf,outStr);
    close(fd);



    status = sendto(sockd, buf, strlen(buf)+1, 0,
            (struct sockaddr*)&cli_name, sizeof(cli_name));

    close(sockd);
    return 0;
}
 
Last edited:
Top