linux下的rs232串口通讯c代码

RT希望牛人能够给予解答,要是能够通过gcc的程序,谢谢串口线是不是两个都不是针口??... RT
希望牛人能够给予解答,要是能够通过
gcc的程序,谢谢
串口线是不是两个都不是针口??
展开
 我来答
dreong
推荐于2016-03-21
知道答主
回答量:0
采纳率:0%
帮助的人:0
展开全部
补充:
针口的叫“公头”,有孔的叫“母头”,如果没有两个母头的串口线的话,可以使用虚拟机,两个虚拟机之间采用"管道"的方式连接,可达到几乎和双机互联一样的效果。
另外还可以下载一个串口调试助手,用于观察或者发送数据。

#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <errno.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <termios.h>

#include <stdlib.h>

int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)

{

/*

设置串口属性:

fd: 文件描述符

nSpeed: 波特率

nBits: 数据位

nEvent: 奇偶校验

nStop: 停止位

*/

struct termios newtio,oldtio;

if ( tcgetattr( fd,&oldtio) != 0) {

perror("SetupSerial 1");

return -1;

}

bzero( &newtio, sizeof( newtio ) );

newtio.c_cflag |= CLOCAL | CREAD;

newtio.c_cflag &= ~CSIZE;

switch( nBits )

{

case 7:

newtio.c_cflag |= CS7;

break;

case 8:

newtio.c_cflag |= CS8;

break;

}

switch( nEvent )

{

case 'O':

newtio.c_cflag |= PARENB;

newtio.c_cflag |= PARODD;

newtio.c_iflag |= (INPCK | ISTRIP);

break;

case 'E':

newtio.c_iflag |= (INPCK | ISTRIP);

newtio.c_cflag |= PARENB;

newtio.c_cflag &= ~PARODD;

break;

case 'N':

newtio.c_cflag &= ~PARENB;

break;

}

switch( nSpeed )

{

case 2400:

cfsetispeed(&newtio, B2400);

cfsetospeed(&newtio, B2400);

break;

case 4800:

cfsetispeed(&newtio, B4800);

cfsetospeed(&newtio, B4800);

break;

case 9600:

cfsetispeed(&newtio, B9600);

cfsetospeed(&newtio, B9600);

break;

case 115200:

cfsetispeed(&newtio, B115200);

cfsetospeed(&newtio, B115200);

break;

default:

cfsetispeed(&newtio, B9600);

cfsetospeed(&newtio, B9600);

break;

}

if( nStop == 1 )

newtio.c_cflag &= ~CSTOPB;

else if ( nStop == 2 )

newtio.c_cflag |= CSTOPB;

newtio.c_cc[VTIME] = 0;

newtio.c_cc[VMIN] = 0;

tcflush(fd,TCIFLUSH);

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

{

perror("com set error");

return -1;

}

printf("set done!\n");

return 0;

}

int open_port(int fd,int comport)

{

// char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};

long vdisable;

if (comport==1)

{ fd = open( "/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY);

// { fd = open( "/dev/ttyUSB0", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){

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

return(-1);

}

else

printf("open ttyS0 .....\n");

}

else if(comport==2)

{ fd = open( "/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);

if (-1 == fd){

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

return(-1);

}

else

printf("open ttyS1 .....\n");

}

else if (comport==3)

{

fd = open( "/dev/ttyS2", O_RDWR|O_NOCTTY|O_NDELAY);

if (-1 == fd){

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

return(-1);

}

else

printf("open ttyS2 .....\n");

}

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 terminal device\n");

else

printf("isatty success!\n");

printf("fd-open=%d\n",fd);

return fd;

}

int main(void)

{

int fd;

int nread,i;

char buff[]="Hello\n";

if((fd=open_port(fd,1))<0){

perror("open_port error");

return;

}

if((i=set_opt(fd,115200,8,'N',1))<0){

perror("set_opt error");

return;

}

printf("fd=%d\n",fd);

nread=read(fd,buff,8);

printf("nread=%d,%s\n",nread,buff);

close(fd);

return;

}

linux下串口通讯大致就是先打开设备文件,获得文件描述符,然后设置通讯参数,(如波特率什么的),然后就可以像一般文件一样read,write了。

你可以根据上面的例子稍微改动一下做一个发送的程序,然后用串口线双机互联,观察一下效果。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
?>

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式