CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Opening Port

  1. #1
    Join Date
    Jul 2008
    Posts
    6

    Question Opening Port

    // SerialTest.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include "comport.h"

    int _tmain(int argc, _TCHAR* argv[])
    {
    // TODO: Specify Virtual COM port
    char* port = "COM5";

    char cmd[ 253 ];
    DWORD error = 0;

    printf( "Command: " );

    // Read command from console
    gets( cmd );

    do
    {
    // Open port
    error = PxSerialOpen( port );
    if ( error != 0 )
    {
    printf( "Error %08x opening %s.\n", error, port );
    break;
    }

    // Append CR and LF
    char buffer[ 256 ];
    sprintf( buffer, "%s\r\n", cmd );

    // Write command to port
    DWORD written = 0;
    error = PxSerialWrite( buffer, (DWORD)strlen( buffer ), &written );
    if ( error != 0 )
    {
    printf( "Error %08x writing %s.\n", error, port );
    break;
    }

    // TODO: Adjust timeout as needed
    const DWORD TIMEOUT = 1500; // Millisec

    DWORD elapsedTime = 0;
    DWORD lastRead = timeGetTime();

    // Read until TIMEOUT time has elapsed since last
    // successful read.
    while ( elapsedTime <= TIMEOUT )
    {
    DWORD bytesRead;

    error = PxSerialRead( buffer, sizeof( buffer ) - 1, &bytesRead );
    if ( error != 0 )
    {
    printf( "Error %08x reading %s.\n", error, port );
    break;
    }

    if ( bytesRead > 0 )
    {
    buffer[ bytesRead ] = 0; // Append NULL to print to console
    printf( "%s", buffer );

    lastRead = timeGetTime();
    }

    elapsedTime = timeGetTime() - lastRead;
    }
    }
    while ( 0 );

    printf( "\n" );

    // Close port
    error = PxSerialClose();
    if ( error != 0 )
    {
    printf( "Error %08x closing %s.\n", error, port );
    }

    return 0;
    }



    HELLO!
    I HAVE THIS C++ CODE:
    this code opening gpib port?
    if anyone know please send my message
    thanks

  2. #2
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: Opening Port

    Hi,

    what problem you are facing... ??? please be specific ?

    use code tags while posting code !

    -Anant
    "Devise the simplest possible solution that solves the problems"

  3. #3
    Join Date
    Jul 2008
    Posts
    6

    Question Re: Opening Port

    Hi
    I have a pc ,a gpib cable and an agilent 34401A and i want to send and receive command from the agilent..
    that is the problem..

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Opening Port

    Does PxSerialOpen, PxSerialRead or PxSerialWrite return an error?

  5. #5
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: Opening Port

    Hi,

    what errors are you getting in code you have posted ?? are you able to open port ?? send command receive responce ??

    try to initialise array before using it may have garbage data which results in wrong commands

    e.g char arr[256]={0};


    -Anant
    "Devise the simplest possible solution that solves the problems"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured