-
March 5th, 2013, 07:46 AM
#46
Re: Sending Byte to serial port
Sorry, I wasn't specific. I meant not using printf("%s", dst).
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 5th, 2013, 09:36 AM
#47
Re: Sending Byte to serial port
Well! Thank you! This is a very good forum. I forgot to create
a loop for each element in the buffer!
I have finished my second step with success: encoding and decoding the packet! And thanks to "printf" I have verified that all works correctly!
Now I can pass to the 3rd step.
-
March 6th, 2013, 04:08 AM
#48
Re: Sending Byte to serial port
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.
-
March 6th, 2013, 04:15 AM
#49
Re: Sending Byte to serial port
Victor Nijegorodov
-
March 6th, 2013, 04:41 AM
#50
Re: Sending Byte to serial port
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 ''^" ?
-
March 6th, 2013, 04:49 AM
#51
Re: Sending Byte to serial port
Originally Posted by Bfrancesco
Because you are using the wrong symbol "ˆ" instead of the "^"
Originally Posted by Bfrancesco
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.
Victor Nijegorodov
-
March 6th, 2013, 05:00 AM
#52
Re: Sending Byte to serial port
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 6th, 2013, 05:05 AM
#53
Re: Sending Byte to serial port
-
March 14th, 2013, 05:14 AM
#54
Re: Sending Byte to serial port
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:
Code:
void CSerialPort::Write(const void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesWritten)
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:
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);
Now I'm thinking to add the following code:
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);
But i don't know if it's correct. Can you help me?
-
March 14th, 2013, 05:20 AM
#55
Re: Sending Byte to serial port
Originally Posted by Bfrancesco
Now I'm thinking to add the following code:
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);
But i don't know if it's correct. Can you help me?
But why don't you want to try? It would be much faster than write this post to the forum and then wait to the answers!
Victor Nijegorodov
-
March 14th, 2013, 05:32 AM
#56
Re: Sending Byte to serial port
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...
-
March 14th, 2013, 05:48 AM
#57
Re: Sending Byte to serial port
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;
}
-
March 14th, 2013, 05:49 AM
#58
Re: Sending Byte to serial port
But how did you do it sending non-encoded byte sequence to the serial port?
Victor Nijegorodov
-
March 20th, 2013, 04:55 AM
#59
Re: Sending Byte to 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.
-
March 20th, 2013, 07:17 AM
#60
Re: Sending Byte to serial port
Did you debug your code to be sure this write() function is called and your byte array is sent to the port?
Originally Posted by Bfrancesco
... 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.
What "BYTE*" do you mean?
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|