Sorry, I wasn't specific. I meant not using printf("%s", dst).
Printable View
Sorry, I wasn't specific. I meant not using printf("%s", dst).
Well! Thank you! This is a very good forum. I forgot to createI have finished my second step with success: encoding and decoding the packet! And thanks to "printf" I have verified that all works correctly!Quote:
a loop for each element in the buffer!
Now I can pass to the 3rd step.
One fast question...
I'm implementing a 16bit CCITT CRC algorithm and I have found this sample:
Code:unsigned int crc(unsigned char data)
{
static unsigned int crc;
crc = (unsigned char)(crc >> 8) | (crc << 8);
crc = data;
crc ˆ= (unsigned char)(crc & 0xff) >> 4;
crc ˆ= (crc << 8) << 4;
crc ˆ= ((crc & 0xff) << 4) << 1;
return crc;
}
I'd like to understand how work the "^=" operator.
OK thank you. I didn't find http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx.
Why the compiler gives the follow error:
- syntax error: ';' before identifier ''^" ?
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.