在 S5PV210 的 开发板上 使用 串口 收发信息

 

参考学习教程:周立功嵌入式Linux开发教程-(上册)

 

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

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

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

 

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

 

#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <string.h>

//socket 头文件 2017年6月28日09:40:47,所长
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdbool.h>

#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 += -Wall
obj := uart 
src := uart.c
CC  := arm-linux-gcc 

$(obj): $(src)
    $(CC) $(CFLAGS) $^ -o $@ -g

.PHONY: clean
clean:
    -rm $(obj) 

 

posted on 2017-06-29 10:33  所长  阅读(666)  评论(0编辑  收藏  举报

导航