Send and receive through serial Port

This is a discussion on Send and receive through serial Port within the C++ Programming forums, part of the General Programming Boards category; Hi, I`m trying to read Hexnumbers of a text file, bytewise, and send them through a serial port, one after ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    21

    Send and receive through serial Port

    Hi,
    I`m trying to read Hexnumbers of a text file, bytewise, and send them through a serial port, one after the other. with the same routine I want to receive a message.

    The following routine works, reading out the textfile and sending it, but I cant get it to receive.
    I also dont understand how it works, it goes through the serialport setup sequence every time and also closes the port after each sent letter.
    Despite all that, the terminal that I`m sending the numbers to , reads them correctly.
    That`s not the case when I only place the WriteFile() func. in the loop.
    Can anyone maybe straighten out my code?
    Working code, but doesn`t receive:
    Code:
    int main()
    {
    DCB dcb;
    HANDLE hCom;
    DWORD dwError;
    DWORD dwWritten;
    BOOL fSuccess;
    int rows;  
    int i=0,j=0;
    int x[28];
    char buff[8]; //  BUFSIZE
    unsigned char Msg[128], Data;
    
    FILE *HexCode;
    HexCode=fopen("hex.txt","r");  //reads hex.txt
    
    clrscr();
    //---------------reading first collumb of text file-------------------------------------
    
      while(i<zeilanz && fgets(buff,sizeof(buff),HexCode)!=NULL )
       {
        if (sscanf(&buff[0],"%x",&x[i])==1){
          // printf("sscanf=%d  i=%d x=%x\n", sscanf(buff, "%x", &x[i]), i, x[i]);
         i++;
        }
       }
    //------------------  x[] is available  ---------------------------------
       for(j=0;j<4;j++) //  and will be sent through WritwFile()
       {
       // cout<<"j="<<j<<" x="<<x[j]<<"\n";
    //------------------------------------------------------------------------------
    hCom=CreateFile("COM1",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,NULL);
    //Buffert erst ab hier
    //------------------------------------------------------------------------------
    if ( hCom == INVALID_HANDLE_VALUE ) // If COMM port can't be opened, then ...
     {
        dwError = GetLastError();  // Display error code, and abort.
        FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, dwError, 0, (char *)Msg, 124, 0 );
        printf("Cannot Open Port COM1 !\nError Code: %d, - %s.\n", dwError, Msg );
    
        return 1;
     }
    //------------------------------------------------------------------------------
    
    fSuccess = GetCommState(hCom, &dcb);
    
    dcb.BaudRate=CBR_2400;
    dcb.ByteSize=8;
    dcb.Parity=NOPARITY;
    dcb.StopBits=ONESTOPBIT;
    dcb.fDtrControl=DTR_CONTROL_DISABLE;
    dcb.fOutxCtsFlow=FALSE;
    dcb.fOutxDsrFlow=FALSE;
    dcb.fRtsControl=RTS_CONTROL_DISABLE;
    
    fSuccess=SetCommState(hCom, &dcb);
    //---------------------- Sendfunc -----------------------------------------
    
    WriteFile(hCom, &x[j], 4, &dwWritten, NULL);
    
    //------------------------------------------------------------------------------
    
    CloseHandle(hCom);
    }  // close loop
    getch();  //wait for display
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    check out this example at MSDN.

    http://msdn.microsoft.com/library/de...sdn_serial.asp

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending streams over serial port in win32
    By deian in forum C Programming
    Replies: 2
    Last Post: 07-13-2009, 12:01 PM
  2. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  3. Duplex communication thro serial port
    By Priyachu in forum Networking/Device Communication
    Replies: 1
    Last Post: 05-30-2009, 04:24 AM
  4. PC104 Serial Port Programming Problem
    By outerspace in forum C Programming
    Replies: 6
    Last Post: 11-09-2005, 06:07 PM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 03:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21