2009年8月24日 星期一

【應用-模組D1】嵌入式系統( Embedded System )專案實作:網路通訊I/O系列 上課實作分享

在這一門課裡,總共完成了1. GPIO Linux 驅動程式、2. UART控制實驗、3. 周邊Linux驅動程式控制、4. TCP/IP Socket Programming、5. POSIX Serial Programming、6. Http Server Implement、7. 再外加一FTP Server Porting移植實作,上述這些實戰範例均是在PXA270 ARM教學板上完成的。


有關TCP/IP Socket Programming的部份,分別在PC上以Java及DEV-C寫出TCP Client端程式;在PXA270 ARM教學板上寫一個TCP Server端程式及板上LED驅動程式,TCP Server端程式可以跟PC上Client端程式互相通訊,接收PC上以Java或DEV-C的TCP Client端程式送來的01字串,可用以控制LED閃爍。


我這裡先貼上在 PC上以Java 語言,課堂上當場 CODING 寫成的 TCP Client 端程式,與在 PXA270 ARM 教學板上寫的 TCP Server 端程式(PS:礙於上課時數,因此TCP Server未加入fork,SO...範例是單人上線方式 ),分享給大家^^。 此程式執行畫面如下:



Java程式碼如下:
/*
 * Java simple Frame Example for PXA270(non fork)
 * Copyright (C) 2009 Yi-Hua, Chiang
 * microcyh@seed.net.tw
 */
import java.awt.*;
import java.lang.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;


public class myFrame extends Frame
{
  private TextField myTextField;
  private Button myButton;
  public myFrame()
  {
    this.setLayout( null );
    this.setBounds( 100,100,300,300 );
    myTextField = new TextField( 10 );
    myTextField.setBounds( 100,50,150,25 );


    myButton = new Button( "Send" );
    myButton.setBounds( 125,100,60,25 );
    myButton.addActionListener(
    new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        //System.out.println( "myButton Click" );
        //System.out.println( myTextField.getText() );
        try
        {
          Socket cliSCK = new Socket();
          cliSCK.connect( new InetSocketAddress( "192.168.10.222" , 2222 ) );
          OutputStream OS = cliSCK.getOutputStream();
          OS.write( myTextField.getText().getBytes() );
          OS.close();
          cliSCK.close();
        }
        catch( Exception err)
        {
          System.out.println( "Exception..." );
        }
      }
    }
    );
    this.addWindowListener(
     new WindowAdapter()
     {
      public void windowClosing(WindowEvent e)
      {
        System.exit(0);
      }
     }
    );
    this.add(myTextField);
    this.add(myButton);
    this.setVisible( true );
  }
  public static void main( String[] para )
  {
    new myFrame();
  }
}


TCP Server端程式碼如下:


/*
 * TCP Server Example for PXA270(non fork)
 * Copyright (C) 2009 Yi-Hua, Chiang
 * microcyh@seed.net.tw
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
  int sd, cli_sd;
  struct sockaddr_in svrsock, clisock;


  int len_clisock = sizeof(clisock);
  char *MESSBUF, firstChar;
  if( (sd = socket( AF_INET, SOCK_STREAM, 0 ) ) != -1 )
  {
    memset( &svrsock, 0, sizeof(struct sockaddr_in) );
    svrsock.sin_family = AF_INET;
    svrsock.sin_addr.s_addr = inet_addr("192.168.10.222");
    svrsock.sin_port = htons( 2222 );
    if( (bind( sd , (struct sockaddr *)&svrsock , sizeof(svrsock) )) == 0 )
    {
      if( listen( sd, 1 ) == 0 )
      {
        int fd, x1;
        fd = open( "/dev/LED", O_RDWR );
        while( 1 )
        {
          if( (cli_sd = accept( sd, (struct sockaddr *)&clisock , &len_clisock ) ) != -1)
          {
            MESSBUF = (char*)malloc( 10 );
            read(cli_sd, MESSBUF , 10 );
            printf( "MESSBUF=%s\r\n", MESSBUF );
            close( cli_sd );
            for( x1=0;x1 <10;x1++ )
            {
              write( fd , (void *)(&MESSBUF[x1]) , sizeof(char) *1  );
              sleep( 1 );
            }
          }
        }
        close( fd );
      }
    }
    close( sd );
  }
  return 0;
}


沒有留言:

張貼留言

FPGA Verilog 的學習經驗,提供給要入門的新手

今天簡單說說 FPGA Verilog 的學習經驗,提供給要入門的新手: 1.對自己寫的FPGA Verilog程式,所生成的數位電路要心中有數。 這一點個人認為很重要,就正如寫 C語言,心中要能生成對應的組合語言一樣,我是這樣要求自己的。 雖然 FPGA Verilog語言...