嵌入式LAB 8:网络LED矩阵显示器

Posted tanxiaxuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了嵌入式LAB 8:网络LED矩阵显示器相关的知识,希望对你有一定的参考价值。

前期工作准备:

Mac OS X + Ubuntu14.04虚拟机

器材准备:

树莓派
MAX7219驱动的8x8 LED矩阵
外接显示屏
不需要面包板

实验步骤:

1、编写客户端代码


经典的socket套接字编程,没什么可说的

#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>

using namespace std;

#define SERVER_PORT 6666 
#define BUFFER_SIZE 1000

FILE *MAX7219;


void* ClientThread(void *lpParameter) 

    struct sockaddr addr;
    struct sockaddr_in *addr_v4;

    int clientfd = *(int*) lpParameter;

    char buffer[BUFFER_SIZE + 1];
    string recv_str;

    while (1) 
        if (recv(clientfd, buffer, BUFFER_SIZE, 0) == -1) 
            cout << "recv failed" << endl;
            break;
        

        istringstream in(buffer);
        in >> recv_str;

        fprintf(MAX7219, "%s\\n", recv_str.c_str());
        fflush(MAX7219);
    

    close(clientfd);
    return 0;


int main(int argc, const char * argv[]) 

    if ((MAX7219 = fopen("/dev/MAX7219", "w")) == NULL) 
        cout << "MAX7219 open error!" << endl;
        return 0;
    
    fprintf(MAX7219, "1234\\n");
    fflush(MAX7219);

    socket serverfd, clientfd; 
    struct sockaddr_in saddr, caddr;

    serverfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (serverfd == -1) 
        cout << "socket() failed!" << endl;
        return 0;
    

    memset(&saddr, 0, sizeof(saddr));
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(SERVER_PORT);
    saddr.sin_addr.s_addr = htonl(INADDR_ANY);

    //bind
    bind(serverfd, (struct sockaddr *) &saddr, sizeof(saddr));

    //listen
    int ret = listen(serverfd, 5);
    if (ret == -1) 
        cout << "listen failed!" << endl;
        close(serverfd);
        return 0;
    

    cout << "Waiting for client connecting!" << endl;
    cout << "tips : Ctrl+c to quit!" << endl;

    while (true) 
        //accpet
        socklen_t length = sizeof(caddr);

        clientfd = accept(serverfd, (struct sockaddr *) &caddr, &length);

        cout << "new client" << endl;

        if (clientfd == -1) 
            cout << "accept() failed!" << endl;
            continue;
        

        cout << "Accepted client: " << inet_ntoa(caddr.sin_addr) << ":" << ntohs(caddr.sin_port) << endl;

        pthread_t hThread;
        pthread_create(&hThread, NULL, ClientThread, &clientfd);

        if (hThread < 0) 
            printf("Create Thread Failed!\\n");
            break;
        

        char title[] = "welcome server\\r\\n";
        send(clientfd, title, strlen(title), 0);
    

    close(serverfd);

2、编写客户端的代码

老师说用telnet,不过客户端可以在本地编写了。还是写Java比较方便,不用做复杂的socket、bind、listen、accept什么的。

以上是关于嵌入式LAB 8:网络LED矩阵显示器的主要内容,如果未能解决你的问题,请参考以下文章

嵌入式LAB 7:字符设备驱动程序

嵌入式LAB 7:字符设备驱动程序

嵌入式LAB 6:Linux内核编译

嵌入式LAB 6:Linux内核编译

嵌入式:人机交互接口设计详解

网络LED矩阵显示器