Re: Serial Communication
clinisbut wrote:
This code is the one I based to adapt my code (is bassically the same,
but with other variables names).
What do you refer that is a bit C-ish? Which is the way to declare
variables in C++?
C++ offers the advantage that you can declare variables when you need them
(opposed to C, where you have to declare all variables at the beginning of a
function). Had I written the piece of code, it would like the following:
unsigned char buffer[16];
COleVariant myVar;
myVar.Attach (m_Comm.GetInput());
// Get the length
long lLen = 0;
HRESULT hr = ::SafeArrayGetUBound (myVar.parray, 1, &lLen);
if (hr == S_OK)
{
lLen++; // upper bound is zero based index
// lock array so you can access it.
BYTE *pAccess;
hr = ::SafeArrayAccessData (myVar.parray,(void**)&pAccess);
if (hr == S_OK)
{
// Make a copy of the data
// Note: Need to check that lLen is < buffer length
// so you don't overrun the buffer.
for (int i = 0; i < lLen; i++)
buffer[i] = pAccess[i];
// unlock the data
::SafeArrayUnaccessData (myVar.parray);
}
}
Another thing that I find annoying is the increasing nesting that comes with
checking the HRESULTs of the COM API calls. I prefer to make it this way:
HRESULT hr = ::SomeCOMAPICall (...);
if (!SUCCEEDED (hr))
{
// Do error processing.
return ERROR_CODE; // or throw exception, it depends on the function
//declaration
}
Taking the advantage of the opportunity, a question about implementing
that CSerial...
Imagine that I throw my MSComm and implement CSerial in a new class
that I would include in my project. How can I comunicate this CSerial
class with the one of my application?
For example, everytime I receive a message insert some text in a CEdit
component (for example). I cannot see the way of do this from a
CSerial class.
It looks as if you had to provide some code in order to replace MSComm by
CSerialPort (unlike MSComm, CSerialPort provides no message that tells that new
data is available). You would have to write a quite advanced call-back function
for CSerialPort which reads asynchronously from the serial port and fills your
CEdit with the received data. I think that you'll probably want to stick with
MSComm, as you have obviously solved your problems.
Regards,
Stuart