// SetUartCommand.cpp : 定義主控台應用程式的進入點。
//

#include "stdafx.h"
#include <Windows.h>
#include "MxUartCommand.h"

int _tmain(int argc, _TCHAR* argv[])
{
	UART_COMMAND_FORMAT uartCommandFormat;
	DWORD deBytesReturned = 0;
	DWORD dwBytesToWrite;
    DWORD dwBytesWritten = 0;
    BOOL bErrorFlag = FALSE;
	DCB dcb;  
	HANDLE hSerial;
	BYTE uartCommandToDevice[9];

	/* Set attention */
	uartCommandFormat.ATTN = 0x7;

	/* Set address */
	uartCommandFormat.ADDR = 0xFF;

	/* Set command */
	uartCommandFormat.CMD[0] = 0x42;
	uartCommandFormat.CMD[1] = 0x52;
	uartCommandFormat.CMD[2] = 0x49;

	/* Set length */
	uartCommandFormat.LEN = 0x1;

	/* Set header checksum */
	uartCommandFormat.IHCHK = 0x1B;

	/* Set data */
	uartCommandFormat.PDATA = (byte *)malloc(uartCommandFormat.LEN);
	*uartCommandFormat.PDATA = 0x10;

	/* Set data checksum */
	uartCommandFormat.IDCHK = 0xEF;

	uartCommandToDevice[0] = 0x7;
	uartCommandToDevice[1] = 0xFF;
	uartCommandToDevice[2] = 0x42;
	uartCommandToDevice[3] = 0x52;
	uartCommandToDevice[4] = 0x54;
	uartCommandToDevice[5] = 0x1;
	uartCommandToDevice[6] = 0x10;
	uartCommandToDevice[7] = 0x66;
	uartCommandToDevice[8] = 0x99;

	/* Send the command */
	  HANDLE hFile; 
	  hFile = CreateFile(L"COM5",               // file to open
                       GENERIC_READ| GENERIC_WRITE,          // open for reading
                       0,       // share for reading
                       NULL,                  // default security
                       OPEN_EXISTING,         // existing file only
                       0, // normal file
                       NULL);                 // no attr. template
 
    if (hFile == INVALID_HANDLE_VALUE) 
    { 
        _tprintf(TEXT("Terminal failure: unable to open file \"%s\" for read.\n"), argv[1]);
        return -1; 
    }
	else
	{
		GetCommState(hSerial, &dcb);

		dcb.BaudRate = 9600;                     // set baud rate to 115200 bps
		dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;   // set Hardware flow control
		dcb.ByteSize = 8;                          // data size = 8
		dcb.StopBits = ONESTOPBIT;                 // stop bit = 1
		dcb.Parity = NOPARITY;                     // no parity

		// Update Port setting structure
		SetCommState(hSerial, &dcb);
	}

	dwBytesToWrite = sizeof(uartCommandFormat) + uartCommandFormat.LEN;
	dwBytesToWrite = 9;
	bErrorFlag = WriteFile( 
                    hFile,           // open file handle
                    uartCommandToDevice,      // start of data to write
                    dwBytesToWrite,  // number of bytes to write
                    &dwBytesWritten, // number of bytes that were written
                    NULL);            // no overlapped structure

    if (FALSE == bErrorFlag)
    {
        printf("Terminal failure: Unable to write to file.\n");
    }
    else
    {
        if (dwBytesWritten != dwBytesToWrite)
        {
            // This is an error because a synchronous write that results in
            // success (WriteFile returns TRUE) should write all data as
            // requested. This would not necessarily be the case for
            // asynchronous writes.
            printf("Error: dwBytesWritten != dwBytesToWrite\n");
        }
        else
        {
            _tprintf(TEXT("Wrote %d bytes Successfully.\n"), dwBytesWritten);
        }
    }

    CloseHandle(hFile);

	/*Free the allocated data */
	free(uartCommandFormat.PDATA);

	return 0;
}

