EP9302 ethernet to serial gateway

Discussion in 'Programmer's Corner' started by athenaabc, Mar 19, 2013.

  1. athenaabc

    Thread Starter New Member

    Mar 19, 2013
    1
    0
    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?
    Code ( (Unknown Language)):
    1. #include <sys/types.h>
    2. #include <sys/socket.h>
    3. #include <netinet/in.h>
    4. #include <arpa/inet.h>
    5. #include <string.h>
    6. #include <stdio.h>
    7. #include <stdlib.h>
    8. #include <unistd.h>
    9. #include <fcntl.h>
    10. #include <termios.h>
    11.  
    12.  
    13. #define MAX_BUF 1024
    14. #define port 25
    15.  
    16. int main(int argc, char* argv[])
    17. {
    18.     int sockd;
    19.     struct sockaddr_in my_name, cli_name;
    20.     char buf[MAX_BUF];
    21.     int status;
    22.     int addrlen;
    23.     char InStr[MAX_BUF];
    24.  
    25.     char inStr[30];                // String that we will send
    26.     char outStr[30];            // Strig where will be the received result
    27.     int wordsWritten, wordsRead = 0;
    28.  
    29.     sockd = socket(AF_INET, SOCK_DGRAM, 0);
    30.     if (sockd == -1)
    31.     {
    32.         perror("Socket creation error");
    33.         exit(1);
    34.     }
    35.  
    36.     /* server address */
    37.     my_name.sin_family = AF_INET;
    38.     my_name.sin_addr.s_addr = INADDR_ANY;
    39.     my_name.sin_port = port; //htons(atoi(argv[1]));
    40.  
    41.     status = bind(sockd, (struct sockaddr*)&my_name, sizeof(my_name));
    42.  
    43.     addrlen = sizeof(cli_name);
    44.     status = recvfrom(sockd, buf, MAX_BUF, 0,
    45.             (struct sockaddr*)&cli_name, &addrlen);
    46.  
    47.     strcpy(inStr,buf);
    48.     int len = strlen(inStr);            // The length of inStr
    49.  
    50.     inStr[len] = '\r';                // Carriage return
    51.     inStr[len + 1] = '\0';                // Symbol for the end of the string
    52.    
    53. int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);    
    54. if (fd == (-1)) {                      
    55.                 printf("Unable to open /dev/ttyS0");      
    56.                 return 1;
    57. }
    58. else{                                                      
    59.                 fcntl(fd, F_SETFL, 0);            
    60. }                                                    
    61. struct termios options;                
    62. tcgetattr(fd, &options);
    63. cfsetispeed(&options, B9600);            
    64. cfsetospeed(&options, B9600);          
    65. options.c_cflag &= ~PARENB;            
    66. options.c_cflag &= ~CSTOPB;          
    67. options.c_cflag &= ~CSIZE;      
    68. options.c_cflag |= CS8;              
    69. tcsetattr(fd, TCSANOW, &options);        
    70.    
    71. wordsWritten = write(fd, inStr, strlen(inStr));    
    72. if(wordsWritten != strlen(inStr)){              
    73.     printf("\nERROR! Different count of bytes.");
    74. }
    75. while(wordsRead == 0){                  
    76.     wordsRead = read(fd, outStr, strlen(inStr));
    77.     if(wordsRead != strlen(outStr)){          
    78.         printf("\nERROR! Different count of bytes.");
    79.     }
    80.     outStr[wordsRead] = '\0';              
    81. }
    82.     strcpy(buf,outStr);
    83.     close(fd);
    84.  
    85.  
    86.  
    87.     status = sendto(sockd, buf, strlen(buf)+1, 0,
    88.             (struct sockaddr*)&cli_name, sizeof(cli_name));
    89.  
    90.     close(sockd);
    91.     return 0;
    92. }
    93.  
     
    Last edited: Mar 19, 2013
Loading...