linux系统下,串口接收发送数据,并利用udp转发剖析

  • 格式:doc
  • 大小:10.72 KB
  • 文档页数:8

//************璇ョ▼搴忓疄鐜扮殑UDP鏁版嵁鍜屼覆鍙f暟鎹殑杞寲**********************/ //鏃堕棿锛?013/7/30 //浣滆€咃細鐢板瓙 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PORT 61557 //杩滅涓绘満绔彛鍙?#define BROADCASTPORT 8003 //鏈湴绔彛鍙?#define BUF_SIZE 100 //瀹氫箟UDP鏁版嵁鍙戦€佺殑闀垮害 #define FALSE -1 #define TRUE 0 /** *@bref 璁剧疆涓插彛閫氫俊閫熺巼 *@param fd 绫诲瀷 int 鎵撳紑涓插彛鏂囦欢鐨勫彞鏌?*@param speed 绫诲瀷 int 涓插彛閫熷害 *@return void */ int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300, }; int name_arr[] = {115200,38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300, }; void set_speed(int fd, int speed){ int i; int status; struct termios Opt; tcgetattr(fd, &Opt); for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) { if (speed == name_arr[i]) { tcflush(fd, TCIOFLUSH); cfsetispeed(&Opt, speed_arr[i]); cfsetospeed(&Opt, speed_arr[i]); status = tcsetattr(fd, TCSANOW, &Opt); if (status != 0) { perror("tcsetattr fd"); return; } tcflush(fd,TCIOFLUSH); } } } /** *@bref 璁剧疆涓插彛鏁版嵁浣嶏紝鍋滄浣嶅拰鏍¢獙浣?*@param fd 绫诲瀷 int 鎵撳紑涓插彛鏂囦欢鐨勫彞鏌?*@param databits 绫诲瀷 int 鏁版嵁浣?鍙栧€间负7鎴? *@param stopbits 绫诲瀷 int 鍋滄浣?鍙栧€间负1鎴? *@param parity 绫诲瀷 int 鏍¢獙浣?鍙栧€间负N锛孍锛孫锛孲 *@return void */ int set_Parity(int fd,int databits,int stopbits,int parity) { struct termios options; if ( tcgetattr( fd,&options) != 0) { perror("SetupSerial 1"); return(FALSE); } options.c_cflag &= ~CSIZE; options.c_iflag &= ~INPCK; options.c_iflag |= IGNBRK; options.c_iflag &= ~ICRNL; options.c_iflag &= ~IXON; options.c_lflag &= ~IEXTEN; options.c_lflag &= ~ECHOK; options.c_lflag &= ~ECHOCTL; options.c_lflag &= ~ECHOKE; options.c_oflag &= ~ONLCR; switch (databits) /*脡猫脰脙脢媒鸥脻脦禄脢媒*/ { case 7: options.c_cflag |= CS7; break; case 8: options.c_cflag |= CS8; break; default: // fprintf(stderr,"Unsupported data size\n"); return (FALSE); } switch (parity) { case 'n': case 'N': //options.c_cflag &= ~PARENB; /* Clear parity enable */ //options.c_iflag &= ~INPCK; /* Enable parity checking */ options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); options.c_oflag &= ~OPOST; /*Output*/ break; case 'o': case 'O': options.c_cflag |= (PARODD | PARENB); /* 脡猫脰脙脦陋脝忙脨搂脩茅*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case 'e': case 'E': options.c_cflag |= PARENB; /* Enable parity */ options.c_cflag &= ~PARODD; /* 脳陋禄禄脦陋脜艗脨搂脩茅*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case 'S': case 's': /*as no parity*/ options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB;break; default: // fprintf(stderr,"Unsupported parity\n"); return (FALSE); } /* 脡猫脰脙脥拢脰鹿脦禄*/ switch (stopbits) { case 1: options.c_cflag &= ~CSTOPB; break; case 2: options.c_cflag |= CSTOPB; break; default: // fprintf(stderr,"Unsupported stop bits\n"); return (FALSE); } /* Set input parity option */ if ((parity != 'n')&&(parity != 'N')) options.c_iflag |= INPCK; tcflush(fd,TCIFLUSH); options.c_cc[VTIME] = 150; /* 脡猫脰脙鲁卢脢卤15 seconds*/ options.c_cc[VMIN] = 0; /* Update the options and do it NOW */ if (tcsetattr(fd,TCSANOW,&options) != 0) { // perror("SetupSerial 3"); return (FALSE); } return (TRUE); }

/** *@bref 鎵撳紑涓插彛 *@param Dev 绫诲瀷 char 涓插彛鍙?*@return int 杩斿洖涓插彛鐨勫彞鏌?*/ int OpenDev(char *Dev) { int fd = open( Dev, O_RDWR|O_NOCTTY|O_NONBLOCK ); //浠ヨ鍐欐柟寮忔墦寮€涓插彛锛孖O鎿嶄綔涓洪潪闃诲妯″紡 if (-1 == fd) { // perror("Can't Open Serial Port"); return -1; //鑻ユ墦寮€澶辫触锛岃繑鍥?1 } else return fd; } /** *@bref 浠庡鐣屾帴鏀禪DP鍖?*@param change 绫诲瀷 unsigned char 鍌ㄥ瓨UDP鎺ユ敹杩涙潵鐨勬暟鎹?*@return 杩斿洖鎺ユ敹鍒版暟鎹殑闀垮害 */ int udp_broadcast_service(unsigned char *change) { int castsockfd, connfd; int optval; int client_len; int revc; struct sockaddr_in serv_addr, client_addr; if ( (castsockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) //鍒涘缓濂楁帴瀛梥ocket { // perror("sockfd error!\n"); exit(1); } //******閰嶇疆鏈湴淇℃伅锛屽寘鎷鍙e彿锛孖P锛屽拰鍗忚鏃忥紙涓€鑸负