Communicating over Modbus with C++ (MFC)

M

Thread Starter

ModbusCPP

Hello,

I've communicated with my device (M3) over Modbus Poll. Standard setting and Modbus ASCII. This worked fine and I tried to write a C++ program which would do the same.

I'm handling the device like a file:<pre>









m_hCom = CreateFile(m_sComPort,
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security
OPEN_EXISTING,
0, // no overlapped I/O
NULL); // null template

// This request is exactly the same like the one ModbusPoll sends
char strASCII[] = "3A 30 31 30 33 30 30 30 30 30 30 30 34 46 38 0D 0A";

iBytesWritten = 0;
// Talk to the M3
//WriteFile (m_hCom, str, strlen (str), &iBytesWritten, NULL);

bWriteRC = WriteFile (m_hCom, strASCII, strlen(strASCII), &iBytesWritten, NULL);

memset(sBuffer,0,sizeof(sBuffer));
// Read output of the M3
bReadRC = ReadFile (m_hCom, &sBuffer, sizeof(&sBuffer), &iBytesRead, NULL);

if (bReadRC && iBytesRead > 0)
{
sResult = sBuffer;
}
else
{
sResult = "Read Failed";
dwError = GetLastError();

sprintf(sMsg, "Read length failed: RC=%d Bytes read=%d, "
"Error=%d ",
bReadRC, iBytesRead, dwError);
AfxMessageBox(sMsg);
} // end if</pre>
And you guess it, i get the Read length failed error.
The whole program, my sources are out can be found here http://www.ontrak.net/mfc.htm (Link on the bottom). Simple serial communication.

Can anyone here help me?

Regards
Andreas
 
Top