🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
13 messages
13 total messages Started by JD Sat, 17 Jun 2006 18:40
Comm Port kicking my butt
#3475
Author: JD
Date: Sat, 17 Jun 2006 18:40
89 lines
3894 bytes
I have absolutely no clue why I can't get this simple test to
work. All it does is open a port, and write to it and read from
it.

The write seems to work because no errors are reported and the
number of bytes written are correct. The read, OTOH reports
nada - no errors and no data.

//--------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
    hCom = ::CreateFile( TEXT("COM1"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
    if( hCom == INVALID_HANDLE_VALUE ) throw ComException( "Open Failed", ::GetLastError() );

    FDCB.DCBlength = sizeof( DCB );
    if( ! ::GetCommState(hCom, &FDCB) )
      {
          ::CloseHandle( hCom );
          hCom = INVALID_HANDLE_VALUE;
          throw ComException( "Get State Failed", ::GetLastError() );
      }

    FDCB.BaudRate  =        9600;
    FDCB.ByteSize  =           8;
    FDCB.Parity    =    NOPARITY;
    FDCB.StopBits  =  ONESTOPBIT;
    if( ! ::SetCommState(hCom, &FDCB) )
      {
          ::CloseHandle( hCom );
          hCom = INVALID_HANDLE_VALUE;
          throw ComException( "Set State Failed", ::GetLastError() );
      }

    if( ! ::SetupComm(hCom, 1024*32, 1024*9) )
      {
          ::CloseHandle( hCom );
          hCom = INVALID_HANDLE_VALUE;
          throw ComException( "Setup Failed", ::GetLastError() );
      }

    FTimeOuts.ReadIntervalTimeout         =  15;
    FTimeOuts.ReadTotalTimeoutMultiplier  =   1;
    FTimeOuts.ReadTotalTimeoutConstant    = 250;
    FTimeOuts.WriteTotalTimeoutMultiplier =   1;
    FTimeOuts.WriteTotalTimeoutConstant   = 250;
    if( ! ::SetCommTimeouts(hCom, &FTimeOuts) )
      {
          ::CloseHandle( hCom );
          hCom = INVALID_HANDLE_VALUE;
          throw ComException( "Set Timeouts Failed", ::GetLastError() );
      }
}
//--------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
    if( hCom != INVALID_HANDLE_VALUE ) ::CloseHandle( hCom );
}
//--------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    if( hCom != INVALID_HANDLE_VALUE )
      {
          DWORD nBytes;
          AnsiString c = "Test String";
          if( ! ::WriteFile( hCom, c.c_str(), c.Length(), &nBytes, NULL) )
	    {
                 throw ComException( "Write Failed " + SysErrorMessage(::GetLastError()), ::GetLastError() );
            }
          ShowMessage("Write " + IntToStr(nBytes) );
      }
}
//--------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    if( hCom != INVALID_HANDLE_VALUE )
      {
          DWORD nBytes = 0;
          char Buffer[ 101 ] = { 0 };
          if( ! ::ReadFile(hCom, &Buffer, 100, &nBytes, NULL) )
            {
                throw ComException( "Read Failed " + SysErrorMessage(::GetLastError()), ::GetLastError() );
            }
          else ShowMessage( IntToStr(nBytes) + " " + Buffer );
      }
}
//--------------------------------------------------------------

~ JD
Re: Comm Port kicking my butt
#3476
Author: Michel Leunen
Date: Sat, 17 Jun 2006 18:59
21 lines
1574 bytes
JD wrote:

  > The write seems to work because no errors are reported and the
> number of bytes written are correct. The read, OTOH reports
> nada - no errors and no data.

>           char Buffer[ 101 ] = { 0 };
>           if( ! ::ReadFile(hCom, &Buffer, 100, &nBytes, NULL) )

if( ! ::ReadFile(hCom, Buffer, 100, &nBytes, NULL) )
or
if( ! ::ReadFile(hCom, &Buffer[0], 100, &nBytes, NULL) )

Michel
-- 
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Re: Comm Port kicking my butt
#3479
Author: JD
Date: Sat, 17 Jun 2006 19:15
11 lines
1128 bytes
Michel Leunen <nospam@noreply.please> wrote:
>
>if( ! ::ReadFile(hCom, Buffer, 100, &nBytes, NULL) )
>or
>if( ! ::ReadFile(hCom, &Buffer[0], 100, &nBytes, NULL) )

Did I ever blush when I saw that ... until I tried it. In both
cases, the application hangs.

~ JD
Re: Comm Port kicking my butt
#3480
Author: Remy Lebeau (TeamB)
Date: Sat, 17 Jun 2006 21:33
12 lines
1280 bytes
"JD" <nospam@nospam.com> wrote in message
news:449454bd$1@newsgroups.borland.com...

> In both cases, the application hangs.

Is data being written to the COM port that you can read in the first place?
That would be the only way ReadFile() could hang - it is not receiving any
data at all.


Gambit
Re: Comm Port kicking my butt
#3483
Author: JD
Date: Sun, 18 Jun 2006 13:50
10 lines
1194 bytes
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
> Is data being written to the COM port that you can read in
> the first place?

I don't know how to respond to that. I posted my code. What
do you think?

~ JD
Re: Comm Port kicking my butt
#3386
Author: Michel Leunen
Date: Sun, 18 Jun 2006 16:01
18 lines
1703 bytes
JD wrote:

> I don't know how to respond to that. I posted my code. What
> do you think?

What Remy told you is correct. The only way ReadFile() could hang is 
because no character at all is received otherwise the function returns 
with one parameter indicating how many characters were received.
Thus, the logical conclusion is that your app doesn't receive any character.

Michel
-- 
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Re: Comm Port kicking my butt
#3387
Author: Minas
Date: Sun, 18 Jun 2006 18:09
39 lines
2166 bytes
Hi JD ,


"JD" <nospam@nospam.com> wrote in message 
news:44944ca9$1@newsgroups.borland.com...

> I have absolutely no clue why I can't get this simple test to
> work. All it does is open a port, and write to it and read from
> it.
>
> The write seems to work because no errors are reported and the
> number of bytes written are correct. The read, OTOH reports
> nada - no errors and no data.

> //--------------------------------------------------------------
> void __fastcall TForm1::Button1Click(TObject *Sender)
> {
>    if( hCom != INVALID_HANDLE_VALUE )
>      {
>          DWORD nBytes;
>          AnsiString c = "Test String";
>          if( ! ::WriteFile( hCom, c.c_str(), c.Length(), &nBytes, NULL) )
>     {
>                 throw ComException( "Write Failed " + 
> SysErrorMessage(::GetLastError()), ::GetLastError() );
>            }
>          ShowMessage("Write " + IntToStr(nBytes) );
>      }
> }
> //--------------------------------------------------------------

Can your device understand the command "Test String" ?

If is a modem device better use "at\r" to be sure of Read operation.

_minas

------
"Only virtue's conquests have certainty" Sofokleous Erephyle
Re: Comm Port kicking my butt
#3388
Author: Bob Gonder
Date: Sun, 18 Jun 2006 19:31
36 lines
2150 bytes
JD wrote:

>The write seems to work because no errors are reported and the
>number of bytes written are correct. The read, OTOH reports
>nada - no errors and no data.

Are you sure that   throw ComException is working properly?
Do you have a sender attached to your comm port?
Is the cabling correct (null modem cable if computer-to-computer)?
Or proper loop-back?

>    FDCB.BaudRate  =        9600;
>    FDCB.ByteSize  =           8;
>    FDCB.Parity    =    NOPARITY;
>    FDCB.StopBits  =  ONESTOPBIT;
>    if( ! ::SetCommState(hCom, &FDCB) )

What about handshaking?
You don't set/reset Xon/off or DTR/DSR.
You might look into using BuildCommDCB

>    if( ! ::SetupComm(hCom, 1024*32, 1024*9) )

Those seem extra large.


>          DWORD nBytes;
>          AnsiString c = "Test String";
>          if( ! ::WriteFile( hCom, c.c_str(), c.Length(), &nBytes, NULL) )

Just a note of caution if you go back to overlapped....
If you return without waiting for completion, "c" goes away, and the
data being sent may get corrupted.
This will also work (without VCL):
	::WriteFile( hCom, "Test String", sizeof("Test String")-1,
			 &nBytes, NULL) )
Re: Comm Port kicking my butt
#3391
Author: Helmut Giese
Date: Sun, 18 Jun 2006 22:54
41 lines
2575 bytes
On 18 Jun 2006 06:50:35 -0700, "JD" <nospam@nospam.com> wrote:

>
>"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>>
>> Is data being written to the COM port that you can read in
>> the first place?
>
>I don't know how to respond to that. I posted my code. What
>do you think?

Hi JD,
looking at your code cannot answer Gambit's question, because his
question relates to the run-time of your program. I will re-phrase it:
Are you sure that some other computer is actually _sending_ something
to the port you're reading at?
Here is what I would try out:
- Connect your COM port to whatever source you have (if you have a
null modem cable this could well be the second COM port of your
machine).
- Start HyperTerminal on _your_ COM port.
- Get your source to send something - do you see the data arrive in
HyperTerminal? If not, you have to fix your setup until the
communication works.
Hint: If your ports are connected with a null-modem cable something
easy as "copy c:\autoexec.bat COM2:" can be really useful.
- Note the state of the handshake lines. (You do have this device
which lots of LEDs to show the current state of each line? If you
don't have it: run - don't walk - out to get one - they are life
savers.)
- Back in your program: Make sure the handshake lines have the exact
same state as noted when working with HyperTerminal.
- Repeat whatever you did to send something to HyperTerminal - your
program should receive it now.

Final hint: I would try at first with a null-modem cable and 
	"copy c:\autoexec.bat COM2:"
Until you get this to work there is no point in trying anything else.

HTH
Helmut Giese
Re: Comm Port kicking my butt
#3396
Author: Remy Lebeau (TeamB)
Date: Mon, 19 Jun 2006 02:37
14 lines
1480 bytes
"JD" <nospam@nospam.com> wrote in message
news:44955a2b$1@newsgroups.borland.com...

> I posted my code.

Which means absolutely nothing for the question I asked.  You can't read
data from a port if no data is being written to that port in the first
place.  Assuming that you are trying to read data from a port with a cable
attaching it to another computer/device, is that computer/device writing
data over the cable?  That is what I asked you, and what you need to answer.


Gambit
Re: Comm Port kicking my butt
#3397
Author: Bruce Larrabee
Date: Mon, 19 Jun 2006 09:01
6 lines
1244 bytes
	For what it's worth... I had to do exactly what Helmut recommends...

	It worked well...

	Bruce
Re: Comm Port kicking my butt
#3400
Author: Roddy Pratt
Date: Mon, 19 Jun 2006 14:11
14 lines
1590 bytes
"JD" <nospam@nospam.com> wrote in message 
news:44955a2b$1@newsgroups.borland.com...
>
> I don't know how to respond to that. I posted my code. What
> do you think?

Unless you physically add a 'loopback' DB9 connector, there's no connection 
between the 'read' and 'write' sides of a com port, so what your code is 
writing to it has no relevance to what you read from it...

Make an RS232 loopback connector by joining pins 2 and 3 on a D connector 
and then you may be able to 'read' what you 'write'.

 - Roddy
Re: Comm Port kicking my butt
#3415
Author: JD
Date: Wed, 21 Jun 2006 19:17
12 lines
1327 bytes
"Roddy Pratt" <roddy at spam fritter dot com> wrote:
>
> Unless you physically add a 'loopback' DB9 connector, [...]

BINGO!!

Thanks to all who responded and apologies to those who thought
that I actually had a device connected. I didn't know that
there was a difference.

~ JD
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads