title: Serial port communication initialization
body: <p>At the time we are trying to create an interface for serial communication, to be able to communicate with a microprocessor.</p>
  
  <p>Actually - everything works fine. Almost! 
  To be able to communicate with our controller, we need to sync up with it. To do this, we write a string: <code>"?0{SY}13!"</code>, and the controller should then reply with <code>"!0{SY}F5?"</code> to accept the request for sync. 
  To do this, we use a <code>writeData</code> function (that works - we know that by using <code>echo</code>), and after that we use a <code>readData</code> to read the answer. 
  The problem is that, for some reason, it will not read anything. Though it returns <code>1</code> for success, the chars it reads is constanly <code>" "</code> (nothing). </p>
  
  <p>Now comes the weird part - if we use an external terminal program to initialize the port (like putty), and then close the program, then everything works fine. It accepts the sync request, answers (and we can read it), and then we can do all that we want. But unless we use an external program to initialize the port, it doesn't work. </p>
  
  <p>The constructor for the initializing the interface looks like this:</p>
  
  <pre><code>SerialIF::SerialIF(int baud, int byteSize, int stopBits, char* parity, int debug)
  {
      string coutport = getPort();    
      wstring wideport;               
      debug_ = debug;                 //Debuglevel
  
      sync = false;                   //sync starts with false
  
      error = false;                  //Error false as beginnging
  
      //this is just for converting to the right type
      for (int i = 0; i &lt; coutport.length(); i++)
      {
          wideport += wchar_t(coutport[i]);  
      }
      const wchar_t* port = wideport.c_str();
  
      SerialIF::hserial = CreateFile(port,
          GENERIC_READ | GENERIC_WRITE,       
          0,
          0,
          OPEN_EXISTING,                      
          FILE_ATTRIBUTE_NORMAL,              
          0);
      if (hserial == INVALID_HANDLE_VALUE)
      {
          if (GetLastError() == ERROR_FILE_NOT_FOUND)
          {
              if (debug_ != LOW)
              {
                  cout &lt;&lt; "[-] Port " &lt;&lt; coutport &lt;&lt; "doesn't exist." &lt;&lt; endl;  
              }
          }
  
          if (debug_ != LOW)
          {
              cout &lt;&lt; "[-] Handle error - is there another terminal active?" &lt;&lt; endl;
          }
          error = true;
      }
  
      DCB dcbParms = { 0 };
      dcbParms.DCBlength = sizeof(dcbParms);
  
      if (!GetCommState(hserial, &amp;dcbParms))
      {
          if (debug_ != LOW)
          {
              cout &lt;&lt; "[-] Couldn't get status from port " &lt;&lt; coutport &lt;&lt; endl;
          }
          error = true;  
      }
  
      if (!error)
      {
          setBaud(dcbParms, baud);
          setParity(dcbParms, parity);
          setByteSize(dcbParms, byteSize);
          setStopbits(dcbParms, stopBits);
  
          if (debug_ == HIGH)
          {
              cout &lt;&lt; "[+] Serial port " &lt;&lt; coutport &lt;&lt; " has been activated. \nBaud-rate: " &lt;&lt; baud &lt;&lt; "\nParity: "
                  &lt;&lt; parity &lt;&lt; "\nStop bits: " &lt;&lt; stopBits &lt;&lt; endl;
          }
      }
      else if (debug_ != LOW)
      {
          cout &lt;&lt; "[-] Port not initialized" &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>This should work - I really don't know why it shouldn't. It returns no errors, I've tried A LOT of error searching the last couple of days, I tried timeouts, I tried other ways of building it, but it all boils down to the same problem.</p>
  
  <p>Why wont this initialize the port?</p>
  
  <p>EDIT:</p>
  
  <p>The output when trying to sync: 
  Can't post pictures due to lack of reputation. though it outputs as follows:</p>
  
  <p>[+] Serial port COM1 has been activated.
  Baud-rate: 9600
  Parity: NONE
  Stop bits: 1</p>
  
  <p>[+] -> ?0{SY}13! is written to the port. 
  ((And this is where it goes in to the infinite loop reading " "))</p>
  
  <p>EDIT: code for read:</p>
  
  <pre><code>const int bytesToRead = 1;              //I byte pr læsning
  char buffer[bytesToRead + 1] = { 0 };   //Bufferen til data
  DWORD dwBytesRead = 0;                  //Antal bytes læst
  string store;                           //Store - den vi gemmer den samlede streng i
  bool end = false;                       //Kontrolvariabel til whileloop.
  
  while (end == false)                    
  {
      if (ReadFile(hserial, buffer, bytesToRead, &amp;dwBytesRead, NULL)) 
      /*Readfile læser fra interfacet vha. hserial som vi oprettede i constructoren*/
      {
          if (buffer[0] == '?')           //Da protokollen slutter en modtaget streng med "?", sætter vi end til true 
          {                               //Hvis denne læses.     
              end = true;     
          }
          store += buffer[0];
      }
      else
      {
          if (debug_ != LOW)
          {
              cout &lt;&lt; "[-] Read fail" &lt;&lt; endl;    //Hvis readfile returnerer false, så er der sket en fejl. 
          }
          end = true;
      }
  }
  
  if (debug_ == HIGH)
  {
      cout &lt;&lt; "[+] Recieved: " &lt;&lt; store &lt;&lt; endl;  //I forbindelse med debug, er det muligt at få udsrkevet det man fik ind.
  }
  
  recentIn = store;                               //RecentIN brugES i andre funktioner                
  if (verify())                                   //Som f.eks. her, hvor vi verificerer dataen
  {
      if (debug_ == HIGH)
      {
          cout &lt;&lt; "[+] Verification success!" &lt;&lt; endl;
      }
      return convertRecData(store);
  }
  else
  {
      if (debug_ != LOW)
      {
          cout &lt;&lt; "[-] Verification failed." &lt;&lt; endl;
      }
  
      vector &lt;string&gt; null;   //Returnerer en string uden data i, hvis der er sket en fejl.
      return null;
  }
  </code></pre>