UNIX下串口通讯程序问题。

hero249 2003-08-18 04:23:39
要求com1发送数据,比如“123456789”com2接收数据。
我写的怎么都收不通。大家快帮帮我看看把。我把发送和接收分开写了。
发送函数:
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix 标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX 终端控制定义*/
#include <errno.h> /*错误号定义*/
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}


/**
*@brief 设置串口通信速率
*@param fd 类型 int 打开串口的文件句柄
*@param speed 类型 int 串口速度
*@return void
*/
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]) {
printf("baud is %d\n",name_arr[i]);
tcflush(fd, TCIOFLUSH);
if(cfsetispeed(&Opt, speed_arr[i])==0)
{
printf("baud set is ok.\n");
}
/* if(cfsetospeed(&Opt, speed_arr[i])==0)
{
printf("baud set is ok.\n");
}*/


status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
perror("tcsetattr fd");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}


/**
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄
*@param databits 类型 int 数据位 取值 为 7 或者8
*@param stopbits 类型 int 停止位 取值为 1 或者2
*@param parity 类型 int 效验类型 取值为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) /*设置数据位数*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
printf("data bit is 8.\n");
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n"); return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
printf("parity is 'N'.\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 (FALSE);
}
/* 设置停止位*/
switch (stopbits)
{
case 1:
printf("stop bit is 1.\n");
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;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}


int main(int argc, char **argv)
{
int fd;
int nread;
char buff[512];
char *dev ="/dev/tty01";
fd = OpenDev(dev);
if (fd>0)
set_speed(fd,4800);
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);
}
sprintf(buff, "1234567890");
while(1)
{
while((nread = write(fd,buff,10))>0)
{
printf("\nLen %d\n",nread);
buff[nread+1]='\0';
printf("\n%s",buff);
sleep(1);
}
}
//close(fd);
//exit(0);

}
接收函数:
int main(int argc, char **argv)
{
int fd;
int nread;
char buff[512];
char *dev ="/dev/tty00";
fd = OpenDev(dev);
if (fd>0)
set_speed(fd,4800);
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)
{
while((nread = read(fd,buff,512))>0)
{
printf("\nLen %d\n",nread);
buff[nread+1]='\0';
printf("\n%s",buff);
// sleep(1);
}
}
//close(fd);
//exit(0);

}
快点帮帮我啊,我都一个星期没弄好啦。
...全文
60 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hero249 2003-08-23
  • 打赏
  • 举报
回复
谢谢楼主,非标注输出的控制标我已经了结各项的含义了,我得程序缺少了非标注输出的控制字ICANON.
hero249 2003-08-19
  • 打赏
  • 举报
回复
要把这几项添加到那儿?
wwwunix 2003-08-19
  • 打赏
  • 举报
回复
这是我的一个接收测试程序,你可以参考一下。(调试通过,速率为19200)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <termios.h>
int main() {
int fd,size,stopb,check,i;
unsigned int sub;
char a;
struct termios term,save_term;
struct timeval t1,t2;
struct timezone tz1,tz2;
double per;
speed_t speed;
fd=open("/dev/ttyS0",O_RDWR);
if (fd<0) {
perror("Open tty error");
exit(-1);
}
tcgetattr(fd,&save_term);
term=save_term;
term.c_lflag &= ~(ECHO|ICANON|IEXTEN|ISIG);
term.c_iflag &= ~(BRKINT|ICRNL|INPCK|ISTRIP|IXON);
term.c_cflag &= ~(CSIZE|PARENB);
term.c_cflag |= CS8;
term.c_oflag &= ~(OPOST);
term.c_cc[VMIN]=1;
term.c_cc[VTIME]=0;
cfsetispeed(&term,B19200);
// cfsetospeed(&term,B9600);
if (tcsetattr(fd,TCSANOW,&term)<0)
perror("setattr error");
else
printf("set OK\n");
gettimeofday(&t1,&tz1);
for (i=0;i<19200;i++) {
read(fd,&a,1);
}
gettimeofday(&t2,&tz2);
printf("Begin:%lds%ldus\n",t1.tv_sec,t1.tv_usec);
printf("Stop:%lds%ldus\n",t2.tv_sec,t2.tv_usec);
sub=(t2.tv_sec-t1.tv_sec)*1000000+(t2.tv_usec-t1.tv_usec);
printf("sub=%d\n",sub);
per=(double)sub/19200.00;
printf("per=%fus/byte",per);
}
hero249 2003-08-19
  • 打赏
  • 举报
回复
老大,程序运行后发送函数没有任何显示,接收不到信号。

wwwunix 2003-08-19
  • 打赏
  • 举报
回复
对了不要
tcflush(fd, TCIFLUSH)
wwwunix 2003-08-19
  • 打赏
  • 举报
回复
是的,结果如何?
nriet8357 2003-08-19
  • 打赏
  • 举报
回复
比我厉害多了。
hero249 2003-08-19
  • 打赏
  • 举报
回复
老大,我假如只想发送“1234567890”,另外一个接收,这样您看对么?
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix 标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX 终端控制定义*/
#include <errno.h> /*错误号定义*/

main()
{
int fd;
char buff[1024];
int nbyte;
struct termios oldtio,newtio;

fd=open("/dev/tty00",O_RDWR | O_NOCTTY );
if(fd==-1){
perror("Can not Open the com1:");
exit(-1);
}

tcgetattr(fd,&oldtio);
bzero(&newtio, sizeof(newtio));
newtio.c_lflag &= ~(ECHO|ICANON|IEXTEN|ISIG);
newtio.c_iflag &= ~(BRKINT|ICRNL|INPCK|ISTRIP|IXON);
newtio.c_cflag &= ~(CSIZE|PARENB);
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
sprintf(buff,"1234567890");
while(1){
while((nbyte=write(fd,buff,10))>0){
printf("\nLen%d\n",nbyte);
buff[nbyte+1]='\0';
printf("\n%s",buff);
sleep(1);
}

}

}

接收:
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix 标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX 终端控制定义*/
#include <errno.h> /*错误号定义*/

main()
{
int fd;
char buff[1024];
int nbyte;
int Len;
struct termios oldtio,newtio;

fd=open("\dev\tty01",O_RDWR);
if(fd==-1){
perror("Can not Open the com2:")
exit(-1);
}

tegetattr(fd,&oldtio);
bzero(&newtio, sizeof(newtio));
newtio.c_lflag &= ~(ECHO|ICANON|IEXTEN|ISIG);
newtio.c_iflag &= ~(BRKINT|ICRNL|INPCK|ISTRIP|IXON);
newtio.c_cflag &= ~(CSIZE|PARENB);
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

while(1){
while((nbyte=read(fd,buff,Len)>0){
printf("\nLen%d\n",nbyte);
// buff[nread+1]='\0';
printf("\n%s",buff);
}

}

}
wwwunix 2003-08-19
  • 打赏
  • 举报
回复
添加到设置串口属性那里。
wwwunix 2003-08-18
  • 打赏
  • 举报
回复
先要将其设置为非规范模式:
c_lflag &= ~(ECHO|ICANON|IEXTEN|ISIG);
c_iflag &= ~(BRKINT|ICRNL|INPCK|ISTRIP|IXON);
c_cflag &= ~(CSIZE|PARENB);

23,114

社区成员

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

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