【问题标题】:G++ to Matlab serial only reading first messageG++ 到 Matlab 串行只读取第一条消息
【发布时间】:2017-08-11 20:32:57
【问题描述】:

我正在对正在构建的飞行控制系统进行软件循环测试。我正在使用 G++ gnu 处理由飞机的 Matlab 模拟提供给它的飞行信息。

为了让 Matlab 和 g++ 相互通信,我使用了一个虚拟串行端口(连接两个非物理 COM 端口)。因此,我使用串行通信在它们之间发送数据(COM5 通过Virtual Serial Port Driver 虚拟连接到 COM6)。

这让我们遇到了问题:Matlab 只会读取从 G++ 发送的第一条消息,而任何剩余的消息都将返回空。

代码目录:

  1. 打开串行端口(在 G++ 端)
  2. 发送消息和发送停止(在 G++ 端)
  3. 显示我的问题的示例 G++ 代码
  4. 显示我的问题的示例 Matlab 接收代码

打开串口(在 G++ 端)

class SerialPort
{
    HANDLE hCom;
    public:
    SerialPort(char * comportname);
    ClosePort();
    void send(void *buffer, int num_bytes);
    void sendstop(SerialPort sport);
    int readline(unsigned char *buffer);
};

SerialPort::SerialPort(char * comportname)
{
    // set up the serial port
    hCom = CreateFileA(comportname,GENERIC_READ | 
    GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

    if (hCom == INVALID_HANDLE_VALUE) printf("\n*** CreateFile failed with error : %d\n", GetLastError());
    DCB dcb;
    BOOL fSuccess = GetCommState(hCom, &dcb);
    dcb.BaudRate = CBR_115200;    // 115200 baud rate
    dcb.ByteSize = 8;             // 8 bits data
    dcb.Parity = NOPARITY;        // no parity bit
    dcb.StopBits = ONESTOPBIT;    // one stop bit
    dcb.fDtrControl = DTR_CONTROL_DISABLE; // no flow control
    fSuccess = SetCommState(hCom, &dcb);
    if(!fSuccess) printf("\n*** SetCommState failed with error : %d\n", GetLastError());
    fSuccess = SetCommMask(hCom, EV_TXEMPTY | EV_RXCHAR | EV_ERR);
    if(!fSuccess) printf("\n*** SetCommMask failed with error : %d\n", GetLastError());

    COMMTIMEOUTS cto_time;
    GetCommTimeouts(hCom, &cto_time);
    cto_time.ReadIntervalTimeout = 100; // set time-out to 100 milliseconds
    SetCommTimeouts(hCom, &cto_time);
}

2。 发送消息和发送停止(在 G++ 端)

void SerialPort::send(void *buffer, int num_bytes)
{
    DWORD dwWriteCount;
    WriteFile(hCom, buffer, num_bytes, &dwWriteCount, NULL);
}
void SerialPort::sendstop(SerialPort sport){
    int STOPBIT[]={9};//stop bit
    sport.send(STOPBIT,sizeof(STOPBIT));
}

3。 显示我的问题的示例 G++ 代码

#define S_PORT "COM5"
#include "serialport.cpp"//see #1
#include <stdio.h>
#include <stdlib.h>
char serialportname[] = S_PORT; // the serial port to read/write from
SerialPort sp(serialportname);
printf("Waiting for first message from matlab...\n");
char received_string[32];
printf("~:"); sp.readline((unsigned char *)received_string); 
printf("%s\n",received_string);printf("\n");

int main(void){
    unsigned char rep1[]="Hello Plant"; 
    sp.send(rep1,sizeof(rep1));
    sp.sendstop(sp);

    unsigned char rep2[]="Start";
    sp.send(rep2,sizeof(rep2));//This is where the error starts, it cant read the second message
    sp.sendstop(sp);
    return 0;
}

4。 显示我的问题的示例 Matlab 接收代码

s=serial('COM6');
s.Baudrate=115200;
s.StopBits=1;
s.Terminator=9;
fopen(s);
fwrite(s,'Hello Processor');
fwrite(s,'8');%stopbit for Matlab is 9(Tab in ASCII), and GNU side's is 8(Backspace in ASCII)

rep = fgets(s);
rep %displaying

rep2 = fgets(s);%this fails to read "Start" and gets an empty ''
rep2 %displaying
fclose(s);

我的理论是它以某种方式仅读取起始位,或仅读取串行上的一些信息,或者可能未正确读取起始位。

我已经尝试过的

-改变延迟以尝试解决任何可能的同步问题

-每条消息后打开和关闭串口

-写更长的消息,看看是不是读错了部分

-改变停止位

请注意,这些尝试仍然可以告诉我们问题所在,我只是没有看到他们提供的任何信息

关于此主题的其他论坛帖子(没有真正帮助) https://www.mathworks.com/matlabcentral/answers/17004-cannot-read-from-com-port-more-than-once Matlab serial read bug

如果有人能告诉我我错过了什么,我会非常感激不尽。

【问题讨论】:

    标签: c++ matlab serialization g++


    【解决方案1】:

    经过更多的实验,我发现了一个似乎可以让它一直工作的过程。

    要正确发送,您需要:

    1. 通过 fgetl 或 fscanf 或 fgets 获取数据。

    2. 使用flushinput(serialportobject)(清除缓冲区?)

    3. 在发送下一条消息之前,TX 端必须休眠一段时间(对我来说是 13 毫秒);

    示例脚本(使用原始帖子中的代码):

    发送端:

    char rep1[]="hello";
    sp.send(rep1, sizeof(rep1));
    sp.sendstop(sp);
    Sleep(13); //Important for next read
    

    接收端:

    rep=fscanf(s);
    flushinput(s);
    //Next read can go here
    

    【讨论】:

      猜你喜欢
      • 2017-01-02
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多