音影先锋亚洲天堂网|电影世界尽头的爱完整版播放|国产 熟女 91|高清无码免费观看欧美日韩|韩国一区二区三区黄色录像|美女亚洲加勒比在线|亚洲综合网 开心五月|7x成人在线入口|成人网站免费日韩毛片区|国产黄片?一级?二级?三级

簡單的串口測試程序

出處:mabaoqiu 發(fā)布于:2009-11-30 13:42:01

  1.TTY為編譯好的在嵌入式系統(tǒng)utuLinux下的可執(zhí)行文件(交叉編譯器為arm-linux-gcc).

  2.程序?yàn)閅C2440通過串口1(COM1)向計(jì)算機(jī)主機(jī)發(fā)送一個字符串,主機(jī)可用超級終端(windows)或minicom(Linux)接收。

  3.minicom下只能顯示90個左右的字符,原因?qū)ふ抑小?/P>

  #include     <STdio.h>      /*標(biāo)準(zhǔn)輸入輸出定義*/

  #include     <stdlib.h>     /*標(biāo)準(zhǔn)函數(shù)庫定義*/

  #include     <unistd.h>     /*Unix標(biāo)準(zhǔn)函數(shù)定義*/

  #include     <sys/types.h>  /**/

  #include     <sys/stat.h>   /**/

  #include     <fcntl.h>      /*文件控制定義*/

  #include     <termios.h>    /*PPSIX終端控制定義*/

  #include     <errno.h>      /*錯誤號定義*/

  #define  FALSE  -1

  //設(shè)置串口通信速率

  // fd     類型 int  打開串口的文件句柄

  // speed  類型 int  串口速度

  //

  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, };

  int 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 FALSE;

  }

  tcflush(fd,TCIOFLUSH);

  }

  }

  /**

  [email=*@brief]*@brief[/email]   設(shè)置串口數(shù)據(jù)位,停止位和效驗(yàn)位

  [email=*@param]*@param[/email]  fd     類型  int  打開的串口文件句柄*

  [email=*@param]*@param[/email]  databits 類型  int 數(shù)據(jù)位   取值 為 7 或者8*

  [email=*@param]*@param[/email]  stopbits 類型  int 停止位   取值為 1 或者2*

  [email=*@param]*@param[/email]  parity  類型  int  效驗(yàn)類型 取值為N,E,O,,S

  */

  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;

  switch (databits) /*設(shè)置數(shù)據(jù)位數(shù)*/

  {

  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 */

  break;

  case 'o':

  case 'O':

  options.c_cflag |= (PARODD | PARENB);  /* 設(shè)置為奇效驗(yàn)*/

  options.c_iflag |= INPCK;             /* Disnable parity checking */

  break;

  case 'e':

  case 'E':

  options.c_cflag |= PARENB;     /* Enable parity */

  options.c_cflag &= ~PARODD;   /* 轉(zhuǎn)換為偶效驗(yàn)*/

  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);

  }

  /* 設(shè)置停止位*/

  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')

  options.c_iflag |= INPCK;

  options.c_cc[VTIME] = 150; // 15 seconds

  options.c_cc[VMIN] = 0;

  tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */

  if (tcsetattr(fd,TCSANOW,&options) != 0)

  {

  perror("SetupSerial 3");

  return (FALSE);

  }

  return (0);

  }

  /**

  [email=*@breif]*@breif[/email] 打開串口

  */

  int OpenDev(char *Dev)

  {

  int fd = open(Dev, O_RDWR | O_NOCTTY | O_NDELAY);         //| O_NOCTTY | O_NDELAY

  if (-1 == fd)

  { /*設(shè)置數(shù)據(jù)位數(shù)*/

  perror("Can't Open Serial Port");

  return FALSE;

  }

  else

  return fd;

  }

  /**

  [email=*@breif]*@breif[/email]  main()

  */

  int main(int argc, char **argv)

  {

  int fd;

  int nwrite;

  char *buff="Hello! This is a Serial Test Program,if you can see this,you success!Congratulations!!!!!!!!!";

  char *dev ="/dev/s3c2410_serial1";

  fd = OpenDev(dev);

  if (fd>0)

  set_speed(fd,115200);

  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);

  }

  if((nwrite = write(fd,buff,strlen(buff)))>0)

  {

  printf("%s\n",buff);

  printf("\nLen %d\n",nwrite);

  // buff[nread+1]='\0';

  //printf("\n%s",buff);

  }

  close(fd);

  }

  



  
關(guān)鍵詞:簡單的串口測試程序串口

版權(quán)與免責(zé)聲明

凡本網(wǎng)注明“出處:維庫電子市場網(wǎng)”的所有作品,版權(quán)均屬于維庫電子市場網(wǎng),轉(zhuǎn)載請必須注明維庫電子市場網(wǎng),http://m.58mhw.cn,違反者本網(wǎng)將追究相關(guān)法律責(zé)任。

本網(wǎng)轉(zhuǎn)載并注明自其它出處的作品,目的在于傳遞更多信息,并不代表本網(wǎng)贊同其觀點(diǎn)或證實(shí)其內(nèi)容的真實(shí)性,不承擔(dān)此類作品侵權(quán)行為的直接責(zé)任及連帶責(zé)任。其他媒體、網(wǎng)站或個人從本網(wǎng)轉(zhuǎn)載時(shí),必須保留本網(wǎng)注明的作品出處,并自負(fù)版權(quán)等法律責(zé)任。

如涉及作品內(nèi)容、版權(quán)等問題,請?jiān)谧髌钒l(fā)表之日起一周內(nèi)與本網(wǎng)聯(lián)系,否則視為放棄相關(guān)權(quán)利。

FT232RL—性價(jià)比還不錯的USB轉(zhuǎn)串口通用芯片!
廣告
OEM清單文件: OEM清單文件
*公司名:
*聯(lián)系人:
*手機(jī)號碼:
QQ:
有效期:

掃碼下載APP,
一鍵連接廣大的電子世界。

在線人工客服

買家服務(wù):
賣家服務(wù):
技術(shù)客服:

0571-85317607

網(wǎng)站技術(shù)支持

13606545031

客服在線時(shí)間周一至周五
9:00-17:30

關(guān)注官方微信號,
第一時(shí)間獲取資訊。

建議反饋

聯(lián)系人:

聯(lián)系方式:

按住滑塊,拖拽到最右邊
>>
感謝您向阿庫提出的寶貴意見,您的參與是維庫提升服務(wù)的動力!意見一經(jīng)采納,將有感恩紅包奉上哦!