0

我对一块板(ERC 32 处理器)进行了编程,每次从串行端口发送一个字符时都返回一个字符。当我通过 GTKterm 发送一个字符时,一切正常并且电路板正常返回。我正在用 C 编写代码来发送和取回字符;但是,它要么以某种方式失败,要么适用于某些处决,然后突然就不再起作用了。我阅读了很多关于串行端口的问题,但无法弄清楚发生了什么。

我尝试了 3 种方法:

1)阻塞读/写代码
问题:它工作了一段时间,然后读函数被阻塞并且没有占用任何字节。下面的代码

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <sys/types.h>

//Test chars
char d='a',a;

int main()

{
    struct          termios tio;
    struct          termios old_tio;

    //Open serial port
    tty_fd=open("/dev/ttyS1",O_RDWR | O_NOCTTY);

    //Just to check, ir returs 3 always
    printf("fd: %d\n",tty_fd);

    //Get previous configurations
    tcgetattr(tty_fd,&old_tio);

    cfsetospeed(&tio,B19200);            // 19200 baud
    cfsetispeed(&tio,B19200);

     /*CS8     : 8n1 (8bit,no parity,1 stopbit)
     CLOCAL  : local connection, no modem contol
     CREAD   : enable receiving characters*/

    tio.c_iflag &= ~(IXON | IXOFF | IXANY);
    tio.c_oflag = 0;

    tio.c_cflag &= ~PARENB;   /* Disables the Parity Enable bit(PARENB),So No Parity   */
    tio.c_cflag &= ~CSTOPB;   /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */
    tio.c_cflag &= ~CSIZE;   /* Clears the mask for setting the data size             */
    tio.c_cflag |=  CS8|CREAD|CLOCAL;
    tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //non-cannonical

    tio.c_cc[VMIN]=1;
    tio.c_cc[VTIME]=0;

    tcsetattr(tty_fd,TCSANOW,&tio);
    tcflush(tty_fd, TCIFLUSH);
    tcflush(tty_fd, TCOFLUSH);

    //Write a char and waits for a return char immediately after
    bytes_written=write(tty_fd,&d,1);
    printf("bytes written:%d\n",bytes_written);
    read(tty_fd,&a,1);
    printf(" bytes read:%d\n",bytes_read);

    tcsetattr(tty_fd,TCSANOW,&old_tio);
    close(tty_fd);
    return 0;

}

2)线程阻塞读/写代码
问题:读函数阻塞并且不占用任何字节。
注意:我编程的方式是首先初始化读取线程,以确保它开始“观察”端口,而不管任何写入调用。下面的代码

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <pthread.h>

char a='z';
char read_char='?';

void *read_thread(void *arg)
{

     int *tty_fd_ptr= (int*) arg;
    int tty_fd = *tty_fd_ptr;

    unsigned int bytes_read=0;

    //Read until it gets at leats one byte
    while(bytes_read<=0)
    {
        bytes_read=read(tty_fd,&read_char,1);
    }

    printf("char: %c\n",read_char);

    pthread_exit(0);

}


void *test_thread(void *arg)
{

    int *tty_fd_ptr= (int*) arg;
    int tty_fd = *tty_fd_ptr;
    unsigned int bytes_written;

    bytes_written=write(tty_fd,&a,1);
    printf("Bytes written: %d\n",bytes_written);

    pthread_exit(0);

}

int main ()

{

    int count;
    int tty_fd;

    //The port oppening routine and configurations is exactly the same of the
    //previous code

     //Thread ID
     pthread_t tid,tid_2;

     //Create attributes
     pthread_attr_t attr,attr_2;

     pthread_attr_init(&attr);
     pthread_create(&tid, &attr,read_thread,&tty_fd);

     pthread_attr_init(&attr_2);
     pthread_create(&tid_2, &attr_2,test_thread,&tty_fd);


     pthread_join(tid,NULL);
     pthread_join(tid_2,NULL);


    tcsetattr(tty_fd,TCSANOW,&old_tio);
    close(tty_fd);
    return 0;

}

3)带有信号处理程序的非阻塞读/写代码
问题:正如您在代码中看到的,我在主循环(端口观察程序循环)之前放置了一个写调用。端口的信号处理程序也已设置。它写但不读任何东西。下面的代码

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <sys/types.h>

volatile int STOP=FALSE;
int tty_fd;
int bytes_written=0,bytes_read;
char a='z',c='b';

void signal_handler_IO (int status)
     {
        printf("Interrupt\n");
        wait_flag = FALSE;
     }

int main ()

{

    struct sigaction saio;           /* definition of signal action */

    struct termios tio;
    struct termios old_tio;

    tty_fd=open("/dev/ttyS1",O_RDWR | O_NOCTTY | O_NONBLOCK);
    printf("fd: %d\n",tty_fd);

    /* install the signal handler before making the device asynchronous */
     saio.sa_handler = signal_handler_IO;
     sigemptyset(&saio.sa_mask);
     saio.sa_flags = 0;
     saio.sa_restorer = NULL;
     sigaction(SIGIO,&saio,NULL);

     /* allow the process to receive SIGIO */
    fcntl_status=fcntl(tty_fd, F_SETOWN, getpid());

    fcntl_status=fcntl(tty_fd, F_SETFL, FASYNC);

    tcgetattr(tty_fd,&old_tio);

    cfsetospeed(&tio,B19200);            // 19200 baud
    cfsetispeed(&tio,B19200);
    //cfmakeraw(&tio);

     /*CS8     : 8n1 (8bit,no parity,1 stopbit)
     CLOCAL  : local connection, no modem contol
     CREAD   : enable receiving characters*/

    tio.c_iflag &= ~(IXON | IXOFF | IXANY);

    tio.c_oflag=0;

    //tio.c_cflag=CS8|CREAD|CLOCAL;
    tio.c_cflag &= ~PARENB;   /* Disables the Parity Enable bit(PARENB),So No Parity   */
    tio.c_cflag &= ~CSTOPB;   /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */
    tio.c_cflag &= ~CSIZE;   /* Clears the mask for setting the data size             */
    tio.c_cflag |=  CS8|CREAD|CLOCAL;
    tio.c_lflag=0;


    tio.c_cc[VMIN]=1;
    tio.c_cc[VTIME]=0;

    tcsetattr(tty_fd,TCSANOW,&tio);
    tcflush(tty_fd, TCIFLUSH);
    tcflush(tty_fd, TCOFLUSH);

     bytes_written=write(tty_fd,&c,1);
     printf("bytes written: %d\n",bytes_written);


     while (STOP==FALSE)
      {
              usleep(10000);

              /* after receiving SIGIO, wait_flag = FALSE, input is available
                 and can be read */
         if (wait_flag==FALSE)
         {


            bytes_read=read(tty_fd,&a,1);

            printf("bytes read:%d \n",bytes_read);
             wait_flag = TRUE;      /* wait for new input */
         }
       }

    tcsetattr(tty_fd,TCSANOW,&old_tio);
    close(tty_fd);
    return 0;

}

我的目标是在循环系统中制作一个硬件,其中要传输的数据从 linux 端开始,通过 uart 到板,板获取数据,处理并将其返回到计算机,然后循环运行再次直到满足某个停止条件。我为板子编写了一个简单的代码,只是为了在增加复杂性之前测试单个字符循环。

值得注意的是:
-> 如果我编写一个简单的代码,只用一个字符调用一个写入函数并同时执行 GTKterm,我可以在 GTKterm 控制台中看到板响应。
-> 对于代码的阻塞版本,当代码在读取功能中阻塞时,如果我重置电路板,它将采用电路板欢迎消息的第一个字符。这是预期的,但我不知道为什么编程中的写入调用板它不需要,但 GTkterm 需要。
-> 我还在直接 linux 机器上尝试了一些代码,并且遇到了同样的问题,并且 GTKterm 工作正常。

我不确定是否有我没有看到的更棘手的设置。任何帮助将不胜感激。

开发板:ERC32 芯片 (TSC695F) 采用 RTEMS 实时操作系统进行编程。芯片UART是全双工的。

计算机:Windows 7 上的 VMware 虚拟机上的 Mandriva linux。

$ uname -a
Linux localhost.localdomain 2.6.36.2-desktop586-2mnb #1 SMP Wed Dec 22 17:11:08 UTC 2010 i686 i686 i386 GNU/Linux

$ cat /etc/*-release
Mandriva Linux release 2010.2 (Official) for i586

4

0 回答 0