C语言实现两台电脑通过串口通信
2013-02-23 13:01
302 查看
用C语言实现在发送端控制台输入字符串并在接收端接收显示的功能。
/*********************server.c****************/
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<termios.h>
#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS0"
#define STOP '@'
int main(){
int fd,c=0,res;
struct termios oldtio,newtio;
char ch,s1[20];
printf("start...\n");
fd=open(MODEMDEVICE,O_RDWR | O_NOCTTY);
if(fd<0)
{
perror(MODEMDEVICE);
exit(1);
}
printf("open...\n");
tcgetattr(fd,&oldtio);
bzero(&newtio,sizeof(newtio));
newtio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD;
newtio.c_iflag=IGNPAR;
newtio.c_oflag=0;
newtio.c_lflag=ICANON;
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
printf("writing...\n");
while(1)
{
while((ch=getchar())!='@')
{
s1[0]=ch;
res=write(fd,s1,1);
}
s1[0]=ch;
s1[1]='\n';
res=write(fd,s1,2);
break;
}
printf("close...\n");
close(fd);
tcsetattr(fd,TCSANOW,&oldtio);
return 0;
}
/**************client.c***************/
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<termios.h>
#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS0"
int main()
{
int fd,c=0,res;
struct termios oldtio,newtio;
char buf[256];
printf("start ...\n");
fd=open(MODEMDEVICE,O_RDWR | O_NOCTTY);
if(fd<0)
{
perror(MODEMDEVICE);
exit(1);
}
printf("open...\n");
tcgetattr(fd,&oldtio);
bzero(&newtio,sizeof(newtio));
newtio.c_cflag=BAUDRATE | CS8 | CLOCAL | CREAD;
newtio.c_iflag=IGNPAR;
newtio.c_oflag=0;
newtio.c_lflag=ICANON;
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
printf("reading...\n");
while(1)
{
res=read(fd,buf,255);
buf[res]=0;
printf("res=%d vuf=%s\n",res,buf);
if(buf[0]=='a') break;
}
printf("close...\n");
close(fd);
tcsetattr(fd,TCSANOW,&oldtio);
return 0;
}
发送端截图:
接受端截图:
(-----------that's all-----------)
/*********************server.c****************/
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<termios.h>
#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS0"
#define STOP '@'
int main(){
int fd,c=0,res;
struct termios oldtio,newtio;
char ch,s1[20];
printf("start...\n");
fd=open(MODEMDEVICE,O_RDWR | O_NOCTTY);
if(fd<0)
{
perror(MODEMDEVICE);
exit(1);
}
printf("open...\n");
tcgetattr(fd,&oldtio);
bzero(&newtio,sizeof(newtio));
newtio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD;
newtio.c_iflag=IGNPAR;
newtio.c_oflag=0;
newtio.c_lflag=ICANON;
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
printf("writing...\n");
while(1)
{
while((ch=getchar())!='@')
{
s1[0]=ch;
res=write(fd,s1,1);
}
s1[0]=ch;
s1[1]='\n';
res=write(fd,s1,2);
break;
}
printf("close...\n");
close(fd);
tcsetattr(fd,TCSANOW,&oldtio);
return 0;
}
/**************client.c***************/
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<termios.h>
#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS0"
int main()
{
int fd,c=0,res;
struct termios oldtio,newtio;
char buf[256];
printf("start ...\n");
fd=open(MODEMDEVICE,O_RDWR | O_NOCTTY);
if(fd<0)
{
perror(MODEMDEVICE);
exit(1);
}
printf("open...\n");
tcgetattr(fd,&oldtio);
bzero(&newtio,sizeof(newtio));
newtio.c_cflag=BAUDRATE | CS8 | CLOCAL | CREAD;
newtio.c_iflag=IGNPAR;
newtio.c_oflag=0;
newtio.c_lflag=ICANON;
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
printf("reading...\n");
while(1)
{
res=read(fd,buf,255);
buf[res]=0;
printf("res=%d vuf=%s\n",res,buf);
if(buf[0]=='a') break;
}
printf("close...\n");
close(fd);
tcsetattr(fd,TCSANOW,&oldtio);
return 0;
}
发送端截图:
接受端截图:
(-----------that's all-----------)
相关文章推荐
- C语言实现两台电脑通过串口通信
- C语言通过串口实现两台电脑通信
- 两台电脑间通过网线建立局域网实现文件的共享
- 怎么样让台式机通过笔记本无线上网 台式机共享上网 笔记本当无线路由器 两台电脑一根网线
- python实现简单socket程序在两台电脑之间传输消息的方法
- 指纹模块与电脑通信 - java使用RXTX包实现串口通信
- 两台WEB服务器通过DNS轮询功能实现负载均衡
- C语言学习:通过数组来实现栈
- 如何实现两台电脑共享
- Java代码实现两台电脑之间传文件(1)
- C语言通过二叉树实现单词出现频率的统计
- C语言实现通过日期计算这是一年中的第几天
- 【翻译】如何通过使用两台电脑变得更加有效率
- PAT1003. 我要通过!(C语言实现,多种解法)
- 两台电脑不通过交换机直接互联
- C语言实现密码输入显示星号 VS2010 亲测通过
- python通过微信发送邮件实现电脑关机
- 通过似然分析预测丢失的边和虚假的边--的C语言代码实现
- 通过冒泡排序(C语言实现)学习回调函数设计原理
- PAT Basic 1003. 我要通过!(20)(C语言实现)