Click here to Skip to main content
15,896,063 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: WaitForSingleObject best practices Pin
ForNow28-Sep-16 11:41
ForNow28-Sep-16 11:41 
GeneralRe: WaitForSingleObject best practices Pin
ForNow28-Sep-16 15:57
ForNow28-Sep-16 15:57 
GeneralRe: WaitForSingleObject best practices Pin
leon de boer28-Sep-16 19:20
leon de boer28-Sep-16 19:20 
GeneralRe: WaitForSingleObject best practices Pin
leon de boer30-Sep-16 9:42
leon de boer30-Sep-16 9:42 
GeneralRe: WaitForSingleObject best practices Pin
ForNow30-Sep-16 9:52
ForNow30-Sep-16 9:52 
GeneralRe: WaitForSingleObject best practices Pin
Joe Woodbury24-Oct-16 14:31
professionalJoe Woodbury24-Oct-16 14:31 
GeneralRe: WaitForSingleObject best practices Pin
Joe Woodbury24-Oct-16 14:29
professionalJoe Woodbury24-Oct-16 14:29 
QuestionDevice on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon27-Sep-16 8:18
David MacKinnon27-Sep-16 8:18 
I have a device (Fluke data logger which uses 9600-8-N-1 and no flow control) for which I need to write an serial interface in Visual Studio 2010 (C++) but have not been able to get more than a single response from the unit. I can connect, send the request for (as an example) device identification, but subsequent requests for data from the unit produce no response (variable read remains equal to zero after each Readfile request). I know the unit works fine because I can use a standard terminal program to obtain the responses I a need.

Nothing in the forums here, on MSDN, or repeated Google searches seems to answer my question. This has been frustrating me for more than a week so I am looking for a fresh set of eyes to point out what I am probably missing.

This is a stripped-down version of the test code. Any ideas are welcome. For purposes of testing the device response I'm not using overlapping for testing, but will employ overlapping in the "real" code.

C++
#include "stdafx.h"

#include <stdio.h>
#include <conio.h>
#include <string.h>

#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
	int ch;
    HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);


	//---------------------------
    // Get keyboard mode
	printf("Getting keyboard mode\n");
    HANDLE keyboard = GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode;
    if (!GetConsoleMode(keyboard, &mode))
	{
		printf ("GetConsoleMode failed with error %d.\n", 
			GetLastError());
	}
	// Set for raw reading
	printf("Setting keyboard mode\n");
    mode &= ~ ENABLE_PROCESSED_INPUT;
    if (!SetConsoleMode(keyboard, mode))
	{
		printf ("SetConsoleMode failed with error %d.\n", 
			GetLastError());
	}


	//---------------------------
    // Configure the comm port.
	printf("opening comm port\n");
	TCHAR *port_name = TEXT("COM1"); // Keyspan port
    HANDLE file;
    file = CreateFile(port_name,
        GENERIC_READ | GENERIC_WRITE,
        0, 
        NULL, 
        OPEN_EXISTING,
        0,
        NULL);
    if ( INVALID_HANDLE_VALUE == file) {
		printf ("CreateFile failed with error %d.\n", 
			GetLastError());
		CloseHandle(file);
        if ( _kbhit() ) {
            ch = _getch();
        }
        return 1;
    }

    // Get the current DCB.
	printf("Getting com state\n");
    DCB port;
	SecureZeroMemory(&port, sizeof(port));
    port.DCBlength = sizeof(port);
    if (!GetCommState(file, &port))
	{
		printf ("GetCommState failed with error %d.\n", 
			GetLastError());
		CloseHandle(file);
        if ( _kbhit() ) {
            ch = _getch();
        }
        return 1;
	}

	// Build the state using a convience function
	// By default disables Xon/Xoff and Hardware flow control
	// Enable Xon/Xoff by appending with x
	// Enable hardware by appending with p
	printf("Building com state\n");
	TCHAR *DCBsettings = TEXT("baud=9600 parity=n data=8 stop=1");
    if (!BuildCommDCB(DCBsettings, &port))
	{
		printf ("BuildCommDCB failed with error %d.\n", 
			GetLastError());
		CloseHandle(file);
        if ( _kbhit() ) {
            ch = _getch();
        }
        return 1;
	}

	// Set the com port state
	printf("Adjusting com settings\n");
    if (!SetCommState(file, &port))
	{
		printf ("SetCommState failed with error %d.\n", 
			GetLastError());
		CloseHandle(file);
        if ( _kbhit() ) {
            ch = _getch();
        }
        return 1;
	}

    // Set timeouts on the comm port.
	printf("Setting port time-outs.\n");
    COMMTIMEOUTS timeouts;
    timeouts.ReadIntervalTimeout = 1;
    timeouts.ReadTotalTimeoutMultiplier = 1;
    timeouts.ReadTotalTimeoutConstant = 1;
    timeouts.WriteTotalTimeoutMultiplier = 1;
    timeouts.WriteTotalTimeoutConstant = 1;
    if (!SetCommTimeouts(file, &timeouts))
	{
		printf ("SetCommTimeouts failed with error %d.\n", 
			GetLastError());
		CloseHandle(file);
        if ( _kbhit() ) {
            ch = _getch();
        }
        return 1;
	}


	//---------------------------
	// Write request for device id
	printf("Writing *idn?<cr> to port\n");
    char init[] = "*idn?\n";
    DWORD written;
    if (!WriteFile(file, init, sizeof(init), &written, NULL))
	{
		printf ("WriteFile failed with error %d.\n", 
			GetLastError());
	}
    if (written != sizeof(init))
	{
		printf("not all data written to port\n");
	}


	//---------------------------
    // basic terminal loop:
    char buffer[256];  
	// Make sure this is blanked for printf
	memset(&buffer[0], 0, sizeof(buffer));
    DWORD read;
    char dataReq[] = "fetch? 3\n";
	int i;
    do {
        // check for data on port and display it on screen.
        if( !ReadFile(file, buffer, sizeof(buffer), &read, NULL) )
		{
			// Read successful
			printf ("ReadFile failed with error %d.\n", 
				GetLastError());
		}
        if (read){
			printf ("Characters read = %d.\n", 
				read);
			// There is data in the buffer
			for(i=0; i<(read+1); i++)
			{
				printf("%d ", buffer[i]);
			}
			printf("\n");

			printf("%s\n", buffer);

			// Reset all elements to zero
			memset(&buffer[0], 0, sizeof(buffer));

			Sleep(1000); // Pause 1 second for the slow human

			// Now ask for a measurement
			printf("Writing fetch? 3<cr> to port\n");
			if (!WriteFile(file, dataReq, sizeof(dataReq), &written, NULL))
			{
				printf ("WriteFile failed with error %d.\n", 
					GetLastError());
			}
			if (written != sizeof(dataReq))
			{
				printf("not all data written to port\n");
			}

		}
        // check for keypress, and write any out the port.
        if ( _kbhit() ) {
            ch = _getch();
            WriteFile(file, &ch, 1, &written, NULL);
        }
    // until user hits ctrl-backspace.
    } while ( ch != 127);


	//---------------------------
    // Close everything.
    CloseHandle(keyboard);
    CloseHandle(file);
    return 0;
}

AnswerRe: Device on RS232 only responds to first send request (Visual C++) Pin
jeron127-Sep-16 10:41
jeron127-Sep-16 10:41 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon27-Sep-16 11:38
David MacKinnon27-Sep-16 11:38 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
jeron127-Sep-16 11:44
jeron127-Sep-16 11:44 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon27-Sep-16 12:10
David MacKinnon27-Sep-16 12:10 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
jeron127-Sep-16 13:28
jeron127-Sep-16 13:28 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon28-Sep-16 12:30
David MacKinnon28-Sep-16 12:30 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
jeron129-Sep-16 4:19
jeron129-Sep-16 4:19 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon29-Sep-16 4:43
David MacKinnon29-Sep-16 4:43 
AnswerRe: Device on RS232 only responds to first send request (Visual C++) Pin
leon de boer27-Sep-16 17:18
leon de boer27-Sep-16 17:18 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon29-Sep-16 4:45
David MacKinnon29-Sep-16 4:45 
AnswerRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon29-Sep-16 4:40
David MacKinnon29-Sep-16 4:40 
Question_tcscpy_s and unused buffer part Pin
leon de boer27-Sep-16 5:39
leon de boer27-Sep-16 5:39 
AnswerRe: _tcscpy_s and unused buffer part Pin
Richard MacCutchan27-Sep-16 6:26
mveRichard MacCutchan27-Sep-16 6:26 
GeneralRe: _tcscpy_s and unused buffer part Pin
leon de boer27-Sep-16 18:36
leon de boer27-Sep-16 18:36 
AnswerRe: _tcscpy_s and unused buffer part Pin
Richard MacCutchan27-Sep-16 6:33
mveRichard MacCutchan27-Sep-16 6:33 
AnswerRe: _tcscpy_s and unused buffer part Pin
Chris Losinger27-Sep-16 7:28
professionalChris Losinger27-Sep-16 7:28 
GeneralRe: _tcscpy_s and unused buffer part Pin
leon de boer27-Sep-16 20:24
leon de boer27-Sep-16 20:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.