Because you are using the wrong symbol "ˆ" instead of the "^"
Printable View
Because what looks like '^' in the code actually isn't ^ but some other symbol. Replace what looks like '^' with an actual ^ and it should then compile.
Yes, Thank you!
Hi
Now I have obtained the correct packet byte to send after coding and CRC calculation. This is: "01 02 04 01 02 09 0c 10 02 06 12 01 01 13 01 01 49 f1 00"
Now I want to send it using the project in http://www.naughter.com/serialport.html. But i need to understand well the code of this project to send correctly. It is composed mainly by 2 files: app.ccp and SerialPort.cpp. In the SerialPort.cpp file there is the function:
and I think that it's the function that i must use to send the packet. In the app.ccp this function is called:Code:void CSerialPort::Write(const void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesWritten)
Now I'm thinking to add the following code:Code:port2.Open(6, 9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl, TRUE);
CEvent event(FALSE, TRUE);
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
overlapped.hEvent = event;
try
{
port2.Write(pBuf, 10000, overlapped);
But i don't know if it's correct. Can you help me?Code:char sBuf[] = {0x01,0x02,0x04,0x01,0x02,0x09,0x0c,0x10,0x02,0x06,0x12,0x01,0x01,0x13,0x01,0x01,0x49,0xf1,0x00} ;
port2.Write(sBuf,sizeof(sBuf)/sizeof(unsigned char),overlapped );
printf("pBuf %x, \n", sBuf);
I'm trying but I don't know if it's correct, because i don't read any error when I compile, but none window appears to read the byte that I send...
This is the complete code:
Code:///////////////////////////////// Includes //////////////////////////////////
#include "stdafx.h"
#include "SerialPort.h"
///////////////////////////////// Defines /////////////////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//////////////////////////////// Implementation ///////////////////////////////
class CSerialPortApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
CSerialPortApp theApp;
BOOL CSerialPortApp::InitInstance()
{
BYTE* pBuf = new BYTE[10000];
try
{
COMMCONFIG config;
CSerialPort::GetDefaultConfig(1, config);
CSerialPort port;
port.Open(1, 9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl);
HANDLE hPort = port.Detach();
port.Attach(hPort);
DWORD dwModemStatus;
port.GetModemStatus(dwModemStatus);
DCB dcb;
port.GetState(dcb);
dcb.BaudRate = 9600;
port.SetState(dcb);
DWORD dwErrors;
port.ClearError(dwErrors);
port.SetBreak();
port.ClearBreak();
COMSTAT stat;
port.GetStatus(stat);
DWORD dwBytesWaiting = port.BytesWaiting();
dwBytesWaiting;
COMMTIMEOUTS timeouts;
port.GetTimeouts(timeouts);
port.Setup(10000, 10000);
port.GetConfig(config);
config.dcb.BaudRate = 9600;
port.SetConfig(config);
port.Set0WriteTimeout();
port.Set0ReadTimeout();
//char sBuf[] = "This should appear on the serial port";
//port.Write(sBuf, static_cast<DWORD>(strlen(sBuf)));
DWORD dwMask;
port.GetMask(dwMask);
port.SetMask(EV_TXEMPTY);
//port.WaitEvent(dwMask);
port.TerminateOutstandingWrites();
port.TransmitChar('p');
port.Set0Timeout();
char sRxBuf[10];
DWORD dwRead = port.Read(sRxBuf, 10);
dwRead; //To remove unreferrenced variable in VC 6.
port.TerminateOutstandingReads();
port.ClearDTR();
port.ClearRTS();
port.SetDTR();
port.SetRTS();
port.SetXOFF();
port.SetXON();
COMMPROP properties;
port.GetProperties(properties);
port.ClearWriteBuffer();
port.ClearReadBuffer();
port.Flush();
port.Close();
//Try out the overlapped functions
CSerialPort port2;
port2.Open(6, 9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl, TRUE);
CEvent event(FALSE, TRUE);
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
overlapped.hEvent = event;
try
{
// port2.Write(pBuf, 10000, overlapped);
char sBuf[19] = {0x01,0x02,0x04,0x01,0x02,0x09,0x0c,0x10,0x02,0x06,0x12,0x01,0x01,0x13,0x01,0x01,0x49,0xf1,0x00} ;
port2.Write(sBuf,sizeof(sBuf)/sizeof(unsigned char),overlapped );
printf("pBuf %x, \n", sBuf);
SleepEx(INFINITE, TRUE);
// printf("pBuf %x, \n", pBuf);
}
catch(CSerialException* pEx)
{
if (pEx->m_dwError == ERROR_IO_PENDING)
{
DWORD dwBytesTransferred = 0;
port2.GetOverlappedResult(overlapped, dwBytesTransferred, TRUE);
pEx->Delete();
}
else
{
DWORD dwError = pEx->m_dwError;
pEx->Delete();
CSerialPort::ThrowSerialException(dwError);
}
}
try
{
//port2.Read(pBuf, 10, overlapped);
port2.Read(pBuf, 20, overlapped);
SleepEx(INFINITE, TRUE);
printf("pBuf %x, \n", pBuf);
}
catch(CSerialException* pEx)
{
if (pEx->m_dwError == ERROR_IO_PENDING)
{
DWORD dwBytesTransferred = 0;
port2.GetOverlappedResult(overlapped, dwBytesTransferred, TRUE);
pEx->Delete();
}
else
{
DWORD dwError = pEx->m_dwError;
pEx->Delete();
CSerialPort::ThrowSerialException(dwError);
}
}
port2.SetMask(EV_TXEMPTY);
/*char sBuf[19] = {0x01,0x02,0x04,0x01,0x02,0x09,0x0c,0x10,0x02,0x06,0x12,0x01,0x01,0x13,0x01,0x01,0x49,0xf1,0x00} ;
port2.Write(sBuf,sizeof(sBuf)/sizeof(unsigned char),overlapped );
printf("pBuf %x, \n", sBuf);
SleepEx(INFINITE, TRUE);
port2.Read(pBuf, 20);
SleepEx(INFINITE, TRUE);
printf("pBuf %x, \n", pBuf);
*/
/* for testing on NT only
port2.WriteEx(sBuf, static_cast<DWORD>(strlen(sBuf)));
SleepEx(INFINITE, TRUE);
printf("pBuf %x, \n", sBuf);
port2.ReadEx(pBuf, 10);
SleepEx(INFINITE, TRUE);
*/
}
catch (CSerialException* pEx)
{
TRACE(_T("Handle Exception, Message:%s\n"), pEx->GetErrorMessage().operator LPCTSTR());
pEx->Delete();
}
delete [] pBuf;
return FALSE;
}
But how did you do it sending non-encoded byte sequence to the serial port?
I'm trying to send encoded bytes (The codification with other code is made). I have understood that to stamp in the MFC application I must use "TRACE" and not "printf", but the real problem is that I'm not understing how i can handle the "BYTE*" and how i can use it in "port.write" to send the bytes.
1) Why are you first opening port 1, setting various configurations then closing it and opening port 6?
2) Why are you attempting to use overlapped I/O to just send 19 bytes? Why not just write those 19 bytes to the port directly without all the complication of overlapped I/O?
Why not just
Get this working first, then if you really need to use overlapped I/O you can start at least with something that works. When trying to do something and you're not quite sure how to do it, start with something as simple as possible, get that working and then expand in small steps to what you are trying to achieve. If you start with something small and working and change it and that change doesn't work then the problem must be only in the change so there is a much narrower area for debuging. You can also undo the changes just made and revert to what was working then try changes again.Code:CSerialPort port2;
port2.Open(6, 9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl);
const unsigned char sBuf[] = {0x01,0x02,0x04,0x01,0x02,0x09,0x0c,0x10,0x02,0x06,0x12,0x01,0x01,0x13,0x01,0x01,0x49,0xf1,0x00};
port2.Write(sBuf, sizeof(sBuf) / sizeof(unsigned char));
port2.Read(pBuf, 19);
Thank you very much 2kaud! now it works correctly!
One question please: I implemented a low-pass filter FIR of 12th order and 5Hz cut-off frequency. I'd like to implement the same filter in c++. Can you suggest something?
Thank you
If you know the algorithm then what is the problem to code it with C++?
I don't know the algorithm. With Matlab I use only this command: B = fir1(N,Wn) where N is the order and the Wn the cut-off frequency.
Then start with learning the algorithm: Wiki
I'd need the algorithm of matlab FIR.
Then ask matlab!
Can't you use Google?
Just try https://www.google.com/search?source...+FIR&gs_htsa=1
and the first link will point to fir1 - Window-based finite impulse response filter design
In that link there is the equation B(z). Is it sufficient to implement the filter FIR1(N,Wn) in c++?
I have know idea what filter FIR1 is.
It is up to you to find and understand its algorithm and then try to implement it using C++ code.
And no one will do this work for you. Where to search for I already showed you.
If you then will have some problems with C++ - ask in this Forum.
hi,sir what is the code that send byte of binary data from matlab via serial port
thanks in advance