嵌入式技术及应用-【Ubuntu】实验5-串口通讯实验

1.编写并编译基于PC和ARM平台的串口通信源代码,要求实现字符串传输,写出编译过程。
2.分析串口通信程序的源代码,画出串口通信的流程图

(串口名称:ttySAC3新板,ttySAC1或ttySAC2旧板)

(telnet登陆时root的密码:fa 可以在windows(要在控制面板的程序与功能开通telnet),可以在虚拟机)  例:telnet 192.168.0.245

用gcc编写出wwcom_writer可执行文件

/* com_writer.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include "uart_api.h"
//#include "uart_apiarm.h"
#define BUFFER_SIZE 1024
#define HOST_COM_PORT 1
int main(void) 
{
	int fd;
	char buff[BUFFER_SIZE];
	if((fd = open_port(HOST_COM_PORT)) < 0) /* 打开串口 */
	{
		perror("open_port");
		return 1;
	}
          //printf("ok");	
	if(set_com_config(fd, 115200, 8, 'N', 1) < 0) /* 配置串口 */
	{
		perror("set_com_config");
		return 1;
	}	
	do
	{
		printf("Input some words(enter 'quit' to exit):");
		memset(buff, 0, BUFFER_SIZE);
		if (fgets(buff, BUFFER_SIZE, stdin) == NULL)
		{
			perror("fgets");
			break;
		}
		if(write(fd, buff, strlen(buff))>0)
                   {
			printf("The sended words are : %s", buff);
		}
           printf("ok\n");	 
	} while(strncmp(buff, "quit", 4));
do
    //while(1)
	{
		memset(buff, 0, BUFFER_SIZE);
           printf("start read\n");
		if (read(fd, buff, BUFFER_SIZE) > 0)
                    //read(fd, buff, BUFFER_SIZE);
		{
			printf("The received words are : %s\n", buff);
                         printf("success\n");
		}
               // else 
              // {
		//	printf("receive not success");
		//}
                sleep(2);
	} while(strncmp(buff, "ok", 2));
  	close(fd);
  	return 0;
}

用arm-linux-gcc编译出wwcom_reader可执行文件

/* com_reader.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include "uart_apiarm.h"
//#include "uart_api.h"
#define BUFFER_SIZE 1024
#define TARGET_COM_PORT 1
int main(void) 
{
	int fd;
	char buff[BUFFER_SIZE];
	if((fd = open_port(TARGET_COM_PORT)) < 0) /* 打开串口 */
	{
		perror("open_port");
		return 1;
	}
        printf("ok\n");
	if(set_com_config(fd, 115200, 8, 'N', 1) < 0) /* 配置串口 */
	{
		perror("set_com_config");
		return 1;
	}
        printf("wei\n");
	do
    //while(1)
	{
		memset(buff, 0, BUFFER_SIZE);
           printf("start read\n");
		if (read(fd, buff, BUFFER_SIZE) > 0)
                    //read(fd, buff, BUFFER_SIZE);
		{
			printf("The received words are : %s\n", buff);
                         printf("success\n");
		}
               // else 
              // {
		//	printf("receive not success");
		//}
                sleep(2);
	} while(strncmp(buff, "quit", 4));
//while(1);
       write(fd, "ok", 2);
       printf("success ok\n");   
  	close(fd);
  	return 0;
}

ping检查虚拟机与开发板的连通性

./wwcom_writer./wwcom_reader分别执行即可实现字符串的写入和读取

结论:

1、了解linux环境下串行程序设计的方法,设备文件系统的使用方法;

2、掌握终端的主要属性及设置方法;

3、了解主机与开发板的连接和通信(包括串口和网络连接、主机与开发板之间的文件传输)。

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

利威尔·

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值