0

我需要通过串行端口发送二进制数据,在此过程中没有任何字节被重新解释为控制字符。我目前正在按如下方式设置我的串行端口:

#include <windows.h>

// open serial port
HANDLE hSerial;
hSerial = CreateFile ("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

// get serial parameters
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength = sizeof (dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams)) {
    cout << "error getting state\n";
    exit(0);
}

// set serial params
dcbSerialParams.BaudRate = CBR_115200;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity   = NOPARITY;
if (!SetCommState (hSerial, &dcbSerialParams)) {
    cout << "error setting parameters\n";
    exit(0);
}

// set time outs
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 10;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 10;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts (hSerial, &timeouts)) {
    cout << "problem setting timeout values\n";
    exit(0);
} else cout << "timeouts set\n";

当我发出 ReadFile 命令时,我可以毫无问题地获取并显示从 0 到 255 的字节。但我对 WriteFile 没有这样的运气。有没有办法明确设置二进制写入模式?

编辑

好的,这里有更多信息。我有一台windows机器和一台linux单板机通过串口连接,windows端上面的代码如下:

unsigned char temp = 0;

bool keepReading = true;
while (keepReading) {
    DWORD dwBytesRead = 0;
    ReadFile (hSerial, &temp, 1, &dwBytesRead, NULL);
    if (1 == dwBytesRead) cout << (unsigned int) temp << " ";
    if (255 == temp) keepReading = false;
}
cout << endl;

bool keepWriting = true;
char send = 0;
while (keepWriting) {
    DWORD dwBytesWritten = 0;
    WriteFile (hSerial, &send, 1, &dwBytesWritten, NULL);
    send++;
    if (256 == send) keepWriting = false;
}

我在 linux 端的代码如下所示:

int fd = open("/dev/ttymxc0", O_RDWR | O_NOCTTY);
struct termios options;
bzero (options, sizeof(options));
options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
options.c_iflat = IGNPAR;
options.c_oflag = 0;
options.c_lflag = ICANON;
options.c_cc[VMIN] = 1;
options.c_CC[VTIME] = 0;
tcflush (fd, TCIFLUSH);
tcsetattr (fd, ICSANOW, &options);

bool keepWriting = true;
char send = 0;
while (keepWriting) {
    write (fd, &send, 1);
    send++;
    if (256 == send) keepWriting = false;
}

bool keepReading = true;
while (keepReading) {
    char temp = 0;
    int n = read (fd, &temp, 1);
    if (-1 == n) {
        perror ("Read error");
        keepReading = false;
    } else if (1 == n) {
        cout << temp << " ";
    }
    if (256 == temp) keepReading = false;

}
cout << endl;

close(fd);

我在两台机器上启动代码,第一组 while 循环运行良好。Windows 端的终端显示 0 到 255。然后它就坐在那里。如果我输出在 linux 端为第二组 while 循环读取的字节数,它会不断给我 0 个字节。这通常表明端口关闭,但我只是通过它发送了一堆信息,那怎么可能呢?

4

3 回答 3

2

我认为可能发生的事情是 Linux 正在检测中断并重置端口,或者设置规范模式的事实正在搞砸。除了您已有的设置之外,请尝试以下设置:

    options.c_iflag |= IGNBRK;
    options.c_iflag &= ~BRKINT;
    options.c_iflag &= ~ICRNL;
    options.c_oflag = 0;
    options.c_lflag = 0;
于 2013-10-04T20:04:34.817 回答
2

正如 Jonathan Potter 所提到的,您很可能没有关闭 XON/XOFF 流控制。在调用之前添加这些行SetCommState

dcbSerialParams.fOutX = 0;
dcbSerialParams.fInX  = 0;

您可能需要设置的其他一些字段:

dcbSerialParams.fNull = 0;
dcbSerialParams.fDtrControl = DTR_CONTROL_DISABLE;
dcbSerialParams.fRtsControl = RTS_CONTROL_DISABLE;
于 2013-10-07T13:45:12.913 回答
0

好吧,所以我想通了,而是同事做到了。在 linux 端,在文件 /etc/inittab 中我必须注释掉该行:

T0:23:respawn:/sbin/getty -L ttymxc0 115200 vt100

这是以一种使其无法用于接收字节的方式抓取串行端口。我现在看到了预期的输出。

于 2013-10-08T18:49:54.560 回答