Menu

I am LOST with Serial Programming PLEASE

2007-08-28
2012-09-26
  • ivan perino

    ivan perino - 2007-08-28

    PLEASE.
    I have read the entire forum looking for an example for writing and reading on serial port.
    I refuse to use winAPI32 because I am not a real programmer, I just have made an electronic aplication and I need it to comunicate to PC!

    I wrote this code just for testing:

    include <cstdlib>

    include <iostream>

    include <stdio.h> //I DONT KNOW IF ALL of THIS IS NECESARY

    include <stdlib.h>

    using namespace std;

    int main(int argc, char argv[])
    {
    char C='m';
    FILE
    portfp ;

    system(&quot;mode com1: baud=9600 parity=n data=8 stop=1 to=off xon=off&quot;);
    portfp = fopen( &quot;com1&quot;, &quot;rb&quot; ) ; 
    fprintf(portfp, &quot;Hola!\n\r&quot;) ; // &lt;----- It doesn´t work because &quot;rb&quot; mode
    C = fgetc(portfp);            // &lt;----- It works only with &quot;rb&quot; mode and wait for
                                 //         a character on serial port
    printf(&quot;%c&quot;,C);   // &lt;------ It works OK;
    fclose( portfp ) ; 
    return EXIT_SUCCESS;
    

    }

    Well, as you can see, I just can read and not to write, but.
    If I set fopen(...,"wb");
    I can write but not to read.. Thus, I have tryed r+,w+,rb+,wb+,etc... but nothing.

    I would need just the follow.

    1º Send a 'P' on serial until receive an 'I' and break out.
    2º Send another letter and receive amount of data from my electronic aplication.

    PLEASE, I dont know where to start.
    Might anybody hand me a piece of code? THANKS, THANKS A LOT!!!!

    Ivan Perino

     
    • Nobody/Anonymous

      Well with a post like that and an attitude like that you may be hard pressed to get a response....SOOOOOOO what I suggest is you open your browser and type in the search box "C++ serial port programming tutorial"

       
    • Osito

      Osito - 2007-08-28

      You don't have to be a real programmer to use the API. I'm just a hardware moron and I can handle it. All you really need is this:

      HANDLE hComm;
      hComm = CreateFile(com_port_text, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); // open port

      You can then write with:
      DWORD iBytes;
      WriteFile(hComm,text,length,&iBytes,NULL);
      FlushFileBuffers(hComm); // force immediate write

      Reading is a little trickier, but you can read one char at a time with something like this:
      DWORD dwErrors;
      COMSTAT comStat;
      DWORD iBytes;
      char sBuffer[2];
      ClearCommError(hComm, &dwErrors, &comStat); // fills in status including number of bytes waiting to be read
      bytes_waiting = comStat.cbInQue;
      ReadFile(hComm, &sBuffer, 1, &iBytes, NULL); // read one byte

       
    • Anonymous

      Anonymous - 2007-08-29

      rb+ and wb+ should work. How are you testing this, is your cable sound? Use a port monitor application to see is the serial driver is getting the data (try http://www.microsoft.com/technet/sysinternals/utilities/portmon.mspx for example). The advantage of this is that you can determine the port activity without having to worry about correct cabling or matching baud rates.

      Note that traditionally line ends are "\r\n" not "\n\r" (i.e. CRLF not LFCR)- this may make a difference to some applications.

      I have some example serial I/O code using this technique somewhere, I'll dig it out - standby.

      Clifford

       
    • Anonymous

      Anonymous - 2007-08-29

      ... Sorry, seems I dumped it.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.