网络LED矩阵显示器
来源:互联网 发布:网络群发软件 编辑:程序博客网 时间:2024/05/21 10:50
实验内容:
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);
}
}
............
}
- 网络LED矩阵显示器
- Lab 8:网络LED矩阵显示器
- 嵌入式LAB 8:网络LED矩阵显示器
- 嵌入式实验8——网络LED矩阵显示器
- LED显示器
- LED显示器接口原理
- 用LED矩阵屏 显示网络摄像头图像
- 7段LED显示器字符段码表
- bjfuOJ 1230LED显示器1(模拟)
- EXC2618N LED矩阵驱动程序
- 【Arduino】开发入门【七】7段LED显示器
- LED显示器大约有三方面的优点
- 【Arduino】开发入门【七】7段LED显示器
- 显示器
- 显示器
- 显示器
- 显示器
- 显示器
- VELT-0.2.8对STM32开发的支持(3):启动代码
- centos 搭建C++开发环境(gbk编码)
- JAVA复制对象给另一个对象
- keil软件之51与arm兼容性问题解决
- 【一天一道LeetCode】#101. Symmetric Tree
- 网络LED矩阵显示器
- CvMat用法详解
- 进程管理之 atexit()函数
- 关于ping命令的-I选项的一个问题
- EffectiveJava-类和接口
- Flume监控及参数调优
- csdn可能待改进点之28------>手机上csdn的同步需要好久啊?
- Python 多线程 多进程 GIL
- eclipse环境使用及安卓入门