我正在尝试使用串行连接从RS-232读取C ++的串行库,并在以下位置找到一些示例: http://www.codeproject.com/Articles/992/Serial-library-for-C
是否有任何信号检测方法或事件让我知道。 我使用串行到USB连接器将 Rx 和 Neutral 连接到电路板,在Visual Studio 2013中使用串行库并用C ++编写代码。
我想将从我的电路板传输的ASCII数据写入文本文件,这样我可以很容易地从文本文件中解析数据。
我的主板仅在重新启动时才发送数据。 我是串口连接的新手,当电路板复位时,我无法弄清楚如何让我的程序理解读取数据。
我正在使用监听程序:
#define STRICT
#include <tchar.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "Serial.h"
//C++ headers
#include <iostream>
#include <fstream>
#include <string>
enum { EOF_Char = 27 };
int ShowError (LONG lError, LPCTSTR lptszMessage)
{
// Generate a message text
TCHAR tszMessage[256];
wsprintf(tszMessage,_T("%s\n(error code %d)"), lptszMessage, lError);
// Display message-box and return with an error-code
::MessageBox(0,tszMessage,_T("Listener"), MB_ICONSTOP|MB_OK);
return 1;
}
//int __cdecl _tmain (int /*argc*/, char** /*argv*/)
int WINAPI _tWinMain(HINSTANCE /*hInst*/, HINSTANCE /*hInstPrev*/, LPTSTR /*lptszCmdLine*/, int /*nCmdShow*/)
{
CSerial serial;
LONG lLastError = ERROR_SUCCESS;
const char* Port_name = "COM3";
// Attempt to open the serial port (COM1)
lLastError = serial.Open(_T(Port_name),0,0,false);
if (lLastError != ERROR_SUCCESS)
return ::ShowError(serial.GetLastError(), _T("Unable to open COM-port"));
// Setup the serial port (9600,8N1, which is the default setting)
lLastError = serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
if (lLastError != ERROR_SUCCESS)
return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port setting"));
// Register only for the receive event
lLastError = serial.SetMask(CSerial::EEventBreak |
CSerial::EEventCTS |
CSerial::EEventDSR |
CSerial::EEventError |
CSerial::EEventRing |
CSerial::EEventRLSD |
CSerial::EEventRecv);
if (lLastError != ERROR_SUCCESS)
return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port event mask"));
// Use 'non-blocking' reads, because we don't know how many bytes
// will be received. This is normally the most convenient mode
// (and also the default mode for reading data).
lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking);
if (lLastError != ERROR_SUCCESS)
return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port read timeout."));
// Keep reading data, until an EOF (CTRL-Z) has been received
bool fContinue = true;
do
{
// Wait for an event
lLastError = serial.WaitEvent();
if (lLastError != ERROR_SUCCESS)
return ::ShowError(serial.GetLastError(), _T("Unable to wait for a COM-port event."));
else
return ::ShowError(serial.GetLastError(), _T("Waiting for a COM-port event."));
// Save event
const CSerial::EEvent eEvent = serial.GetEventType();
// Handle break event
if (eEvent & CSerial::EEventBreak)
{
printf("\n### BREAK received ###\n");
}
// Handle CTS event
if (eEvent & CSerial::EEventCTS)
{
printf("\n### Clear to send %s ###\n", serial.GetCTS()?"on":"off");
}
// Handle DSR event
if (eEvent & CSerial::EEventDSR)
{
printf("\n### Data set ready %s ###\n", serial.GetDSR()?"on":"off");
}
// Handle error event
if (eEvent & CSerial::EEventError)
{
printf("\n### ERROR: ");
switch (serial.GetError())
{
case CSerial::EErrorBreak: printf("Break condition"); break;
case CSerial::EErrorFrame: printf("Framing error"); break;
case CSerial::EErrorIOE: printf("IO device error"); break;
case CSerial::EErrorMode: printf("Unsupported mode"); break;
case CSerial::EErrorOverrun: printf("Buffer overrun"); break;
case CSerial::EErrorRxOver: printf("Input buffer overflow"); break;
case CSerial::EErrorParity: printf("Input parity error"); break;
case CSerial::EErrorTxFull: printf("Output buffer full"); break;
default: printf("Unknown"); break;
}
printf(" ###\n");
}
// Handle ring event
if (eEvent & CSerial::EEventRing)
{
printf("\n### RING ###\n");
}
// Handle RLSD/CD event
if (eEvent & CSerial::EEventRLSD)
{
printf("\n### RLSD/CD %s ###\n", serial.GetRLSD()?"on":"off");
}
// Handle data receive event
if (eEvent & CSerial::EEventRecv)
{
// Read data, until there is nothing left
DWORD dwBytesRead = 0;
char szBuffer[101];
do
{
// Read data from the COM-port
lLastError = serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead);
if (lLastError != ERROR_SUCCESS)
return ::ShowError(serial.GetLastError(), _T("Unable to read from COM-port."));
if (dwBytesRead > 0)
{
// Finalize the data, so it is a valid string
szBuffer[dwBytesRead] = '\0';
//writing data to text file
std::ofstream o("save.txt");
o << "Data: " << szBuffer << std::endl << "BytesRead: " <<dwBytesRead <<std::endl;
// Display the data
printf("%s", szBuffer);
// Check if EOF (CTRL+'[') has been specified
if (strchr(szBuffer,EOF_Char))
fContinue = false;
}
}
while (dwBytesRead == sizeof(szBuffer)-1);
}
}
while (fContinue);
// Close the port again
serial.Close();
return 0;
}
我确实在我的while语句中设置了一些断点,并试图重新启动电路板。然后我将数据写入我的文本文件中。
Trail 1:
数据:“H¢〜A,B™žX
BytesRead:100
Trail 2:
数据:x]Ãßÿ$¢œŒ2Y¶SIeó1ñ\ @ó8¬!)þ
BytesRead:30
我知道这个数据不正确,我应该得到ASCII数据。 是否有任何库支持我的问题的解决方案?
我需要在重新启动UART后读取数据,一旦停止接收数据,它应该将所有数据写入文本文件。
请帮助我,非常感谢。
答案 0 :(得分:-1)
在设置功能中设置错误的波特率输出错误。
lLastError = serial.Setup(CSerial::EBaud115200,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
以及
std::ofstream output_file;
//Creating a text file
output_file.open("save_test.txt", std::ios_base::app);
//writing data to text file
output_file /*<< "Data: "*/ << szBuffer;
而不是下面的代码(每次都会覆盖文本文件而不会看到数据)
//writing data to text file
std::ofstream o("save.txt");
o << "Data: " << szBuffer << std::endl << "BytesRead: " <<dwBytesRead <<std::endl;
会将数据附加到文本文件中,这样就可以在文本文件中看到RS232传输的完整数据。