在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
芯片精品文章合集(500篇!)    创芯人才网--重磅上线啦!
查看: 3986|回复: 1

關於S3C2410 UART (kernel 2.4)

[复制链接]
发表于 2006-7-22 22:20:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <termios.h>
#include <linux/slab.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/time.h>        //以上為一些相關的標頭檔設定
#define DEFAULT_PHYS_DEV "/dev/ttyS0"
static char* physdev = DEFAULT_PHYS_DEV;
static int phys_fd;
void TtySetup(int tty, int speed);        //宣告終端機設定的子程式
int BaudrateSetSpeed(int bauds);    //宣告Baudrate設定的子程式
/******************主程式開始****************************/
int main(int argc, char ** argv)
{
     if ((phys_fd = open(physdev, O_RDWR)) < 0)    // 開啟/dev/ttyS0
   {
     perror("could not open phys dev .\n");        //若開啟失敗則印出訊息
     exit(1);
   }
   tcflush(phys_fd, TCIOFLUSH);                  //清除輸入與輸出的資料
   printf("Initial ttyS0 ........\n");
   TtySetup(phys_fd, 4800);                         //設定序列埠的baudrate
      char databuf[255];
     char * a;
     int    fd1,len,maxfd;  /* 輸入源  */
     fd_set readfs;    /* 檔案敘述結構設定 */
     fd1=open(physdev, O_RDWR);
     maxfd= sizeof ( fd1 ) + 1;
     if (fd1<0)
        exit(0);
while(1)
{                                                                     
char echoBuffer[]="1234567";
  int timeLen = strlen(echoBuffer);
          FD_SET(fd1, &readfs);  // 測試輸? ?? 1
             select(maxfd, &readfs, NULL, NULL, NULL);
            if(write(phys_fd,echoBuffer,1)==-1)
     {
           cerr<< "Error:write()" <<endl;
           exit(1);
     }
                    printf("echoBuffer  = %s\n",echoBuffer);
echoBuffer[timeLen]='\0';
                                                
   //respond echo inforation
/********************************傳送資料**************************/

        //exit the child process
// close(connfd);
  memset(echoBuffer,' ',strlen(echoBuffer));
}//end of for(;;)
}
}
/*********************設定序列埠****************************/
void TtySetup(int tty, int speed)
{
   int baudrate;
   struct termios t;                        //宣告t 為termios結構
   baudrate=BaudrateSetSpeed(speed);        //呼叫Buadrate設定的子程式
   if (tcgetattr(tty, &t) < 0)              //取得目前序列埠的設定
   {
     perror("tcgetattr");
     exit(1);
   }
   cfmakeraw(&t);                        //自動產生一些設定
   t.c_cflag &= ~CBAUD;
   t.c_cflag |= baudrate | CS8 | CLOCAL;
   t.c_oflag = 0;                         // 關閉output processing
   t.c_lflag |= 0;                         // 無局部模式(local mode)
   
if (tcsetattr(tty, TCSANOW, &t) < 0)    //立刻寫入己更改的設定
   {
     perror("TtySetup : tcsetattr");
     printf("set error!!\n");
     exit(1);
   }
   return;
}
/*******************Baudrate設定**************************/
int BaudrateSetSpeed(int bauds)
{
   int SpeedBaud = 0;
   switch(bauds)                                //選擇Baudrate
   {
case 4800:
SpeedBaud=B4800;
break;
    case 9600:
             SpeedBaud = B9600;
             break;
    case 38400:
             SpeedBaud = B38400;
             break;
    default:
             printf("Bad baudrate %d.\n", bauds);
             break;
   }
   printf("baudate %d.\n",bauds);
   return SpeedBaud;                                //傳回Baudrate值
}



同樣程式在kernel2.6的PXA255開發版,可以進行傳輸及接收動作,
但在kernel2.4的S3C2410開發版無法進行傳送以及接收。

請問各位可能是什麼原因?
有沒有解決的方法?
若有範例程式或者不錯的解決方式麻煩提供一下。
謝謝...!!
 楼主| 发表于 2006-7-23 13:10:56 | 显示全部楼层

kernel2.4的S3C2410开发版

#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <termios.h>
#include <linux/slab.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/time.h>        //以上为一些相关的标头档设定
#define DEFAULT_PHYS_DEV "/dev/ttyS0"
static char* physdev = DEFAULT_PHYS_DEV;
static int phys_fd;
void TtySetup(int tty, int speed);        //宣告终端机设定的子程式
int BaudrateSetSpeed(int bauds);    //宣告Baudrate设定的子程式
/******************主程式开始****************************/
int main(int argc, char ** argv)
{
     if ((phys_fd = open(physdev, O_RDWR)) < 0)    // 开启/dev/ttyS0
   {
     perror("could not open phys dev .\n");        //若开启失败则印出讯息
     exit(1);
   }
   tcflush(phys_fd, TCIOFLUSH);                  //清除输入与输出的资料
   printf("Initial ttyS0 ........\n");
   TtySetup(phys_fd, 4800);                         //设定序列埠的baudrate
      char databuf[255];
     char * a;
     int    fd1,len,maxfd;  /* 输入源  */
     fd_set readfs;    /* 档案叙述结构设定 */
     fd1=open(physdev, O_RDWR);
     maxfd= sizeof ( fd1 ) + 1;
     if (fd1<0)
        exit(0);
while(1)
{                                                                     
char echoBuffer[]="1234567";
  int timeLen = strlen(echoBuffer);
          FD_SET(fd1, &readfs);  // 测试输? ?? 1
             select(maxfd, &readfs, NULL, NULL, NULL);
            if(write(phys_fd,echoBuffer,1)==-1)
     {
           cerr<< "Error:write()" <<endl;
           exit(1);
     }
                    printf("echoBuffer  = %s\n",echoBuffer);
echoBuffer[timeLen]='\0';
                                                
   //respond echo inforation
/********************************传送资料**************************/

        //exit the child process
// close(connfd);
  memset(echoBuffer,' ',strlen(echoBuffer));
}//end of for(;;)
}
}
/*********************设定序列埠****************************/
void TtySetup(int tty, int speed)
{
   int baudrate;
   struct termios t;                        //宣告t 为termios结构
   baudrate=BaudrateSetSpeed(speed);        //呼叫Buadrate设定的子程式
   if (tcgetattr(tty, &t) < 0)              //取得目前序列埠的设定
   {
     perror("tcgetattr");
     exit(1);
   }
   cfmakeraw(&t);                        //自动产生一些设定
   t.c_cflag &= ~CBAUD;
   t.c_cflag |= baudrate | CS8 | CLOCAL;
   t.c_oflag = 0;                         // 关闭output processing
   t.c_lflag |= 0;                         // 无局部模式(local mode)
   
if (tcsetattr(tty, TCSANOW, &t) < 0)    //立刻写入己更改的设定
   {
     perror("TtySetup : tcsetattr");
     printf("set error!!\n");
     exit(1);
   }
   return;
}
/*******************Baudrate设定**************************/
int BaudrateSetSpeed(int bauds)
{
   int SpeedBaud = 0;
   switch(bauds)                                //选择Baudrate
   {
case 4800:
SpeedBaud=B4800;
break;
    case 9600:
             SpeedBaud = B9600;
             break;
    case 38400:
             SpeedBaud = B38400;
             break;
    default:
             printf("Bad baudrate %d.\n", bauds);
             break;
   }
   printf("baudate %d.\n",bauds);
   return SpeedBaud;                                //传回Baudrate值
}



同样程式在kernel2.6的PXA255开发版,可以进行传输及接收动作,
但在kernel2.4的S3C2410开发版无法进行传送以及接收。

请问各位可能是什么原因?
有没有解决的方法?
若有范例程序或者不错的解决方式麻烦提供一下。
谢谢...!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /2 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-4-26 21:31 , Processed in 0.033895 second(s), 10 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表