【问题标题】:Unexpected behavior related to write() function与 write() 函数相关的意外行为
【发布时间】:2013-12-12 10:40:51
【问题描述】:

该程序旨在从控制台(Windows XP 上的 cygwin)读取一个字节并将其写入串行端口(以便从 PC 与 AVR 进行通信)。然后它应该从串口读取数据。 AVR 设置为定期发送数据,这可以正常工作(通过超级终端确认)。下面的程序可以正常工作,但是当将数据写入串行端口的行被注释掉时,程序也不会读取。实际上,它不会执行(注释掉的)“write (fd, datatemp, 1);”之前的行要么。

为什么不包括写操作时,程序似乎没有按预期执行?

#include "serialport.h"
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>


/* baudrate settings are defined in <asm/termbits.h>, which is
 * included by <termios.h> */
#ifndef BAUDRATE
#define BAUDRATE B2400
#endif

#define _POSIX_SOURCE 1     /* POSIX compliant source */
#define BUFLEN 4

static int fd, c, res;
static struct termios oldtio, newtio;
static char *device;

int serial_init(char *modemdevice)
{
    /* 
     * Open modem device for reading and writing and not as controlling tty
     * because we don't want to get killed if linenoise sends CTRL-C.
     **/
    device = modemdevice;
    fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY);
    //fd = open (device, O_RDWR | O_NOCTTY );
    //fcntl(fd, F_SETFL, 0);
    if (fd < 0)
      {
      perror (device);
      exit(-1);
      }

    tcgetattr (fd, &oldtio);    /* save current serial port settings */
    bzero (&newtio, sizeof (newtio));   /* clear struct for new port settings */

    /* 
     *BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
     *CRTSCTS : output hardware flow control (only used if the cable has
     *all necessary lines. )
     *CS8     : 8n1 (8bit,no parity,1 stopbit)
     *CLOCAL  : local connection, no modem contol
     *CREAD   : enable receiving characters
     **/
    newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;

    /*
     *IGNPAR  : ignore bytes with parity errors
     *ICRNL   : map CR to NL (otherwise a CR input on the other computer
     *          will not terminate input)
     *          otherwise make device raw (no other input processing)
     **/
    newtio.c_iflag = IGNPAR | ICRNL;

    /*
     * Map NL to CR NL in output.
     *                  */
#if 0
    newtio.c_oflag = ONLCR;
#else
    newtio.c_oflag = 0;
#endif


    /*
     * ICANON  : enable canonical input
     *           disable all echo functionality, and don't send signals to calling program
     **/
#if 1
    newtio.c_lflag = ICANON;
#else
    newtio.c_lflag = 0;
#endif

    /* 
     * initialize all control characters 
     * default values can be found in /usr/include/termios.h, and are given
     * in the comments, but we don't need them here
     *                                       */
    newtio.c_cc[VINTR] = 0; /* Ctrl-c */
    newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
    newtio.c_cc[VERASE] = 0;    /* del */
    newtio.c_cc[VKILL] = 0; /* @ */
    newtio.c_cc[VEOF] = 4;  /* Ctrl-d */
    newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
    newtio.c_cc[VMIN] = 1;  /* blocking read until 1 character arrives */
    newtio.c_cc[VSWTC] = 0; /* '\0' */
    newtio.c_cc[VSTART] = 0;    /* Ctrl-q */
    newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
    newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
    newtio.c_cc[VEOL] = 0;  /* '\0' */
    newtio.c_cc[VREPRINT] = 0;  /* Ctrl-r */
    newtio.c_cc[VDISCARD] = 0;  /* Ctrl-u */
    newtio.c_cc[VWERASE] = 0;   /* Ctrl-w */
    newtio.c_cc[VLNEXT] = 0;    /* Ctrl-v */
    newtio.c_cc[VEOL2] = 0; /* '\0' */

    /* 
     * now clean the modem line and activate the settings for the port
     **/
    tcflush (fd, TCIFLUSH);
    tcsetattr (fd, TCSANOW, &newtio);

    /*
     * terminal settings done, return file descriptor
     **/

    return fd;
}

void serial_cleanup(int ifd){
    if(ifd != fd) {
        fprintf(stderr, "WARNING! file descriptor != the one returned by serial_init()\n");
    }
    /* restore the old port settings */
    tcsetattr (ifd, TCSANOW, &oldtio);
}

void main(void) {
    int fd = serial_init("/dev/ttyS0");

    //while(1)
    //{

        printf("Enter a dutycycle: ");
        unsigned char buffer[BUFLEN] = {0};
        fgets(buffer, BUFLEN, stdin);
        uint8_t data = atoi(buffer);
        unsigned char datatemp[1] = {(unsigned char) data};

        printf("hen");
        write (fd, datatemp, 1);

        int n = 0;
        unsigned char inbuffer[2] = {0};
        int temp = -1;
        //printf("%ld", temp);
        while (temp <= 0)
        {
            //printf("%ld", temp);
            temp = read(fd, inbuffer, 1);

        }
        printf("%u", (uint8_t) inbuffer[0]);

    //}

    serial_cleanup(fd);
}

【问题讨论】:

  • 尝试在printf 函数调用上添加换行符,例如。 printf("hen\n"); 而不是 printf("hen"); 以确定程序在哪里停止执行指令
  • @guillem 是正确的; printf 可能正在缓冲输出,而您只是没有看到它。使用一些\n
  • 让调试输出总是转到stderr,因为它没有缓冲:fprintf(stderr, ....

标签: c gcc serial-port embedded cygwin


【解决方案1】:

尝试查看 fflush() 函数。

也许

fflush(NULL)

甚至

fflush(stdout)

对您的情况会有所帮助。值得在输出到控制台之前和/或之后进行刷新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    相关资源
    最近更新 更多