Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should use <code>strlen</code> instead of <code>sizeof</code> here:</p> <pre><code>WriteFile(serialHandle,init,strlen(init),&amp;written,NULL) </code></pre> <p>You would be even better off creating a function like this:</p> <pre><code>function write_to_robot (const char * msg) { DWORD written; BOOL ok = WriteFile(serialHandle, msg, strlen(msg), &amp;written, NULL) &amp;&amp; (written == strlen(msg)); if (!ok) printf ("Could not send message '%s' to robot\n", msg); } </code></pre> <p>But that's only the appetizer. The main trouble is, as MDN says:</p> <p><em>You cannot use the <strong>GetFileSize</strong> function with a handle of a nonseeking device such as a pipe or a communications device.</em></p> <p>If you want to read from the port, you can simply use <code>ReadFile</code> until it returns zero bytes.</p> <p>If you already know the max size of your robot's response, try reading that many characters. Continue reading until the read reports an actual number of bytes read inferior to the size of the buffer. For instance:</p> <pre><code>#define MAX_ROBOT_ANSWER_LENGTH 1000 /* bytes */ const char * read_robot_response () { static char buffer[MAX_ROBOT_ANSWER_LENGTH]; DWORD read; if (!ReadFile (serialHandle, buffer, sizeof(buffer), &amp;read, NULL)) { printf ("something wrong with the com port handle"); exit (-1); } if (read == sizeof(buffer)) { // the robot response is bigger than it should printf ("this robot is overly talkative. Flushing input\n"); // read the rest of the input so that the next answer will not be // polluted by leftovers of the previous one. do { ReadFile (serialHandle, buffer, sizeof(buffer), &amp;read, NULL); } while (read != 0); // report error return "error: robot response exceeds maximal length"; } else { // add a terminator to string in case Mr Robot forgot to provide one buffer[read] = '\0'; printf ("Mr Robot said '%s'\n", buffer); return buffer; } } </code></pre> <p>This simplistic function returns a static variable, which will be overwritten each time you call read_robot_response. </p> <p>Of course the proper way of doing things would be to use blocking I/Os instead of waiting one second and praying for the robot to answer in time, but that would require a lot more effort.</p> <p>If you feel adventurous, you can use overlapped I/O, as <a href="http://msdn.microsoft.com/en-us/library/ff802693.aspx" rel="nofollow">this lenghty MDN article</a> thoroughly explores.</p> <p>EDIT: after looking at your code</p> <pre><code>// this reads at most 103 bytes of the answer, and does not display them if (!ReadFile(serialHandle,buffer,sizeof(buffer),&amp;read,NULL)) { printf("Reading data to port has a problem."); return FALSE; } // this could display the length of the remaining of the answer, // provided it is more than 103 bytes long do { ReadFile (serialHandle,buffer,sizeof(buffer),&amp;read,NULL); cout &lt;&lt; read; } while (read!=0); </code></pre> <p>You are displaying nothing but the length of the response beyond the first 103 characters received.</p> <p>This should do the trick:</p> <pre><code>#define BUFFER_LEN 1000 DWORD read; char buffer [BUFFER_LEN]; do { if (!ReadFile( serialHandle, // handle buffer, // where to put your characters sizeof(buffer) // max nr of chars to read -1, // leave space for terminator character &amp;read, // get the number of bytes actually read NULL)) // Yet another blody stupid Microsoft parameter { // die if something went wrong printf("Reading data to port has a problem."); return FALSE; } // add a terminator after last character read, // so as to have a null terminated C string to display buffer[read] = '\0'; // display what you actually read cout &lt;&lt; buffer; } while (read!=0); </code></pre> <p>I advised you to wrap the actual calls to serial port accesses inside simpler functions for a reason. As I said before, Microsoft interfaces are a disaster. They are verbose, cumbersome and only moderately consistent. Using them directly leads to awkward and obfuscated code.</p> <p>Here, for instance, you seem to have gotten confused between <code>read</code> and <code>buffer</code></p> <ul> <li>read holds the number of bytes actually read from the serial port</li> <li>buffer holds the actual data.</li> </ul> <p><code>buffer</code> is what you will want to display to see what the robot answered you</p> <p>Also, you should have a documentation for your robot stating which kind of answers you are supposed to expect. It would help to know how they are formatted, for instance whether they are null-terminated strings or not. That could dispense to add the string terminator.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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