网络LED矩阵显示器
2016-06-25 22:41
531 查看
实验内容:
1. 设计外部设备方案,完成连线;
3. 编写Linux应用程序,能通过第七次实验的设备驱动程序控制LED矩阵显示字符串,每个字符停留500ms;
4. 编写Linux应用程序,能通过TCP接受一个连接,将发来的文字在LED矩阵上流动显示出来;
5. 用telnet连接Linux板卡,发送字符串来显示。
1. 设计方案,连线完成电路
连线方案如下所示:
连接实物图:
2. 编写Linux应用程序,能通过第七次实验的设备驱动程序控制LED矩阵显示字符串,每个字符 停留500ms;
在实验7的基础上,增加一个新的函数Display来输出一串字符。
3. 编写Linux应用程序,能通过TCP接受一个连接,将发来的文字在LED矩阵上流动显示出来;
下图为较为标准的TCP S/C实现流程
按照上面的思想实现TCP连接
4. 用telnet连接Linux板卡,发送字符串来显示
由于上面的程序使用的是阻塞式连接,故无法同时处理多个连接,我们以多线程的方式解决这一问题。
1. 设计外部设备方案,完成连线;
3. 编写Linux应用程序,能通过第七次实验的设备驱动程序控制LED矩阵显示字符串,每个字符停留500ms;
4. 编写Linux应用程序,能通过TCP接受一个连接,将发来的文字在LED矩阵上流动显示出来;
5. 用telnet连接Linux板卡,发送字符串来显示。
1. 设计方案,连线完成电路
连线方案如下所示:
树莓派 | MAX7219 |
5V | VCC |
GND | GND |
3 | DIN |
5 | CS |
7 | CLK |
2. 编写Linux应用程序,能通过第七次实验的设备驱动程序控制LED矩阵显示字符串,每个字符 停留500ms;
在实验7的基础上,增加一个新的函数Display来输出一串字符。
void Display(char* display, int n){ int try_write; int fd; fd = open("/dev/MAX7219",O_RDWD); if(fd==1){ printf("Fail to open.\n"); return -1; } for(int i=0;i<n;i++){ try_write = write(fd,&display[i],1); if(try_write==-1) { printf("Fail to write.\n"); return -1; } Delay_ms(500); } } |
3. 编写Linux应用程序,能通过TCP接受一个连接,将发来的文字在LED矩阵上流动显示出来;
下图为较为标准的TCP S/C实现流程
按照上面的思想实现TCP连接
#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, "Cound not 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, "Count not bind %s:%d\n", ADDR, PORT); exit(1); } if (listen(server, QUEUE) == -1){ fprintf(stderr, "Listen error\n"); exit(1); } printf("Server running 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, "Connect error"); exit(1); } printf("A new 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); re |
4. 用telnet连接Linux板卡,发送字符串来显示
由于上面的程序使用的是阻塞式连接,故无法同时处理多个连接,我们以多线程的方式解决这一问题。
void* serverRecv(void* data){ int conn = *(int*)data; 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; } write(Matrix, receiveBuf, count); } pthread_exit(NULL); return NULL; } ............ int main(){ ............ printf("Server running at %s:%d\n", ADDR, PORT); while (1){ pthread_t thread; struct sockaddr_in clientAddr; socklen_t length = sizeof(clientAddr); int conn = accept(server, (struct sockaddr*)&clientAddr, &length); int result; if (conn < 0){ fprintf(stderr, "Connect error"); exit(1); } printf("A new connection from %s:%d\n", inet_ntoa(clientAddr.sin_addr),clientAddr.sin_port); result = pthread_create(&thread, NULL, serverRecv, &conn); if (result < 0){ printf("Create thread error\n"); exit(1); } } ............ } |
相关文章推荐
- Debian for armel 进展情况
- 学习嵌入式系统需要具备的条件、方法及步骤
- 嵌人式实时操作系统uC/OS在控制工程中的应用
- 嵌入式系统简介
- 当人们谈论物理信息系统(CPS)的时候,他们到底在说什么?
- 几种开源TCPIP协议概述--LwIP,uIP,TinyTcp和uC/IP
- 加入嵌入式系统软件开发人员需掌握技能
- STM32 入门教程 系统时钟 SysTick
- ATM32F10x启动文件的选择
- cubieboard 参考资料
- 关于本博客-------helper2416开发板学习心得
- java的技巧!
- java的浅谈!
- 高手必备的六点!
- java的学习方案!
- java的学习技巧方案分析!
- TI低功耗蓝牙(BLE)—入门篇
- 新人推浪
- 认识RPi
- Linux系统调用