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、了解主机与开发板的连接和通信(包括串口和网络连接、主机与开发板之间的文件传输)。