-
July 9th, 2008, 07:55 AM
#1
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
-
July 9th, 2008, 08:07 AM
#2
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"
-
July 9th, 2008, 08:17 AM
#3
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..
-
July 9th, 2008, 08:23 AM
#4
Re: Opening Port
Does PxSerialOpen, PxSerialRead or PxSerialWrite return an error?
-
July 9th, 2008, 08:31 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|