⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 用于主机通过网络控制和检测S3C44B0 ARM开发板上的相关外设接口,包括服务器端与客户端socket编程
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "main.h"
#include "io.h"

#include "about.h"

#include <stdlib.h>
#include <stdio.h>


#include <winsock.h>

#define CMD_LED 1
#define CMD_KEY 2
#define CMD_EXIT 3
/*
Application Protocol
                       cmd
(0)        client ----------------> server

(1)               if cmd = CMD_LED
        client ----------------->server
                 led1,led2,led3

(2)               if cmd = CMD_KEY
        client <------------------server
                 key1,key2,key3,key4

(3)               cmd = CMD_EXIT
        client -------- X ---------server

*/

//---------------------------------------------------------------------------
//
//      Info
//---------------------------------------------------------------------------
void Info(AnsiString s)
{
        MessageBox(NULL,s.c_str(),"S3C44B0测试",MB_OK | MB_ICONINFORMATION);
}

//---------------------------------------------------------------------------
//
//     Set  Led
//              led = true, on
//              led = false, off
//
//---------------------------------------------------------------------------
void SetLed(int sock,bool l1,bool l2,bool l3)
{
        char buf[4];

         //command = set led
         buf[0] = CMD_LED;
         send(sock, buf,1,0);

        //led1
        if(l1)
                buf[0] = 1;
        else
                buf[0] = 0;
        //led2
        if(l2)
                buf[1] = 1;
        else
                buf[1] = 0;

        //led3
        if(l3)
                buf[2] = 1;
        else
                buf[2] = 0;

         send(sock, buf,3,0);

}
//---------------------------------------------------------------------------
//
//      Read key state
//              key = true, key pressed
//              key = false, key released
//---------------------------------------------------------------------------
bool ReadKey(int sock,bool &k1,bool &k2,bool &k3,bool &k4)
{
        char buf[4];

        //cmd = get key
         buf[0] = CMD_KEY;
         send(sock,buf,1,0);

         //get key
         recv(sock,buf,4,0);

        if(buf[0])
                k1 = true;
        else
                k1 = false;

        if(buf[1])
                k2 = true;
        else
                k2 = false;

        if(buf[2])
                k3 = true;
        else
                k3 = false;

        if(buf[3])
                k4 = true;
        else
                k4 = false;

        return true;
}
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{
        timer->Enabled = false;

        //Win sock init
        WORD wVersionRequested;
        WSADATA wsaData;

        wVersionRequested = MAKEWORD( 2, 0 );

        WSAStartup( wVersionRequested, &wsaData );


        //
        sock = -1;

        //init key
        ShowKey(spKey1,0);
        ShowKey(spKey2,0);
        ShowKey(spKey3,0);
        ShowKey(spKey4,0);

}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::Button1Click(TObject *Sender)
{
        //open connection

         struct sockaddr_in server;
         short port;

        if(sock != -1)
        {
                Info("请先断开网络!");
                return;
        }
         /* 建立套接字 */
         sock = socket(AF_INET, SOCK_STREAM, 0);

         server.sin_family = AF_INET;

         try
          {
                server.sin_addr.s_addr = inet_addr(edIP->Text.c_str());

                 port = edPort->Text.ToInt();
         }
         catch(...)
         {
                 Info("IP地址或端口转换错误!");
                 closesocket(sock);
                 sock = -1;
                 return;

                }
         server.sin_port = htons(port);

         if ( connect(sock, (struct sockaddr*)&server, sizeof(server)) != 0)
         {
                 AnsiString s="连接错误代码:";
                 s += WSAGetLastError();
             Info(s);
             sock = -1;

              return ;
         }

        bool k1,k2,k3,k4;

        //read key state
        if(ReadKey(sock,k1,k2,k3,k4))
        {
                ShowKey(spKey1,k1);
                ShowKey(spKey2,k2);
                ShowKey(spKey3,k3);
                ShowKey(spKey4,k4);
        }

        //set led
        SetLed(sock,sbLed1->Down,sbLed2->Down,sbLed3->Down);



}
void __fastcall TfrmMain::btCloseClick(TObject *Sender)
{

        char buf=CMD_EXIT;

        if(sock == -1)
        {
                Info("请先连接网络!");
                return;
        }

        //disable timer
        if(timer->Enabled)
        {
                btStart->Click();
        }

        send(sock,&buf,1,0);

        closesocket(sock);

        sock = -1;
}
//---------------------------------------------------------------------------
//
//
//---------------------------------------------------------------------------

void __fastcall TfrmMain::sbLed1Click(TObject *Sender)
{
        if(sock == -1)
        {
                Info("请先连接网络");
                return;
        }

        SetLed(sock,sbLed1->Down,sbLed2->Down,sbLed3->Down);
}

//---------------------------------------------------------------------------
void __fastcall TfrmMain::Button4Click(TObject *Sender)
{

        bool k1,k2,k3,k4;

        if(sock == -1)
        {
                Info("请先连接网络");
                return;
        }

        if(ReadKey(sock,k1,k2,k3,k4))
        {
                ShowKey(spKey1,k1);
                ShowKey(spKey2,k2);
                ShowKey(spKey3,k3);
                ShowKey(spKey4,k4);
        }
}
//---------------------------------------------------------------------------
//      Display the key state
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ShowKey(TShape *s,bool on)
{
        if(on)
                s->Brush->Color =  clLime;
        else
                s->Brush->Color =  clGray;
}

//---------------------------------------------------------------------------
void __fastcall TfrmMain::btStartClick(TObject *Sender)
{
        if(sock == -1)
        {
                Info("请先连接网络");
                return;
        }

        if(timer->Enabled)
        {
                btStart->Caption = "开始";
                timer->Enabled = false;
        }
        else
        {

                try
                {
                        timer->Interval = edInterval->Text.ToInt();
                }
                catch(...)
                {
                        Info("字符类型输入错误!");
                        return;
                }
                
                btStart->Caption = "停止";
                timer->Enabled = true;
        }

}
//---------------------------------------------------------------------------


void __fastcall TfrmMain::timerTimer(TObject *Sender)
{
        bool k1,k2,k3,k4;

        //read key state
        if(ReadKey(sock,k1,k2,k3,k4))
        {
                ShowKey(spKey1,k1);
                ShowKey(spKey2,k2);
                ShowKey(spKey3,k3);
                ShowKey(spKey4,k4);
        }

        //set led
        SetLed(sock,sbLed1->Down,sbLed2->Down,sbLed3->Down);

}
//---------------------------------------------------------------------------


void __fastcall TfrmMain::Button3Click(TObject *Sender)
{
        frmAbout->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::FormClose(TObject *Sender, TCloseAction &Action)
{
        char buf=CMD_EXIT;

       if(sock == -1)
                return;

        //disable timer
        if(timer->Enabled)
        {
                btStart->Click();
        }

        send(sock,&buf,1,0);

        closesocket(sock);


}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -