串口不能发数据 收正常 达人帮忙看看程序

ffuu 2007-08-10 04:12:02
串口不能发数据 收正常 达人帮忙看看程序

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>
#include <termios.h>
#include <errno.h>


#define BAUDRATE B38400

#define MODEMDEVICE "/dev/ttyS0"

#define _POSIX_SOURCE 1
#define FALSE 0

#define TRUE 1



volatile int STOP=FALSE;



main()

{

int fd,c, res;

struct termios oldtio,newtio;

char buf[255];



fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );

if (fd <0) {perror(MODEMDEVICE); exit(-1); }



tcgetattr(fd,&oldtio);


bzero(&newtio, sizeof(newtio));

newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

newtio.c_iflag = IGNPAR;

newtio.c_oflag = 0;





newtio.c_lflag = 0;



newtio.c_cc[VTIME] = 1;
newtio.c_cc[VMIN] = 0;


tcflush(fd, TCIFLUSH);

tcsetattr(fd,TCSANOW,&newtio);





while (STOP==FALSE) {

write(fd,"abc",3);


res = read(fd,buf,255);
if(res == 0) continue;

buf[res]=0;
printf(":%s:%d\n", buf, res);

if (buf[0]=='z') STOP=TRUE;

}

tcsetattr(fd,TCSANOW,&oldtio);

}

...全文
200 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
星星的爹地 2010-04-16
  • 打赏
  • 举报
回复
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,};


int open_ser(int fd,int comport)
{
char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2","/dev/ttyS3"};
fd=open(dev[comport-1],O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
perror("can not open serial port");
return (-1);
}
if(fcntl(fd,F_SETFL,0)<0)
{
printf("fcntl failed!\n");
}
else
{
printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));
}
if(isatty(STDIN_FILENO)==0)
{
printf("standard input is not a termianl device\n");
}
else
{
printf("isatty success!\n");
}
printf("fd-open %d\n",fd);
return fd;
}
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 fd");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
void read_ser(int fd)
{
int rd_num=0;
char recv_buf[20];
rd_num=read(fd,recv_buf,sizeof(recv_buf));
if(rd_num>0)
{
g_print("we can read %s from the COM.total:%d char\n",recv_buf,rd_num);
}
else
{
printf("read fail\n");
}
}
void write_ser(int fd)
{
int wr_num=0;
char send_buf[]="hello ,world\n";
wr_num=write(fd,send_buf,sizeof(send_buf));
if(wr_num>0)
{
printf("write seccess!\n");
}
else
{
printf("write fail!\n");
}
}
星星的爹地 2010-04-16
  • 打赏
  • 举报
回复
我的是读有问题,写串口没问题。楼主可以看下
ffuu 2007-08-11
  • 打赏
  • 举报
回复
我用的VMware 不会是VMware有问题吧? 有人遇到过没?
cceczjxy 2007-08-11
  • 打赏
  • 举报
回复
1,分别测试读和写.
2,检查你的串口线.
3,现不要设那么多项,仅仅用fsetispeed和fsetospeed设波特率看看.
ffuu 2007-08-10
  • 打赏
  • 举报
回复
再顶一下
ffuu 2007-08-10
  • 打赏
  • 举报
回复
照楼上的弄了 没效果 #_#
ma100 2007-08-10
  • 打赏
  • 举报
回复

newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;


newtio_new.c_cflag &= ~CSTOPB; //1 stop bits
newtio_new.c_cflag &= ~PARENB; //no parity check

23,223

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧