LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 831|回复: 2

一个串口通信的问题,程序运行有问题,望大家帮忙!

[复制链接]
发表于 2005-8-3 00:53:47 | 显示全部楼层 |阅读模式
我在同一PC下,同一下串口下,发送和接收数据!
源代码:
#include <assert.h>
#include <termios.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>      /*错误号定义*/
#define RCV_LEN  32
#define SND_LEN  32
#define TIMES  3
                                                                                                                                             
void receive(int fd)
                                                                                                                                             
{int errorcode;
char rec_buf[32];
errorcode=read(fd,rec_buf,RCV_LEN);
if(errorcode) printf("The received string is %s\n",rec_buf);
}
                                                                                                                                             
void send (int fd)
                                                                                                                                             
{int nwrite;
  char snd_buf[32];
  printf("lease enter a string to send(<=32 characters):\n");
  scanf ("%s",snd_buf);
  nwrite=write(fd,snd_buf,SND_LEN);
  if(nwrite==-1) printf("error");
  if(nwrite) printf("You have already send %s  ",snd_buf);
                                                                                                                                             
}
                                                                                                                                             
void set_speed(int fd, int speed)
{
  int   i;
  int   status;
  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, };
  struct termios   Opt;
  tcgetattr(fd, &Opt);
  for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)
   {
        if  (speed == name_arr)
        {
            tcflush(fd, TCIOFLUSH);
        cfsetispeed(&Opt, speed_arr);
        cfsetospeed(&Opt, speed_arr);
        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)
  {
        perror("SetupSerial 1");
        return(0);
  }
  options.c_cflag &= ~CSIZE;
   /*设置数据位数*/
   switch (databits)
                                                                                                                                             
  {
        case 7:
                options.c_cflag |= CS7;
                break;
        case 8:
                options.c_cflag |= CS8;
                break;
        default:
                fprintf(stderr,"Unsupported data size\n");
                return (0);
        }
  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);  /* 设置为奇效验*/
    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 (0);
                }
  /* 设置停止位*/
  switch (stopbits)
        {
        case 1:
                options.c_cflag &= ~CSTOPB;
                break;
        case 2:
                options.c_cflag |= CSTOPB;
                break;
        default:
                fprintf(stderr,"Unsupported stop bits\n");
                return (0);
        }
  /* 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 (0);
        }
  return (1);
}


int main()
{
int fd,i;
char pathname[11]="/dev/ttyS0";
printf("now start。。。。。。\n:");
fd=open(pathname,O_RDWR,0666);
printf("fd=%d\n",fd);
if(fd)  set_speed(fd,19200);
else  {printf("Can't Open Serial Port!\n"); exit(0); }
if (set_Parity(fd,8,1,'N')== 0)
{printf("Set Parity Error\n");  exit(1); }
for (i=0;i<TIMES;i++)   { send(fd);  receive(fd);  }
return 0;
}

运行结果:
now start。。。。。。
fd=3
Please enter a string to send(<=32 characters):
aadd                                              //我的输入,然后回车
aadd?   ?                                      //回显的内容,然后就没有动静了!!

这个到底是什么原因阿???
发表于 2005-8-7 16:22:10 | 显示全部楼层
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
这句话错了吧,应该为i<sizeof(speed_arr)。

if (status != 0)
perror("tcsetattr fd1");
return;
这个也错了吧,应该为
if(status != 0)
{
perror("tcsetattr fd1");
return;
}
回复 支持 反对

使用道具 举报

发表于 2005-8-7 19:45:46 | 显示全部楼层
Post by realtang
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
这句话错了吧,应该为i<sizeof(speed_arr)。

------------------
>>   这句铁没错,惯用法么。不过楼主写的还不够经典,经典的应该这么写:
>>     i < sizeof(speed_arr) / sizeof(speed_arr[0]);
>>   这样无论speed_arr的类型如何变化,都不影响代码的移植,哈哈。
回复 支持 反对

使用道具 举报

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

本版积分规则

快速回复 返回顶部 返回列表