主题 : 从零做起,让我们共同彷徨到天亮。Suffer Together, Share Each Other. 复制链接 | 浏览器收藏 | 打印
路漫漫其修远兮,吾将上下而求索!
级别: 新手上路
UID: 14671
精华: 0
发帖: 40
金钱: 200 两
威望: 40 点
贡献值: 0 点
综合积分: 80 分
注册时间: 2010-02-20
最后登录: 2015-06-26
楼主  发表于: 2010-03-20 05:30

 从零做起,让我们共同彷徨到天亮。Suffer Together, Share Each Other.

一个月前,我决定学Linux,从此我就开始彷徨。
如果你也刚刚开始决定学Linux,你要说你不彷徨,你自己也会说你在撒谎。

由于时间和经济上的考虑,我们不可能每个人都去上课。

而且周围基本上没有可以请教的朋友。

我所经历的苦恼是我们的先行者都经历过的,我想你也不会例外。
我将在我以后的帖子中,公布我真实的实验结果和源程序。希望和我一样处在彷徨中的朋友与我一起做实验。
让我们一起痛苦,彷徨,分享。

这一个月来,我按照韦东山老师书上的步骤重复了N遍。
我在老的2410板上做,看到的都是:
Warning: unable to open an initial console.
Failed to execute /sbin/init.  Attempting defaults...
Kernel panic - not syng: No init found.  Try passing init= option to kernel.
我在2410板子上反复U-BOOT, Kernel, BusyBox. 不知道三个中那个或哪几个我没搞对.

直到前天我放弃了2410板。开始在2440板上测试。
结果在第一次Nand Scrub后就有一个坏块.
第三次烧写Nor Flash 就读不到片子的ID.  .........

几经周折,得到了与2410板子同样的结果.

直到前天才有点结果。终于看到了我的内核和文件系统真正工作了。

和我一样刚刚起步的朋友,如果你的内核和文件系统还没有启动起来的话,我劝你暂时放弃它。
这里有好多老师,我才刚刚起步就如此劝大家,难免有些过分。

为什么呢?
首先,对一个初学者来说,要他把U-boot,Kernel,Busy-box柔到一起来配置,编译,下载,运行,调试,Debug,...,
这无疑是一种灭绝人性的做法。

其次,我们大多数人我想只是想用到Linux的部分功能。而这些功能大部分在厂家提供的现成平台上就能实现。
为什么我们不用高手搭成的现有平台呢?
我想我们不是想刚刚开始就想显示自己比老师牛吧?

再次,我们最初编译出来的内核和做出的文件系统基本上没有什么实用价值。

所以我劝各位暂时放弃。
不要象大办钢铁时期,家家户户都炼铁。

要知道我们大多数人学Linux,是为了用它,而不是研究它。
我们需要一个平台,一个框架,但我们没有必要都去自己编译内核和做文件系统,除非我们的应用到了非这么做不可的阶段。

所以今天我恢复了MINI2440原装的系统。


平台有了,用它来做什么呢?

我首先想到的就是串口通讯。
因为工作中需要它的地方太多了。

我在网上下载了一个程序: http://www.phpfans.net/ask/answer1/4814150316.html
经过了我反复修改,编译,测试。

我在此声明,我在所有的帖子中的例子,如果不是我的原创,我都注明出处。 如果拥有者觉得不妥,请通知我,谢谢!
我以后一定会有实用的原创源码,所有的朋友可以分享。


注意板子上有四个口,CON0和CON1都是同一个信号源,只是电平不一样。

下面是源程序和实验结果:   Frank_Uart-V1.0.rar (3 K) 下载次数:26

/*
   File name: Frank_Uart-V1.0.c
   The source is from http://www.phpfans.net/ask/answer1/4814150316.html
   Modified by Frank Tu on 19MAR10
   Use /dev/ttySAC1 as the testing port. Send back "1" if anything received from /dev/ttySAC1.
   Also output received data to the console which might be another port(/dev/ttySAC0 in this file) by printf("\n%s\n",buff);
   the baud rate for /dev/ttySAC1 is 9600. If it is 115200, my voltage convertor dosn't work properly.
    
   Environment
    Operating system is Ubuntu 9.10    (PC)
    Using MINI2440 original installation: http://www.mini-box.com/mini2440v2
    Using default user directory /home/plg for FTP (MINI2440)
    Board ip address: 192.168.1.230
       Testing directory is: /home/frank/frank/test/Frank_Uart
    
    On PC:
     cd /home/frank/frank/test/Frank_Uart
      arm-linux-g++ -static Frank_Uart.c will create a.out

        frank@frank-laptop:~$ ftp 192.168.1.230
        Connected to 192.168.1.230.
        220 FriendlyARM FTP server (Version 6.4/OpenBSD/Linux-ftpd-0.17) ready.
        Name (192.168.1.230:frank): plg
        331 Password required for plg.
        Password:
        230 User plg logged in.
        Remote system type is UNIX.
        Using binary mode to transfer files.
        ftp> put a.out
        local: a.out remote: a.out
        200 PORT command successful.
        150 Opening BINARY mode data connection for 'a.out'.
        226 Transfer complete.
        518827 bytes sent in 0.30 secs (1705.8 kB/s)
        ftp>
    
    On MINI2440:
        cd /home/plg
        ./a.out

    Now on the console we can see:(PC)
        open ttySAC1
        fcntl=0
        isatty success!
        set done!
    Now on the PC-B: Set hyperTerminal to 9600,8bit, none parity 1 stop bit, none flow control.
        If you input 123456FrankTu, you can see from the console(PC-A):
            1ello

            2ello

            3ello

            4ello

            5ello

            6ello

            Fello

            rello

            aello

            nello

            kello

            Tello

            uello

    On PC-B you can see:
            112131415161F1r1a1n1k1T1u1
    On PC-A, enter Ctrl+C will terminate ttySAC1 communication.

Conenction:

                                                              _________________
                                                             |                 |
                         ------------------------------------|    Router       |--------------------------------
                         |                                   |_________________|                                |
                         |                                            |                                         |
                         |                                            |                                         |
                         |                                            |                                         |
                         |                                            |                                         |
                         |                                            |                                         |
                  PC-A  \|/                                 Mini2440 \|/                              PC-B     \|/
              -----------------                              -----------------                        -----------------
             |                 |                            |                 |                      |                 |
             |                 |   115200,8,0,'N',1         |                 |                      |                 |
             |           RS-232|<-------------------------->|ttySAC0(CON0)    |                      |                 |
             |                 |                            |                 |      __________      |                 |
             |                 |                            |                 |     | Voltage  |     |                 |
             |                 |                            |   ttySAC1(CON2) |<----| Convertor|---->|RS-232           |
             |                 |                            |                 |     |__________|     |(9600,8,0,'N',1) |
             |                 |                            |                 |                      |                 |
             |                 |                            |                 |                      |                 |
             |                 |                            |                 |                      |                 |
              -----------------                              -----------------                        -----------------
                Ubuntu 9.10                                                                             Windows-XP


        CON2:   pin1--TXD1, pin2--RXD1, pin4--GND
            pin1 is on the power switch side

    
*/

#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<termios.h>
int open_port(int fd,int comport)
{
   char *dev[]={"/dev/ttySAC0","/dev/ttySAC1","/dev/ttySAC2"};
   if(comport==1)
    {
    fd=open("/dev/ttySAC0",O_RDWR|O_NOCTTY|O_NDELAY);
    if(-1==fd)
    {
         perror("can't open serial port");
        return(-1);
    }
    else
        printf("open ttySAC0\n");
    }
    if(comport==2)
    {
    fd=open("/dev/ttySAC1",O_RDWR|O_NOCTTY|O_NDELAY);
    if(-1==fd)
    {
         perror("can't open serial port");
        return(-1);
    }
    else
        printf("open ttySAC1\n");
    }
    if(comport==3)
    {
    fd=open("/dev/ttySAC2",O_RDWR|O_NOCTTY|O_NDELAY);
    if(-1==fd)
    {
         perror("can't open serial port");
        return(-1);
    }
    else
        printf("open ttySAC2\n");
    }
    if(fcntl(fd,F_SETFL,0)<0)
       printf("fcnt failed!\n");
    else
    printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));
    if(isatty(STDIN_FILENO)==0)
    printf("standard input is not a terminal device\n");
    else
    printf("isatty success!\n");
    return fd;
    
}
int set_opt(int fd,int nSpeed,int nBits,int nFctl,char nEvent,int nStop)
{
    struct termios newtio,oldtio;
    if(tcgetattr(fd,&oldtio)!=0)
    {
        perror("SetupSerial 1");
        exit(1);
    }
    bzero(&newtio,sizeof(newtio));
    newtio.c_cflag |=CLOCAL | CREAD;//
    newtio.c_cflag &= ~CSIZE;
    //newtio.c_iflag &= ~(ICRNL|IGNCR);
    //newtio.c_lflag &=~(ICANON|ECHO|ECHOE|ISIG);
    //newtio.c_oflag &=~OPOST;
    switch(nBits)
    {
    case 7:
        newtio.c_cflag |= CS7;
        break;
    case 8:
        newtio.c_cflag |= CS8;
        break;
    }
    switch(nFctl)
    {
    case 0:
        newtio.c_cflag &=~CRTSCTS;
        break;
    case 1:
        newtio.c_cflag |=CRTSCTS;
        break;
    case 2:
        newtio.c_cflag |=IXON|IXOFF|IXANY;
        break;
    }
    switch(nEvent)
    {
    case'O':
        newtio.c_cflag |= PARENB;//enble
        newtio.c_cflag |= PARODD;
        newtio.c_iflag |= (INPCK| ISTRIP);
        break;
    case'E':
        newtio.c_iflag |= (INPCK | ISTRIP);
        newtio.c_cflag |= PARENB;
        newtio.c_cflag &= ~PARODD;
        break;
    case'N':
        newtio.c_cflag &=~PARENB;
        break;
    }
    switch(nSpeed)
    {
    case 2400:
        cfsetispeed(&newtio,B2400);
        cfsetospeed(&newtio,B2400);
        break;
    case 4800:
        cfsetispeed(&newtio,B4800);
        cfsetospeed(&newtio,B4800);
        break;
    case 9600:
        cfsetispeed(&newtio,B9600);
        cfsetospeed(&newtio,B9600);
        break;
    case 115200:
        cfsetispeed(&newtio,B115200);
        cfsetospeed(&newtio,B115200);
        break;
    default:
        cfsetispeed(&newtio,B9600);
        cfsetospeed(&newtio,B9600);
        break;
    }
    if(nStop==1)
        newtio.c_cflag &=~CSTOPB;
    else if(nStop==2)
        newtio.c_cflag |= CSTOPB;
    newtio.c_cc[VTIME]=5;
    newtio.c_cc[VMIN]=0;
    newtio.c_lflag &=~(ICANON|ECHO|ECHOE|ISIG);
    newtio.c_oflag &=~OPOST;
    tcflush(fd,TCIFLUSH);
    if((tcsetattr(fd,TCSANOW,&newtio))!=0)
    {
        perror("com set error");
        return -1;
    }
    printf("set done!\n");
    return 0;
}
int main(void)
{
  int fd=-1;
  int i,nread;
   char buff[512]="hello";
  if((fd=open_port(fd,2))<0)
   {
    perror("open_port erro");
        exit(1) ;
   }
  if((i=set_opt(fd,9600,8,0,'N',2))<0)
  {
    perror("set_opt error");
    return 1;
  }
  printf("ID IS: %d",fd);
while(1)
{
  while((nread=read(fd,buff,512))>0)
  {

     //  printf("len %d\n",nread);
//    nread=read(fd,buff,512);
    write(fd, "1" , 1);
    printf("\n%s\n",buff);    
  }
}

if(close(fd)<0){
    perror("close:");
    exit(1);
   }
   else
    printf("close ok");
   exit(0);

}


[ 此帖被franktu在2010-03-21 04:14重新编辑 ]
级别: 新手上路
UID: 812
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
贡献值: 0 点
综合积分: 8 分
注册时间: 2008-06-03
最后登录: 2017-09-13
1楼  发表于: 2010-05-29 23:46

 回 12楼(franktu) 的帖子

好继续努力