当前位置:   article > 正文

socket编程之socket转串口_socket转232

socket转232

---------------------------------------------------------------------------------

系统环境:Centos 6.5

    开发板:s3c2440

---------------------------------------------------------------------------------

1、程序功能

该程序是一个运行在开发板上的client程序,连接到windows下usr-tcp调试工具模拟的server,接受串口和socket数据并转发

usr-tcp通过tcp服务器向client发送数据,client接受到数据后通过串口发回usr-tcp

usr-tcp通过串口向client发送数据,client接收到数据后通过socket发回usr-tcp

 




2、源代码

串口相关设置 serial.c

  1. /*********************************************************************************
  2. * Copyright: (C) 2016 Lu Zengmeng<1540999272@qq.com>
  3. * All rights reserved.
  4. *
  5. * Filename: serial.c
  6. * Description: This file
  7. *
  8. * Version: 1.0.0(08/01/2016)
  9. * Author: Lu Zengmeng <1540999272@qq.com>
  10. * ChangeLog: 1, Release initial version on "08/01/2016 06:44:59 PM"
  11. *
  12. ********************************************************************************/
  13. #include <stdio.h>
  14. #include <termios.h>
  15. #include <fcntl.h>
  16. #include <string.h>
  17. #define FAIL -1
  18. #define OK 0
  19. int speed_arr[] = {B115200,B57600,B38400,B19200,B9600,B4800,B2400,B1200,B600,B300,B110};
  20. int name_arr[] = { 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 600, 300, 110};
  21. /* set baud rate */
  22. void set_speed(int fd, int speed)
  23. {
  24. int i;
  25. int status;
  26. struct termios options;
  27. tcgetattr(fd, &options);
  28. options.c_iflag &= ~ (INLCR | ICRNL | IGNCR);
  29. options.c_oflag &= ~(ONLCR | OCRNL);
  30. options.c_iflag &= ~(IXON);
  31. for( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
  32. {
  33. if(speed == name_arr[i])
  34. {
  35. tcflush(fd, TCIOFLUSH);/*empty input cache*/
  36. cfsetispeed(&options, speed_arr[i]);
  37. cfsetospeed(&options, speed_arr[i]);
  38. status = tcsetattr(fd, TCSANOW, &options);
  39. if(status != 0)
  40. perror("tcsetattr fd");
  41. return;
  42. }
  43. tcflush(fd,TCIOFLUSH);/*empty input cache*/
  44. }
  45. }
  46. /**
  47. * *@brief 设置串口数据位,停止位和效验位
  48. * *@param fd 类型 int 打开的串口文件描述符
  49. * *@param databits 类型 int 数据位 取值 为 7 或者8
  50. * *@param stopbits 类型 int 停止位 取值为 1 或者2
  51. * *@param parity 类型 int 效验类型 取值为N,E,O,S
  52. * */
  53. int set_parity(int fd,int databits,int stopbits,int parity)
  54. {
  55. struct termios options;
  56. if ( tcgetattr( fd,&options) != 0)
  57. {
  58. perror("SetupSerial 1");
  59. return(FAIL);
  60. }
  61. options.c_cflag &= ~CSIZE;
  62. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
  63. options.c_oflag &= ~OPOST; /*Output*/
  64. switch (databits) /*set datebits*/
  65. {
  66. case 7:
  67. options.c_cflag |= CS7;
  68. break;
  69. case 8:
  70. options.c_cflag |= CS8;
  71. break;
  72. default:
  73. fprintf(stderr,"Unsupported data size\n");
  74. return (FAIL);
  75. }
  76. switch (parity)
  77. {
  78. case 'n':
  79. case 'N':
  80. options.c_cflag &= ~PARENB; /* Clear parity enable */
  81. options.c_iflag &= ~INPCK; /* Enable parity checking */
  82. break;
  83. case 'o':
  84. case 'O':
  85. options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
  86. options.c_iflag |= INPCK; /* Disnable parity checking */
  87. break;
  88. case 'e':
  89. case 'E':
  90. options.c_cflag |= PARENB; /* Enable parity */
  91. options.c_cflag &= ~PARODD; /* 转换为偶效验*/
  92. options.c_iflag |= INPCK; /* Disnable parity checking */
  93. break;
  94. case 'S':
  95. case 's': /*as no parity*/
  96. options.c_cflag &= ~PARENB;
  97. options.c_cflag &= ~CSTOPB;
  98. break;
  99. default:
  100. fprintf(stderr,"Unsupported parity\n");
  101. return (FAIL);
  102. }
  103. /*set stopbits */
  104. switch (stopbits)
  105. {
  106. case 1:
  107. options.c_cflag &= ~CSTOPB;
  108. break;
  109. case 2:
  110. options.c_cflag |= CSTOPB;
  111. break;
  112. default:
  113. fprintf(stderr,"Unsupported stop bits\n");
  114. return (FAIL);
  115. }
  116. /* Set input parity options */
  117. if (parity != 'n')
  118. options.c_iflag |= INPCK;
  119. options.c_cc[VTIME] = 0; // 15 seconds
  120. options.c_cc[VMIN] = 0;
  121. tcflush(fd,TCIFLUSH); /* Update the optionsions and do it NOW */
  122. if (tcsetattr(fd,TCSANOW,&options) != 0)
  123. {
  124. perror("tcsetattr");
  125. return FAIL;
  126. }
  127. return (OK);
  128. }
  129. /**
  130. * *@breif open device
  131. * */
  132. int open_dev(char *dev)
  133. {
  134. int fd;
  135. fd = open( dev, O_RDWR|O_NOCTTY|O_NDELAY);
  136. if (fd < 0)
  137. {
  138. perror("open");
  139. return FAIL;
  140. }
  141. else
  142. return fd;
  143. }

serial.h

  1. /********************************************************************************
  2. * Copyright: (C) 2016 Lu Zengmeng<1540999272@qq.com>
  3. * All rights reserved.
  4. *
  5. * Filename: serial.h
  6. * Description: This head file
  7. *
  8. * Version: 1.0.0(08/01/2016)
  9. * Author: Lu Zengmeng <1540999272@qq.com>
  10. * ChangeLog: 1, Release initial version on "08/01/2016 06:44:25 PM"
  11. *
  12. ********************************************************************************/
  13. #include <stdio.h>
  14. #ifndef __SERIAL_H__
  15. #define __SERIAL_H__
  16. extern int speed_arr[];
  17. extern int name_arr[];
  18. extern void set_speed(int fd, int speed);
  19. extern int set_parity(int fd,int databits,int stopbits,int parity);
  20. extern int open_dev(char *dev);
  21. #endif
socket初始化连接 tcp.c

  1. /*********************************************************************************
  2. * Copyright: (C) 2016 Lu Zengmeng<1540999272@qq.com>
  3. * All rights reserved.
  4. *
  5. * Filename: tcp.c
  6. * Description: This file
  7. *
  8. * Version: 1.0.0(08/02/2016)
  9. * Author: Lu Zengmeng <1540999272@qq.com>
  10. * ChangeLog: 1, Release initial version on "08/02/2016 08:09:55 AM"
  11. *
  12. ********************************************************************************/
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <arpa/inet.h>
  17. #include <netinet/in.h>
  18. #include <strings.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <fcntl.h>
  23. #include <sys/stat.h>
  24. #include "tcp.h"
  25. /**************************************************************************************
  26. * Description:
  27. * Input Args:
  28. * Output Args:
  29. * Return Value:
  30. *************************************************************************************/
  31. int socket_init(void)
  32. {
  33. int fd;
  34. struct sockaddr_in serv_addr;
  35. bzero(&serv_addr,sizeof(serv_addr));
  36. serv_addr.sin_family = AF_INET;
  37. serv_addr.sin_port = htons(PORT);
  38. if ((inet_pton(AF_INET,IPADDR,&serv_addr.sin_addr)) < 0)
  39. {
  40. perror("inet_pton");
  41. exit(1);
  42. }
  43. fd = socket(AF_INET, SOCK_STREAM, 0);
  44. if (fd < 0)
  45. {
  46. perror("socket");
  47. exit(1);
  48. }
  49. if (-1 == connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)))
  50. {
  51. perror("connect");
  52. exit(1);
  53. }
  54. printf("connect sucesseful\n");
  55. return fd;
  56. }
  57. /* ----- End of socket_init() ----- */

tcp.h

  1. /********************************************************************************
  2. * Copyright: (C) 2016 Lu Zengmeng<1540999272@qq.com>
  3. * All rights reserved.
  4. *
  5. * Filename: tcp.h
  6. * Description: This head file
  7. *
  8. * Version: 1.0.0(08/02/2016)
  9. * Author: Lu Zengmeng <1540999272@qq.com>
  10. * ChangeLog: 1, Release initial version on "08/02/2016 08:22:06 AM"
  11. *
  12. ********************************************************************************/
  13. #include <stdio.h>
  14. #ifndef __TCP_H_
  15. #define __TCP_H_
  16. #define PORT 8003
  17. #define IPADDR "192.168.1.104"
  18. extern int socket_init(void);
  19. #endif

main.c

  1. /*********************************************************************************
  2. * Copyright: (C) 2016 Lu Zengmeng<1540999272@qq.com>
  3. * All rights reserved.
  4. *
  5. * Filename: main.c
  6. * Description: This file
  7. *
  8. * Version: 1.0.0(08/01/2016)
  9. * Author: Lu Zengmeng <1540999272@qq.com>
  10. * ChangeLog: 1, Release initial version on "08/01/2016 06:45:37 PM"
  11. *
  12. ********************************************************************************/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include <termios.h>
  22. #include <pthread.h>
  23. #include "serial.h"
  24. #include "tcp.h"
  25. #define DEVICE "/dev/ttyS1"
  26. #define BUF_SIZE 128
  27. int usrt_fd;
  28. int sock_fd;
  29. void usrt_to_socket (void);
  30. void socket_to_usrt (void);
  31. /* start main */
  32. int main(int argc,char **argv)
  33. {
  34. int ret;
  35. char *dev = DEVICE;
  36. pthread_t sock,usrt;
  37. sock_fd = socket_init();
  38. usrt_fd = open_dev(dev);
  39. set_parity(usrt_fd,8,1,'N'); // 设置串口数据位、停止位、校验位
  40. set_speed(usrt_fd,9600); // 设置串口波特率
  41. ret = pthread_create(&sock,NULL,(void *)socket_to_usrt,NULL); // 创建sock线程接收socket数据
  42. if(ret < 0)
  43. {
  44. perror("phread_create");
  45. exit(1);
  46. }
  47. ret = pthread_create(&usrt,NULL,(void *)usrt_to_socket,NULL); // 创建usrt线程接收串口数据
  48. if(ret < 0)
  49. {
  50. perror("phread_create");
  51. exit(1);
  52. }
  53. pthread_join(sock,NULL); // 等待线程退出
  54. pthread_join(usrt,NULL);
  55. close(sock_fd);
  56. close(usrt_fd);
  57. printf("main exit...\n");
  58. return 0;
  59. }
  60. /**************************************************************************************
  61. * Description:接收socket数据并通过串口转发
  62. * Input Args:
  63. * Output Args:
  64. * Return Value:
  65. *************************************************************************************/
  66. void socket_to_usrt (void)
  67. {
  68. printf("start socket_to_usrt...\n");
  69. int n = 0;
  70. int m = 0;
  71. char buf[BUF_SIZE];
  72. while(1)
  73. {
  74. memset(buf,0,BUF_SIZE);
  75. n = read(sock_fd,buf,BUF_SIZE);
  76. if(n>0)
  77. {
  78. if(0==strncmp(buf,"exit",4)) // 收到exit时,线程退出
  79. {
  80. m = write(usrt_fd,buf,BUF_SIZE);
  81. if(m<0)
  82. {
  83. perror("write to usrt");
  84. }
  85. break;
  86. }
  87. m = write(usrt_fd,buf,BUF_SIZE);
  88. if(m<0)
  89. {
  90. perror("write to usrt");
  91. }
  92. }
  93. else if(n<0)
  94. {
  95. perror("read from socket");
  96. }
  97. }
  98. printf("socket_to_usrt exit...\n");
  99. pthread_exit(0);
  100. } /* ----- End of sock_to_usrt() ----- */
  101. /**************************************************************************************
  102. * Description:接收串口数据并通过socket转发
  103. * Input Args:
  104. * Output Args:
  105. * Return Value:
  106. *************************************************************************************/
  107. void usrt_to_socket (void)
  108. {
  109. printf("start usrt_to_socket...\n");
  110. int n = 0;
  111. int m = 0;
  112. char buf[BUF_SIZE];
  113. while(1)
  114. {
  115. memset(buf,0,BUF_SIZE);
  116. n = read(usrt_fd,buf,BUF_SIZE);
  117. if(n>0)
  118. {
  119. if(0==strncmp(buf,"exit",4)) // 收到exit时,线程退出
  120. {
  121. m = write(sock_fd,buf,BUF_SIZE);
  122. if(m<0)
  123. {
  124. perror("write to sock");
  125. }
  126. break;
  127. }
  128. m = write(sock_fd,buf,BUF_SIZE);
  129. if(m<0)
  130. {
  131. perror("write to sock");
  132. }
  133. }
  134. else if(n<0)
  135. {
  136. perror("read from usrt");
  137. }
  138. }
  139. printf("usrt_to_socket exit...\n");
  140. pthread_exit(0);
  141. } /* ----- End of usrt_to_sock() ----- */
makefile

  1. NAME=client
  2. CC=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc
  3. EXTRA_CFLAGS+= -Wall -Werror
  4. EXTRA_CFLAGS+= -lpthread
  5. all:
  6. $(CC) $(EXTRA_CFLAGS) main.c serial.c tcp.c -o $(NAME)
  7. cp $(NAME) /tftp


3、调试助手下载

USR-TCP232-Test




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/791264
推荐阅读
相关标签
  

闽ICP备14008679号