调用两次时,串口读取失败

时间:2015-11-14 23:42:57

标签: c linux unix serial-port

已解决的Bellow:读/写字符数错误

我正在尝试读取和写入串口,而且我没有太多的C / C ++经验。

我的端口连接到需要以下设置的运动控制器/驱动程序:

  • 波士顿:57600
  • 数据位:8
  • 奇偶校验:无
  • 停止位:1
  • 流量控制:Xon / Xoff
  • 终结者CRLF

问题:代码将写入和读取我的第一个命令,但它会挂起第二个读取调用。我目前正在阻止,直到我收到至少一个字符,但每个写入的命令都应该生成一个返回。

附加信息:如果我先拔下遥控器并将其重新插入,我只能运行第一次写入/读取。或者,我可以打开一个很酷的术语窗口,设置我的串口并运行一些命令。当我关闭酷术语窗口时,我将能够写入/读取一次。

当前代码:

#include <stdio.h>
#include <string.h>
#include <unistd.h> 
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <time.h>
#include <iostream>
using namespace std;

int open_port(void)
{
        int fd;

     // open file descriptor
        fd = open("/dev/cu.USA19H41P1.1", O_RDWR | O_NOCTTY);

     // if unsucessful
        if (fd == -1)
        {
                printf("open_port: unable to open port. \n");
        }
        else
        {
             // remains open across executables
                fcntl(fd, F_SETFL, 0);
                printf("port is open. \n");
        }
        return (fd);
 }

int configure_port(int fd)
{

     // store terminal port settings
        struct termios port_settings;
        memset(&port_settings, 0, sizeof port_settings);
        if(tcgetattr(fd, &port_settings) !=0)
        {
                printf("error tcgetattr \n");
                cout << errno;
        }

        port_settings.c_iflag = 0;
        port_settings.c_oflag = 0;
        port_settings.c_lflag = 0;
        port_settings.c_cflag = 0;

     // flush
        tcflush(fd, TCIOFLUSH);

     // set baud rate
        cfsetispeed(&port_settings, B57600);
        cfsetospeed(&port_settings, B57600);

     // xon/xoff requirment
        port_settings.c_iflag |= IXON;
        port_settings.c_iflag |= IXOFF;

     // no parity requirement
        port_settings.c_cflag &= ~PARENB;

     // one stop bin requirement
        port_settings.c_cflag &= ~CSTOPB;

     // turn on read
        port_settings.c_cflag |= CREAD;
        port_settings.c_cflag |= CLOCAL;

     // no character processing and 8 bit input
        port_settings.c_cflag &= ~CSIZE;
        port_settings.c_cflag |= CS8;

     // one character blocking 
        port_settings.c_cc[VMIN]  = 1;
        port_settings.c_cc[VTIME] = 5;

     // apply above settings
       if(tcsetattr(fd,TCSANOW, &port_settings) != 0)
        {
                printf("error tcsetattr \n");
                cout << errno;
        }

     // flush buffers one more time
        tcflush(fd, TCIOFLUSH);

        return(fd);
}

int read_write(int fd)
{
        // write to serial port
        ssize_t size=write(fd, "1va?\r\n", 8);
        // wait until output is transmitted
        tcdrain(fd);

        // read from serial port
        char buf[100];
        memset(buf, '\0', sizeof buf);
        ssize_t size2=read(fd, &buf, sizeof(buf));
        cout << buf << endl;
        return(fd);
 }


int main(void)
{
 // open port
 int fd = open_port();

// store old settings
struct termios old_settings;
memset(&old_settings, 0, sizeof old_settings);
tcgetattr(fd,&old_settings);

// configure port
configure_port(fd);

// write/read first command ("1va?\r\n")
read_write(fd);

// write read second command ("1pa?\r\n")
ssize_t size=write(fd, "1pa?\r\n", 8);
tcdrain(fd);
char buf[100];
memset(buf, '\0', sizeof buf);
ssize_t size3=read(fd, &buf, sizeof(buf));
cout << buf;

//close serial port
close(fd);
tcsetattr(fd, TCSANOW, &old_settings);
return 0;
}

1 个答案:

答案 0 :(得分:0)

Here是使用串口设置正确的标志和波特率的一些示例代码:

为我们提供您的代码和更全面的解释,以解决您的问题。你写第一个命令是什么意思?在此命令后,您的设备是否仍然响应?是不是发送了第二个命令?