嵌入式实验8——网络LED矩阵显示器

实验八——网络LED矩阵显示器


日期:2016-6-22


一、实验目的

1. 复习socket编程(网络原理课);

2. 实现一个网络访问的LED矩阵显示器

二、实验器材

硬件

Ÿ  Linux实验板卡一块;

Ÿ  5V/1A电源一个;

Ÿ  microUSB线一根;

Ÿ  面包板一块;

Ÿ  8x8LED矩阵一块;

Ÿ  74HC595两个;

Ÿ  360Ω 1/8W电阻8颗;

Ÿ  面包线若干。

软件

Ÿ  交叉编译软件;

三、实验步骤

1. 在面包板上连线,完成外部电路;

2. 编写Linux应用程序,能通过第七次实验的设备驱动程序控制LED矩阵显示字符串,每个字符停留500ms;

直接通过write函数向显示器输入数字字符通过while(1)进行循环输出。

#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int Matrix;
 
#define ALLCHAR "0123456789abcdefghijklmnopqrstuvwxyz"
 
int main(){
    Matrix =open("/dev/matrix", O_WRONLY);
    if (Matrix < 0){
        fprintf(stderr,"Cound not open matrix device");
        exit(1);
}
int index=0;
    while(1){
write(Matrix, ALLCHAR,strlen(ALLCHAR[index]));
index=(index+1)/36;
sleep(500000);
}
return 0;
}

3. 编写Linux应用程序,能通过TCP接受一个连接,将发来的文字在LED矩阵上流动显示出来;


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
 
int Matrix;
int server;
 
#define PORT 8080
#define ADDR "0.0.0.0"
#define QUEUE 20
 
#define BUFF_SIZE 2048
 
int main(){
    // 打开matrix
    Matrix = open("/dev/matrix", O_WRONLY);
    if (Matrix < 0){
        fprintf(stderr, "Coundnot open matrix device\n");
        exit(1);
    }
    // 建立服务器
    int server = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
 
    struct sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr =inet_addr(ADDR);
    serverAddr.sin_port = htons(PORT);
 
    // 绑定ip以及端口
    if (bind(server, (struct sockaddr*)&serverAddr,sizeof(serverAddr)) == -1){
        fprintf(stderr, "Countnot bind %s:%d\n", ADDR, PORT);
        exit(1);
    }
 
    if (listen(server, QUEUE) == -1){
        fprintf(stderr, "Listenerror\n");
        exit(1);
    }
 
    printf("Serverrunning at %s:%d\n", ADDR, PORT);
 
    while (1){
        struct sockaddr_in clientAddr;
        socklen_t length = sizeof(clientAddr);
 
        // 对连入的连接进行处理
        int conn = accept(server, (struct sockaddr*)&clientAddr,&length);
        if (conn < 0){
            fprintf(stderr, "Connecterror");
            exit(1);
        }
 
        printf("Anew connection from %s:%d\n",inet_ntoa(clientAddr.sin_addr), clientAddr.sin_port);
 
        // 处理连接发送过来的字符
        while (1){
            char receiveBuf[BUFF_SIZE];
            int count;
            memset(receiveBuf, 0, sizeof(receiveBuf));
 
            //接收字符
            count = recv(conn, receiveBuf, sizeof(receiveBuf),0);
 
            //如果接收到的字符为空,则表示离开
            if (count == 0){
                close(conn);
                break;
            }
 
            //将接收到的所有字符发送给matrix进行显示
            write(Matrix, receiveBuf, count);
        }
    }
    close(server);
    return 0;
}



4. 用telnet连接Linux板卡,发送字符串来显示。

Linux上用telnet远程连接:

树莓派服务器成功接收:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值