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

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

typedef struct _UART_COMMAND_FORMAT
{
	byte ATTN;
	byte ADDR;
	byte CMD[3];
	byte LEN;
	byte IHCHK;
	byte *PDATA;
	byte IDCHK;
}
UART_COMMAND_FORMAT,*PUART_COMMAND_FORMAT;

#define MAX_UART_COMMAND_SIZE 82

int _tmain(int argc, _TCHAR* argv[])
{
	BYTE uartCommandFromDevice[MAX_UART_COMMAND_SIZE];
	DWORD deBytesReturned = 0;
	UART_COMMAND_FORMAT uartCommand;
	int uartCommadSize = 0;
	HANDLE hSerial;
	DCB dcb;  

	/* Receive the uart command from scalar */
	  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);     
 
    if (hFile == INVALID_HANDLE_VALUE) 
    { 
        _tprintf(TEXT("Terminal failure: unable to open file \"%s\" for read.\n"), argv[1]);
		
        return -1; 
    }
	else
	{
		// Get Port setting structure
		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);
	}

	/* Read data from scalar */
	 if( FALSE == ReadFile(hFile, uartCommandFromDevice, 9, &deBytesReturned,false) )
    {
        printf("Terminal failure: Unable to read from file.\n GetLastError=%08x\n", GetLastError());
        CloseHandle(hFile);
        return -1;
    }
	 else
	 {
		 printf("deBytesReturned = %d\n", deBytesReturned);
	 }

	 printf("uartCommandFromDevice[0] = %x\n", uartCommandFromDevice[0]);
	 printf("uartCommandFromDevice[1] = %x\n", uartCommandFromDevice[1]);
	 printf("uartCommandFromDevice[2] = %x\n", uartCommandFromDevice[2]);
	 printf("uartCommandFromDevice[3] = %x\n", uartCommandFromDevice[3]);
	 printf("uartCommandFromDevice[4] = %x\n", uartCommandFromDevice[4]);
	 printf("uartCommandFromDevice[5] = %x\n", uartCommandFromDevice[5]);
	 printf("uartCommandFromDevice[6] = %x\n", uartCommandFromDevice[6]);
	 printf("uartCommandFromDevice[7] = %x\n", uartCommandFromDevice[7]);
	 printf("uartCommandFromDevice[8] = %x\n", uartCommandFromDevice[8]);

	 /* Map to uart command structure */
	 uartCommand.LEN = uartCommandFromDevice[5];
	 uartCommand.PDATA = (byte *)malloc(1);
	 uartCommadSize = uartCommand.LEN + 8;
	 memcpy(&uartCommand, uartCommandFromDevice, 7);
	 *uartCommand.PDATA = uartCommandFromDevice[7];
	 uartCommand.IDCHK = uartCommandFromDevice[8];
	 
	 /* Print the items in uart command */
	 printf("uartCommand.ATTN = %x\n", uartCommand.ATTN);
	 printf("uartCommand.ADDR = %x\n", uartCommand.ADDR);
	 printf("uartCommand.CMD[0] = %x\n", uartCommand.CMD[0]);
	 printf("uartCommand.CMD[1] = %x\n", uartCommand.CMD[1]);
	 printf("uartCommand.CMD[2] = %x\n", uartCommand.CMD[2]);
	 printf("uartCommand.LEN = %x\n", uartCommand.LEN);
	 printf("uartCommand.IHCHK = %x\n", uartCommand.IHCHK);
	 printf("uartCommand.PDATA = %x\n", *uartCommand.PDATA);
	 printf("uartCommand.IDCHK = %x\n", uartCommand.IDCHK);
	 
	/*Free the allocated data */
	free(uartCommand.PDATA);

	return 0;
}

