Sunday, 18 August 2013

What variable type to use for parameter LPVOID lpBuffer in C++ functions WriteFile and ReadFile

What variable type to use for parameter LPVOID lpBuffer in C++ functions
WriteFile and ReadFile

pWhat variable type should be used for stronglpBuffer/strong of C++
codeReadFile/code and codeWriteFile/code functions to communicate between
a Windows XP based PC and a micro-controller based system? The PC has
WinForm application in VS2010 C++/CLI. The micro-controller firmware is
ANSI C./p pMy PC is supposed to transmit command characters (say 'S', 'C'
etc) followed by command termination character code0xd/code (hex for
decimal 13). The micro-controller based system would respond with 5 to 10
bytes that would be mix of ASCII characters and hex numbers e.g. 'V'
followed by code0x41/code code0x72/code etc./p pstrongPC transmits and
micro-controller receives:/strong/p ol lipcodeTxMessage/code, codePP1/code
and codepTx/code declared as codechar/code and keeping
codenNumberOfBytesToWrite/code as 2, makes the micro-controller receive
code0x53/code for 'S' followed by code0xC3/code instead of
code0xd/code./p/li lipcodeTxMessage/code, codePP1/code and codepTx/code
declared as codewchar_t/code and keeping codenNumberOfBytesToWrite/code as
2, makes the micro-controller receive code0x53/code for 'S' only./p/li
lipcodeTxMessage/code, codePP1/code and codepTx/code declared as
codewchar_t/code and keeping codenNumberOfBytesToWrite/code as 4, makes
the micro-controller receive code0x53/code for 'S' followed by
code0xd/code correctly./p/li /ol pThe third scheme of transmit and receive
above meets my expected behavior of a solution. But the confusion is here:
Although the PC might be transmitting 4 bytes (for two codewchar/code
types), the micro-controller receives 2 bytes code0x53/code for 'S',
correctly followed by code0xD/code./p pstrongMicro-controller transmits
and PC receives:/strong/p pAssuming that codewchar_t/code is the right
choice for codelpBuffer/code, strongwhat should be my
codenNumberOfBytesToRead/code/strong for receiving 10 bytes from the
micro-controller? ReadFile would expect 20 bytes by virtue of
codewchar_t/code, whereas the micro-controller would transmit 10 bytes
only./p pAmazingly, irrespective of declaring (codeRxMessage/code,
codePP2/code and codepRx/code) as codewchar_t/code, codechar/code or
codeunsigned char/code, ReadFile receives 10 bytes from the
micro-controller (meets my expected behavior of a solution). But the issue
is that transmitting 'A' 10 times from the micro-controller, ReadFile on
the PC's end receives junk like 'S', code0x0/code, code0xd/code,
code0x54/code, code0x29/code./p precode/// Required designer variable.
HANDLE hCommPort; BOOL fSuccess; arraylt;wchar_tgt; ^ TxMessage;
arraylt;unsigned chargt; ^ RxMessage; TxMessage = gcnew arraylt;wchar_tgt;
(12); RxMessage = gcnew arraylt;unsigned chargt; (12); {
TxMessage[0]='S';//target cmd TxMessage[1]=0xd;//cmd termination character
DWORD dwhandled; if (hCommPort != INVALID_HANDLE_VALUE) {
pin_ptrlt;wchar_tgt; pp1 = amp;TxMessage[0]; wchar_t *pTx = pp1; fSuccess
= WriteFile(hCommPort, pTx, 4, amp;dwhandled, NULL); PurgeComm(hCommPort,
PURGE_RXABORT|PURGE_TXABORT|PURGE_RXCLEAR|PURGE_TXCLEAR);
pin_ptrlt;unsigned chargt; pp2 = amp;RxMessage[0]; unsigned char *pRx =
pp2; fSuccess = ReadFile(hCommPort, pRx, 10, amp;dwhandled, NULL); }//if
IsOpen else{ this-gt;toolStripStatusLabel4-gt;Text=Port Not Opened;} }
/code/pre

No comments:

Post a Comment