🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #16536

Problem with CreateFile() function API

#16536
From: Mario
Date: Mon, 05 May 2003 21:26
165 lines
4980 bytes
When I send one series of bytes on the serial port, I receive the same
buffer wich I transmit, while I would have myself to be waited for the
answer of OK from the modem. How can I resolve this problem ?
I have used the following example:

//--------------------------------------------------------------------------
-
#include <vcl.h>
#include <windows.h>
#include <stdio.h>
#include <tapi.h>
#include <assert.h>
#pragma hdrstop
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
Tcalling *calling;
//--------------------------------------------------------------------------
-
__fastcall Tcalling::Tcalling(TComponent* Owner)
  : TForm(Owner)
{
}
//--------------------------------------------------------------------------
-

void __fastcall Tcalling::Button1Click(TObject *Sender)
{
  state->Clear();
  state->Lines->Add("###START");

  if ( setComm("COM1", 38400, 8) )
  {
    if ( writeComm("AT\r\n", 4) )
    {
      readComm();
    }

    CloseHandle(hCom);
  }
  state->Lines->Add("###END");
}
//--------------------------------------------------------------------------
-

bool __fastcall Tcalling::setComm(char *pcCommPort, int baudRate, int
byteSize)
{
  DCB dcb;
  bool fSuccess;

  // Create handle file with the serial port
  hCom =CreateFile(pcCommPort,
                  GENERIC_READ |GENERIC_WRITE,
                  0,
                  NULL,
                  OPEN_EXISTING,
                  0,
                  NULL);

  if (hCom ==INVALID_HANDLE_VALUE)
  {
    state->Lines->Add("Port not ready !");
    fSuccess =false;
  }
  else
  {
    // Read state of the serial port
    fSuccess =GetCommState(hCom, &dcb);
    if (!fSuccess)
    {
      state->Lines->Add("Failed open port !");
    }
    else
    {
      dcb.BaudRate  =baudRate;
      dcb.ByteSize  =byteSize;
      dcb.Parity    =NOPARITY;
      dcb.StopBits  =ONESTOPBIT;

      // Initiate the serial port
      fSuccess =SetCommState(hCom, &dcb);

      if (!fSuccess)
      {
        state->Lines->Add("Failed initiate port !");
      }
      else
      {
        bool fSuccess =SetCommMask(hCom, EV_TXEMPTY);
        if (!fSuccess)
          fSuccess =false;
        else
        {
          o.hEvent =CreateEvent(NULL, FALSE, FALSE, NULL);
          assert(o.hEvent);

          COMMTIMEOUTS ctmoCommPort;
          ctmoCommPort.ReadIntervalTimeout = MAXDWORD;
          ctmoCommPort.ReadTotalTimeoutMultiplier = MAXDWORD;
          ctmoCommPort.ReadTotalTimeoutConstant = MAXDWORD;
          ctmoCommPort.WriteTotalTimeoutMultiplier = 0;
          ctmoCommPort.WriteTotalTimeoutConstant = 0;
          SetCommTimeouts(hCom, &ctmoCommPort);
        }
      }
    }
  }

  return fSuccess;
}
//--------------------------------------------------------------------------
-

int __fastcall Tcalling::readComm()
{
  char *inBuffer;
  int nBytesRead =0;
  DWORD nBytesToRead =4;

  if ( !ReadFile(hCom, &inBuffer, nBytesToRead, (DWORD *)&nBytesRead,
NULL) )
    nBytesRead =-1;

  if (nBytesRead >0)
    state->Lines->Add("read: " +(String)inBuffer);

  return nBytesRead;
}

//--------------------------------------------------------------------------
-
int __fastcall Tcalling::writeComm(char *outBuffer, int nBytesToWrite)
{
  int nBytesWritten;

  if (nBytesToWrite>0)
    state->Lines->Add("write:" +(String)outBuffer);

  if ( WriteFile(hCom, &outBuffer, nBytesToWrite, (DWORD *)&nBytesWritten,
NULL) )
  {
    if ( FlushFileBuffers(hCom) )
    {
      DWORD dwEvtMask;
      if (WaitCommEvent(hCom, &dwEvtMask, NULL))
      {
        if (dwEvtMask & EV_TXEMPTY)
          state->Lines->Add("write complete");
        else
          nBytesWritten =-1;
      }
    }
  }
  else
    nBytesWritten =-1;

  return nBytesWritten;
}
//--------------------------------------------------------------------------
-


Thanks Mario.
Technical Details
Message-ID: <3eb6d718@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!iad-peer.news.verio.net!news.verio.net!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,364db1658d3a55fb,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-05-05 14:36:03 PST
From: "Mario" <m.sonnessa.spes@urmetsistemi.it>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Problem with CreateFile() function API
Date: Mon, 5 May 2003 23:26:49 +0200
Lines: 167
Organization: URMET SPES
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 62.11.4.195
Message-ID: <3eb6d718@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1052170008 62.11.4.195 (5 May 2003 14:26:48 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!iad-peer.news.verio.net!news.verio.net!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail