我正在做一個軟件,我正在建立一個飛行控制系統的迴路測試。我正在使用G ++ gnu來處理飛機的Matlab仿真所提供的航班信息。G ++ to Matlab串行只讀第一條消息
要獲得Matlab和G ++來相互交談,我使用的是虛擬串行端口(連接兩個非物理COM端口)。因此,我使用串行通信在它們之間發送數據(COM5通過Virtual Serial Port Driver虛擬連接到COM6)。
這會讓我們的問題:Matlab的將只讀取從G ++發來的第一條消息,和任何剩餘的消息將返回空。
的代碼的目錄:
- 打開串行端口(上G ++側)
- 發送消息和發送停止(在G ++側)
- 例G ++代碼顯示我的問題
- 例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);
我的理論是,它是某種只讀起始位,或僅閱讀一些信息過來串行,或者起始位未被正確讀取。
我已經嘗試過
-Altering延誤試圖解決任何可能的同步問題
-Opening和關閉串口每封郵件
- 寫入到較長的消息後,看看它是否讀錯了部分
- 更改停止位
注意的是,這些努力仍然可以告訴我們關於這個問題,我只是沒有看到他們
其他論壇帖子(S)在這個問題上的任何信息(這並沒有真正的幫助) https://www.mathworks.com/matlabcentral/answers/17004-cannot-read-from-com-port-more-than-once Matlab serial read bug
如果有人可以告訴我,我錯過了什麼,我會非常感激。