博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在 S5PV210 的 开发板上 使用 串口 收发信息
阅读量:6973 次
发布时间:2019-06-27

本文共 3151 字,大约阅读时间需要 10 分钟。

 

参考学习教程:

 

材料:首先 准备一个 安装好 Linux 的 开发板  

 使用  xshell 工具 连接 开发板  ,winscp 工具 连接 开发板  ,  准备 一个 Ubuntu  32位 ,装上 交叉编译链。。

使用下面 代码 和 Makefile 文件 进行编译  ,生成的  执行 文件  利用 winscp 软件复制到 Linux开发板上 ,利用 xshell 运行 这个可执行文件。

 

下面    代码的  功能  是   打开 串口 ,进行 等待接收 串口的 数据 ,接收完毕 后 利用 printf 打印出来 ,然后给串口返回一个  hello ,这个 是 在 led的 基础上更改的 。。。2017年6月29日10:31:03

 

#include 
#include
#include
#include
#include
#include
#include
#include
#include
//socket 头文件 2017年6月28日09:40:47,所长#include
#include
#include
#include
#include
#define LED_PATH "/sys/devices/platform/x210-led"#define UART0_PATH "/dev/ttyS0"#define OPEN_LED "1"#define OFF_LED "0"#define LED1 1#define LED2 2#define LED3 3#define LED4 4#define ledOperationTypeOPEN 1#define ledOperationTypeOFF 0#define SERVER_IP "192.168.10.11"#define SERVER_PORT ((uint16_t)7007)int led(int ledNumber,int ledOperationType){ char path[40],data[2]; int fd, ret, flag; strcpy(path, LED_PATH); if ( ledNumber == 1 ) strcat(path, "/led1"); else if( ledNumber == 2 ) strcat(path, "/led2"); else if( ledNumber == 3 ) strcat(path, "/led3"); else if( ledNumber == 4 ) strcat(path, "/led4"); else return -1; printf("打开路径%s文件",path); fd = open(path, O_RDWR);//打开/sys/devices/platform/x210-led路径下的 led ledNumber 文件 if( fd < 0 ) //判断是否打开失败 { perror("open"); return -2; } else { printf("led%d 文件打开成功.\r\n",ledNumber); } if( ledOperationType == 1) ret = write(fd, OPEN_LED, strlen(OPEN_LED) );//文件写入内容: OPEN_LED else if( ledOperationType == 0) ret = write(fd, OFF_LED, strlen(OFF_LED) ); //文件写入内容: OFF_LED else return -3; if( ret < 0 ) { perror("write"); return -4; } else { flag =1; printf("led%d 文件写入ledOperationType(1:打开LED 0:关闭LED): %d 数据成功.\r\n",ledNumber,ledOperationType); }/* for(;;) { //闪烁 LED1 if( flag == 1 ) {//如果LED灯 是打开的状态 就关闭LED灯 flag = 0; ret = write(fd, OFF_LED, strlen(OFF_LED) );//文件写入内容: OFF_LED } else {//如果LED灯 是关闭的状态 就打开LED灯 flag = 1; ret = write(fd, OPEN_LED, strlen(OPEN_LED) );//文件写入内容:OPEN_LED } sleep(1); } */ printf("关闭文件,退出进程!\r\n"); close(fd);//文件写入完毕后 要进行关闭文件 return 0; }int main(int argc, char *argv[]){ int flag =0 ; int fd; int len, i,ret; char buf[64] = "hello GXP!"; char responseMessage[64] = "hello GXP!\r\n"; fd = open(UART0_PATH, O_RDWR | O_NOCTTY); if(fd < 0) { perror(UART0_PATH); printf("打开串口0 失败!\r\n"); return -1; } else { printf("打开串口0 成功!\r\n"); } for(;;) { (void)memset(buf, 0, sizeof(buf)); len = read(fd, buf, sizeof(buf)); if (len < 0) { printf("read error \n"); return -1; } printf("%s", buf); len = write(fd, responseMessage, strlen(responseMessage)); if (len < 0) { printf("write data error \n"); } } /* for(;;) { //闪烁 LED1 if( flag == 1 ) {//如果LED灯 是打开的状态 就关闭LED灯 flag = 0; led( LED1 ,ledOperationTypeOFF ); } else {//如果LED灯 是关闭的状态 就打开LED灯 flag = 1; led( LED1 ,ledOperationTypeOPEN ); } sleep(1); } */ return(0); }

 

 

注意 下面 是 Makefile 文件  :  其中 arm-gcc-linux  在 安装交叉 编译器 的时候被我 重定义了  应该是 :arm-none-linux-gnueabi-gcc

 

CFLAGS += -Wallobj := uart src := uart.cCC  := arm-linux-gcc $(obj): $(src)    $(CC) $(CFLAGS) $^ -o $@ -g.PHONY: cleanclean:    -rm $(obj)

 

转载于:https://www.cnblogs.com/suozhang/p/7093170.html

你可能感兴趣的文章
网络编程之urllib
查看>>
Simple Python Dictionary :)
查看>>
c语言程序设计第六次作业--循环结构(二)
查看>>
【转】WebView的JavaScript与本地代码三种交互方式
查看>>
xml转换为对象 微信接口
查看>>
软件产品案例分析
查看>>
HTML表单
查看>>
四元数
查看>>
bzoj2125: 最短路
查看>>
P4781 【模板】拉格朗日插值
查看>>
jzoj5984. 【北大2019冬令营模拟2019.1.1】仙人掌 (分块)
查看>>
python jenkins api
查看>>
电梯调度算法的实现
查看>>
IIPC--share memory
查看>>
前端之html5和css3
查看>>
View绘制机制
查看>>
跟KingDZ学HTML5之四 继续探究Canvas之路径
查看>>
054_VisualForce Ajax 01
查看>>
Android性能优化问题总结
查看>>
html5中 背景自适应
查看>>