Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to receive data from serial port
    text
    copied!<p>Currently I try to write a serial port communication in VC++ to transfer data from PC and robot via XBee transmitter. But after I wrote some commands to poll data from robot, I didn't receive anything from the robot (the output of filesize is 0 in the code.). Because my MATLAB interface works, so the problem should happen in the code not the hardware or communication. Would you please give me help? </p> <p>01/03/2014 Updated: I have updated my codes. It still can not receive any data from my robot (the output of read is 0). When I use "cout&lt;&lt;&amp;read" in the while loop, I obtain "0041F01C1". I also don't know how to define the size of buffer, because I don't know the size of data I will receive. In the codes, I just give it a random size like 103. Please help me.</p> <pre><code>// This is the main DLL file. #include "StdAfx.h" #include &lt;iostream&gt; #define WIN32_LEAN_AND_MEAN //for GetCommState command #include "Windows.h" #include &lt;WinBase.h&gt; using namespace std; int main(){ char init[]=""; HANDLE serialHandle; // Open serial port serialHandle = CreateFile("\\\\.\\COM8", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); // Do some basic settings DCB serialParams; DWORD read, written; serialParams.DCBlength = sizeof(serialParams); if((GetCommState(serialHandle, &amp;serialParams)==0)) { printf("Get configuration port has a problem."); return FALSE; } GetCommState(serialHandle, &amp;serialParams); serialParams.BaudRate = CBR_57600; serialParams.ByteSize = 8; serialParams.StopBits = ONESTOPBIT; serialParams.Parity = NOPARITY; //set flow control="hardware" serialParams.fOutX=false; serialParams.fInX=false; serialParams.fOutxCtsFlow=true; serialParams.fOutxDsrFlow=true; serialParams.fDsrSensitivity=true; serialParams.fRtsControl=RTS_CONTROL_HANDSHAKE; serialParams.fDtrControl=DTR_CONTROL_HANDSHAKE; if (!SetCommState(serialHandle, &amp;serialParams)) { printf("Set configuration port has a problem."); return FALSE; } GetCommState(serialHandle, &amp;serialParams); // Set timeouts COMMTIMEOUTS timeout = { 0 }; timeout.ReadIntervalTimeout = 30; timeout.ReadTotalTimeoutConstant = 30; timeout.ReadTotalTimeoutMultiplier = 30; timeout.WriteTotalTimeoutConstant = 30; timeout.WriteTotalTimeoutMultiplier = 30; SetCommTimeouts(serialHandle, &amp;timeout); if (!SetCommTimeouts(serialHandle, &amp;timeout)) { printf("Set configuration port has a problem."); return FALSE; } //write packet to poll data from robot WriteFile(serialHandle,"&gt;*&gt;p4",strlen("&gt;*&gt;p4"),&amp;written,NULL); //check whether the data can be received char buffer[103]; do { ReadFile (serialHandle,buffer,sizeof(buffer),&amp;read,NULL); cout &lt;&lt; read; } while (read!=0); //buffer[read]="\0"; CloseHandle(serialHandle); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload