[C.C++] c语言uart串口通信编程s3c2410为例

393 0
Honkers 2025-6-4 01:18:37 | 显示全部楼层 |阅读模式

 嵌入式linux系统串口通信示例代码

  1. /*
  2. author:sun
  3. date: summer 2012
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <termios.h>
  10. //#include "gpio.h"
  11. //#define LED1 0x31
  12. //#define LED2 0x32
  13. //#define LED3 0x33
  14. //#define LEDOFF 0x34
  15. //#define LEDON 0x35
  16. struct termios options, oldoptions;
  17. int uart_init(void)
  18. {
  19. int fd;
  20. fd = open("/dev/s3c2410_serial0", O_RDWR);
  21. tcgetattr(fd, &oldoptions);
  22. tcgetattr(fd, &options);
  23. cfsetispeed(&options, B115200);
  24. cfsetospeed(&options, B115200);
  25. options.c_cflag &= ~PARENB;
  26. options.c_cflag &= ~CSIZE;
  27. options.c_cflag |= CS8;
  28. options.c_lflag &= (~ICANON);
  29. tcsetattr(fd,TCSANOW,&options);
  30. return fd;
  31. }
  32. int main(int argc, char *argv[])
  33. {
  34. int fd;
  35. char buf;
  36. fd=uart_init();
  37. while(read(fd,&buf,2)>0);
  38. printf("%s",buf);
  39. tcsetattr(fd,TCSANOW,&oldoptions);
  40. return 0;
  41. }
复制代码
  1. /*
  2. author:sun
  3. date: summer 2012
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <termios.h>
  10. #include "s5pv210-gpio.h"
  11. struct termios options, oldoptions;
  12. int uart_init(void)
  13. {
  14. int fd;
  15. char test[50];
  16. fd = open("/dev/s3c2410_serial0", O_RDWR);
  17. tcgetattr(fd, &oldoptions);
  18. tcgetattr(fd, &options);
  19. cfsetispeed(&options, B115200);
  20. cfsetospeed(&options, B115200);
  21. options.c_cflag &= ~PARENB;
  22. options.c_cflag |= CSTOPB;
  23. options.c_cflag &= ~CSIZE;
  24. options.c_cflag |= CS8;
  25. options.c_lflag &= (~ICANON);
  26. tcsetattr(fd,TCSANOW,&options);
  27. return fd;
  28. }
  29. int ledd()
  30. {
  31. int fb;
  32. fb= open("/dev/gpH0",O_WRONLY);
  33. if(fb<0)
  34. {
  35. perror("open error");
  36. }
  37. ioctl(fb,GPIO_SET_PIN_OUT,0x1);
  38. ioctl(fb,GPIO_SET_PIN_OUT,0x2);
  39. ioctl(fb,GPIO_SET_PIN_OUT,0x3);
  40. }
  41. int main(int argc, char *argv[])
  42. {
  43. int fa;
  44. char buf;
  45. int led;
  46. fa=uart_init();
  47. led=ledd();
  48. char led=0x01;
  49. char down=0x00;
  50. while(read(fa,&buf,1)>0);
  51. { printf("%d",buf);
  52. switch(buf)
  53. case 1:
  54. {
  55. led=0x02;
  56. write(fb,&led,sizeof(led));
  57. sleep(1);
  58. write(fb,&down,sizeof(led));
  59. }
  60. case 2:
  61. {
  62. led=0x04;
  63. write(fb,&led,sizeof(led));
  64. sleep(1);
  65. write(fb,&down,sizeof(led));
  66. }
  67. case 3:
  68. {
  69. led=0x08;
  70. write(fb,&led,sizeof(led));
  71. sleep(1);
  72. write(fb,&down,sizeof(led));
  73. }
  74. case 4:
  75. {
  76. led=0x02;
  77. while(1)
  78. {
  79. sleep(1);
  80. write(fb,&led,sizeof(led));
  81. led=led<<1;
  82. if(led==0x2<<3)
  83. {
  84. led=0x02;
  85. }
  86. }
  87. }
  88. }
  89. tcsetattr(fa,TCSANOW,&oldoptions);
  90. return 0;
  91. }
复制代码
  1. /*
  2. author:sun
  3. date: summer 2012
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <termios.h>
  10. #include "gpio.h"
  11. #define LED1 0x31
  12. #define LED2 0x32
  13. #define LED3 0x33
  14. #define LEDOFF 0x34
  15. #define LEDON 0x35
  16. struct termios options, oldoptions;
  17. int uart_init(void)
  18. {
  19. int fd;
  20. fd = open("/dev/s3c2410_serial0", O_RDWR);
  21. tcgetattr(fd, &oldoptions);
  22. tcgetattr(fd, &options);
  23. cfsetispeed(&options, B115200);
  24. cfsetospeed(&options, B115200);
  25. options.c_cflag &= ~PARENB; //无奇偶校验位
  26. options.c_cflag |= CSTOPB; //停止位为1位
  27. options.c_cflag &= ~CSIZE;
  28. options.c_cflag |= CS8; //数据位为8位
  29. options.c_lflag &= (~ICANON); //非标准模式(RAW模式), 无缓冲区,当接收时,输入的字符在它们被收到后立即被传送
  30. //options.c_lflag &= (~ECHO); //关回显
  31. //此处回显关不关是不影响本实验的,在手机实验的时候会有影响,因为手机
  32. //用的串口1的终端是没有打开的,若是不关的话,会把所以的输入全部读到,所以
  33. //要将回显关掉
  34. tcsetattr(fd,TCSANOW,&options);
  35. return fd;
  36. }
  37. int main(int argc, char *argv[])
  38. {
  39. tcsetattr(fd,TCSANOW,&oldoptions);
  40. return 0;
  41. }
复制代码
  1. #ifndef __USR_GPIO_H__
  2. #define __USR_GPIO_H__
  3. #define GPIO_SET_PIN 0
  4. //#define GPIO_SET_MULTI_PIN 1
  5. #define GPIO_SET_ALL_PIN 2
  6. #define GPIO_CLR_PIN 3
  7. //#define GPIO_CLR_MULTI_PIN 4 //等同GPIO_CLR_PIN
  8. #define GPIO_CLR_ALL_PIN 5
  9. /******************************
  10. * the configration of gpio mode
  11. ******************************/
  12. #define GPIO_CFG_PIN_OUT 6
  13. //#define GPIO_CFG_MULTI_PIN_OUT 10 //等同GPIO_CFG_PIN_OUT
  14. #define GPIO_CFG_PIN_IN 7
  15. //#define GPIO_CFG_MULTI_PIN_IN 11 //等同GPIO_CFG_PIN_IN
  16. #define GPIO_CFG_PIN_UP_ON 8
  17. //#define GPIO_CFG_MULTI_PIN_UP_ON 12 //等同GPIO_CFG_PIN_UP_ON
  18. #define GPIO_CFG_PIN_UP_OFF 9
  19. //#define GPIO_CFG_MULTI_PIN_UP_OFF 13 //等同GPIO_CFG_PIN_UP_OFF
  20. #define GPIO_PIN_AREA(startbit,endbit) ((startbit)|((endbit)<<4))
  21. #define GPIO_PIN_BIT(bit) GPIO_PIN_AREA(bit,bit)
  22. #endif
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4014
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行