0

我在我的树莓派上使用下面的代码。我在 TX 和 RX 线之间建立了物理连接。

#include <termios.h> 
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h> 
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>


#define BAUDRATE B9600
#define PORT "/dev/ttyAMA0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1


void signal_handler_IO ( int status );   // definition of signal handler        
volatile int STOP = FALSE;         
int wait_flag = TRUE;                  // TRUE while no signal received


int main()
{
    int fd, res, sen;
    int i;
    struct termios oldtio, newtio;
    struct sigaction saio; /* definition of signal action */
    char comm[10]="12345\n";
    char    resp[10];

   /* open the device to be non-blocking (read will return immediatly) */
    fd = open( PORT, O_RDWR | O_NOCTTY | O_NONBLOCK );

    if (fd < 0) { perror(PORT); return 1; }


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

    /* allow the process to receive SIGIO */
     fcntl( fd, F_SETOWN, getpid() );
     /* Make the file descriptor asynchronous (the manual page says only 
        O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
     fcntl( fd, F_SETFL, FASYNC );


    //tcgetattr(fd,&oldtio); /* save current port settings */ 
    /* set new port settings for canonical input processing */

    newtio.c_cflag = BAUDRATE | CRTSCTS | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR | ICRNL;
    //newtio.c_iflag = IGNPAR; 
    newtio.c_oflag = 0; 
    newtio.c_lflag = ICANON; 
    //newtio.c_lflag = 0; 
    newtio.c_cc[VMIN] = 0; 
    newtio.c_cc[VTIME] = 0;
    tcflush(fd, TCIFLUSH); 
    tcsetattr( fd, TCSANOW, &newtio );

    printf("Write start...\n"); 

    sen = write( fd, comm, 8);

    printf("Write stop...\n %s sen = %d\n", comm,sen);


    printf("Read start...\n");

    res = read( fd, resp, 8 );

    printf("Read stop...\n");

    resp[res] = '\0';

    printf("Response = %s, how much bytes %d\n", resp,res );


    /* restore old port settings */ 
    tcsetattr( fd, TCSANOW, &oldtio );

  return 0;

}


 /***************************************************************************
 * signal handler. sets wait_flag to FALSE, to indicate above loop that     *
 * characters have been received.                                           *
 ***************************************************************************/      
void signal_handler_IO (int status)
{
    printf("received SIGIO signal.\n");
    wait_flag = FALSE;
}

但是当我吃午饭时,它会看到:

写入开始... 写入停止... 12345 sen = 8 读取开始... 收到 SIGIO 信号。读取停止... 响应 = , 多少字节 6

问题是我没有看到任何响应字符串。我收到了 6 个字节,就像我发送的一样。我看到新行就像在 comm[10]="12345\n";

我能做些什么?

我还用 minicom 进行了测试:当我吃午饭时: minicom -b 115200 -o -D /dev/ttyAMA0

在另一个终端窗口上我写: printf '12345' > /dev/ttyAMA0

当 TX 和 RX 连接时,我在 minicom 屏幕上看到了它。可以吗?

4

0 回答 0