mini2440 系统下串口程序

本文介绍如何使用C语言设置串口通信速度、数据位数、停止位数及奇偶校验,通过实例演示了从打开串口、设置参数到读取数据的完整过程,适合初学者理解串口通信的基本配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>
#include <termio.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <time.h>
#include <errno.h>
#include <string.h>

#define FALSE -1
#define TRUE  1
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300, //
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {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 fd1");
   return;
  }
  tcflush(fd,TCIOFLUSH); //ͬÉÏ
 }
}

int set_Parity(int fd,int databits,int stopbits,int parity)
{
 struct termios options; //¶¨ÒåÒ»¸ö½á¹¹
 if ( tcgetattr( fd,&options) != 0) //Ê×ÏȶÁȡϵͳĬÈÏÉèÖÃoptionsÖÐ,±ØÐë
 {
  perror("SetupSerial 1");
  return(FALSE);
 }
 options.c_cflag &= ~CSIZE; //ÕâÊÇÉèÖÃc_cflagÑ¡Ïî²»°´Î»Êý¾ÝλÑÚÂë
 switch (databits)
 {
 case 7:
  options.c_cflag |= CS7; //ÉèÖÃc_cflagÑ¡ÏîÊý¾ÝλΪ7λ
  break;
 case 8:
  options.c_cflag |= CS8; //ÉèÖÃc_cflagÑ¡ÏîÊý¾ÝλΪ8λ
  break;
 default:
  fprintf(stderr,"Unsupported data size\n"); //ÆäËûµÄ¶¼²»Ö§³Ö
  return (FALSE);
 }
 switch (parity) //ÉèÖÃÆæÅ¼Ð£Ñ飬c_cflagºÍc_iflagÓÐЧ
 {
 case 'n':
 case 'N':
  options.c_cflag &= ~PARENB;
  options.c_iflag &= ~INPCK;
  break;
 case 'o':
 case 'O': options.c_cflag |= (PARODD | PARENB);
  options.c_iflag |= INPCK;
  break;
 case 'e':
 case 'E':
  options.c_cflag |= PARENB;
  options.c_cflag &= ~PARODD;
  options.c_iflag |= INPCK;
  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);
 }
 
 if (parity != 'n')
  options.c_iflag |= INPCK;
 
 
 options.c_cc[VTIME] = 150; // 15 seconds
 options.c_cc[VMIN] = 0;
 
 tcflush(fd,TCIFLUSH);
 if (tcsetattr(fd,TCSANOW,&options) != 0)
 {
  perror("SetupSerial 3");
  return (FALSE);
 }
 return (TRUE);
}

int OpenDev(char *Dev)
{
    int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAYÕâÖÖ·½Ê½¿´openº¯Êý
    if (-1 == fd)
    {
  perror("Can't Open Serial Port");
  return -1;
    }
    else
  return fd;
}

int main(int argc, char **argv)
{
    int fd;
    int nread;
    char buff[512];
    char *dev ="/dev/ttySAC1";
    fd = OpenDev(dev);
    if (fd>0)
    {
 printf("Open Serial succeed\n");
 set_speed(fd,19200);
    }
    else
    {
  printf("Can't Open Serial Port!\n");
  exit(0);
    }
    if (set_Parity(fd,8,1,'N')== FALSE)
    {
  printf("Set Parity Error\n");
  exit(1);
    }
 
    while(1)
    {
 int i;
// char buff1[2]={0x5a,0x5a};
// write(fd,buff1,1);
 while((nread = read(fd,buff,512))>0)
 {
  printf("\nLen %d\n",nread);
  buff[nread+1]='\0';
  for(i=0;i<nread;i++)
   printf("0x%x",buff[i]);
  printf("\n");
 }
    }
    //close(fd);
    //exit(0);
}

 

完了之后arm-linux-gcc -o voltage_set voltage_set.c

生成voltage_set

下载到mini2440核心板中

执行#./voltage_set

把学习板的地与核心板的地相连,学习板的TX与核心板的RX2相连就可以看到温度值了,当然,我用16进制显示在超级终端上,关掉程序用ctrl+c

转自:http://blog.sina.com.cn/s/blog_5d9c98c70100k6rs.html

转载于:https://www.cnblogs.com/rdyzju/archive/2012/03/21/2409318.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值