⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tusb3410demo.cpp

📁 桦宣430研讨会资料盘里的430源码资料
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    if (COMBuffer[0] & 0x01)
      printf("SW1: DOWN  ");
    else
      printf("SW1: UP    ");

    if (COMBuffer[0] & 0x02)
      printf("SW2: DOWN  ");
    else
      printf("SW2: UP    ");

    if (COMBuffer[0] & 0x04)
      printf("SW3: DOWN  ");
    else
      printf("SW3: UP    ");

    if (COMBuffer[0] & 0x08)
      printf("SW4: DOWN  \n");
    else
      printf("SW4: UP    \n");
  }

  // Initiate next overlapped read operation
  ReadFileEx(hCom, COMBuffer, 1, lpOverlapped, FileIOCompletionRoutine);
}
//------------------------------------------------------------------------------
// int main(int argc, char* argv[])
//
// The main application code first scans for the attached MSP430-TUSB3410
// reference design, then opens the port and enters an event handling loop.
//
// IN:  argc                  CMD line param count - not used
//      argv                  CMD line params - not used
// OUT: return()              1 if program failed (No HW found, COM error, ...)
//                            0 otherwise
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
  DWORD dwIndex;
  TCHAR szDeviceName[MAX_PATH];
  TCHAR ComPort[20];
  DWORD dwReturnValue;
  DCB dcb;                                      // COM device-control block
  COMMTIMEOUTS CommTimeouts;                    // COM timeout structure
  HANDLE hConsoleInput;
  DWORD NumberOfEventsRead;
  INPUT_RECORD InputBuffer;
  BYTE PCButtonState = 0;
  BOOL TxPCButtonState = FALSE;
  BOOL ExitRequested = FALSE;

  printf("\nMSP430-TUSB3410 Reference Design Demonstration Software V1.00\n");
  printf("Andreas Dannenberg\n");
  printf("MSP430/TMS470 Applications\n");
  printf("Texas Instruments Inc.\n");
  printf("November 2005\n\n");

  printf("Scanning system for MSP430-TUSB3410 Reference Design VCP...");

  // Scan through all COM port class devices in the system and
  // check for a friendly name / HW ID match with the MSP430 TUSB3410
  // Reference Design demo board. COM devices from 0 to MAX_COM_SEARCH_INDEX
  // are scanned.
  for (dwIndex = 0; dwIndex < MAX_COM_SEARCH_INDEX; dwIndex++)
  {
    dwReturnValue = EnumComPorts(dwIndex, szDeviceName);

    if (dwReturnValue == ERROR_SUCCESS)
    {
      break;                                    // COM port found!
    }
    if (dwReturnValue == ERROR_WRONG_COM)
    {
      continue;                                 // Keep on searching...
    }
    else if (dwReturnValue == ERROR_NO_MORE_ITEMS)
    {
      printf("not found!\n");                   // End of list reached
      return 1;
    }
    else
    {
      printf("error!\n");                       // Other error
      return 1;
    }
  }

  printf("found on %s.\n", szDeviceName);

  strcpy((CHAR*)&ComPort[0], "\\\\.\\");        // Preceed name with "\\.\" to
  strcpy((CHAR*)&ComPort[4], szDeviceName);     // access higher # ports

  hCom = CreateFile(ComPort,                    // COM-Port
    GENERIC_READ | GENERIC_WRITE,               // read/write-mode
    0,                                          // Open w/ exclusive-access
    NULL,                                       // no security attributes
    OPEN_EXISTING,                              // creation distribution
    FILE_FLAG_OVERLAPPED,                       // Use overlapped I/O
    NULL);                                      // hTemplate must be NULL

  if (hCom == INVALID_HANDLE_VALUE)             // Open successful?
  {
    printf("Error opening %s!\n", szDeviceName);
    return 1;
  }

  printf("%s opened.\n", szDeviceName);

  if (!GetCommState(hCom, &dcb))                // Read current DCB
  {
    printf("Error during GetCommState()!\n");
    CloseHandle(hCom);                          // Close COM ressource
    return 1;                                   // Exit program
  }

  dcb.BaudRate        = 460800;                 // Set baud rate
  dcb.fBinary         = TRUE;
  dcb.fParity         = FALSE;
  dcb.fOutxCtsFlow    = FALSE;                  // Disable flow-control for
  dcb.fOutxDsrFlow    = FALSE;                  // working with only 3 lines
  dcb.fDsrSensitivity = FALSE;                  // (GND, RXD, TXD)
  dcb.fOutX           = FALSE;
  dcb.fInX            = FALSE;
  dcb.fNull           = FALSE;
  dcb.fRtsControl     = RTS_CONTROL_DISABLE;    // Disable flow control
  dcb.fAbortOnError   = FALSE;
  dcb.ByteSize        = 8;
  dcb.StopBits        = ONESTOPBIT;

  if (!SetCommState(hCom, &dcb))                // Set DCB
  {
    printf("Error during SetCommState()!\n");
    CloseHandle(hCom);                          // Close COM ressource
    return 1;                                   // Exit program
  }

  CommTimeouts.ReadIntervalTimeout         = 0;
  CommTimeouts.ReadTotalTimeoutMultiplier  = 1000;  // Wait 1s per RX char
  CommTimeouts.ReadTotalTimeoutConstant    = 0;
  CommTimeouts.WriteTotalTimeoutMultiplier = 0;
  CommTimeouts.WriteTotalTimeoutConstant   = 0;

  if (!SetCommTimeouts(hCom, &CommTimeouts))    // Set COM timeouts
  {
    printf("Error during SetCommTimeouts()!\n");
    CloseHandle(hCom);                          // Close COM ressource
    return 1;                                   // Exit program
  }

  hConsoleInput = GetStdHandle(STD_INPUT_HANDLE);

  // Get handle to console user interface
  if (hConsoleInput == INVALID_HANDLE_VALUE)
  {
    printf("Error during GetStdHandle()!\n");
    CloseHandle(hCom);                          // Close COM ressource
    return 1;                                   // Exit program
  }

  // Activate UART RX functionality
  if (!ReadFileEx(hCom, COMBuffer, 1, &OverlappedIn, FileIOCompletionRoutine))
  {
    printf("Error during ReadFileEx()!\n");
    CloseHandle(hCom);                          // Close COM ressource
    return 1;                                   // Exit program
  }

  printf("\nOperate the 1, 2, 3, and 4 number keys to send data.\n");
  printf("Press <ESC> to exit this program.\n\n");

  // Enter event processing loop...
  while (!ExitRequested)
  {
    // Check and process console events
    // Also, stay in alertable wait mode for that overlapped I/O callback
    // events can be processed.
    if (WaitForSingleObjectEx(hConsoleInput, 1, TRUE) == WAIT_OBJECT_0)
    {
      ReadConsoleInput(
        hConsoleInput,                          // Handle to console input buf
        &InputBuffer,                           // Buffer address
        1,                                      // Number of records to read
        &NumberOfEventsRead);                   // Number of records read

      if (InputBuffer.EventType == KEY_EVENT)   // Was keyboard event?
      {
        switch (InputBuffer.Event.KeyEvent.uChar.AsciiChar)
        {
          case '1' :                            // Key '1' pressed?
            if (InputBuffer.Event.KeyEvent.bKeyDown)
              PCButtonState |= 0x01;
            else
              PCButtonState &= ~0x01;
            TxPCButtonState = TRUE;             // Request TX
            break;
          case '2' :                            // Key '2' pressed?
            if (InputBuffer.Event.KeyEvent.bKeyDown)
              PCButtonState |= 0x02;
            else
              PCButtonState &= ~0x02;
            TxPCButtonState = TRUE;             // Request TX
            break;
          case '3' :                            // Key '3' pressed?
            if (InputBuffer.Event.KeyEvent.bKeyDown)
              PCButtonState |= 0x04;
            else
              PCButtonState &= ~0x04;
            TxPCButtonState = TRUE;             // Request TX
            break;
          case '4' :                            // Key '4' pressed?
            if (InputBuffer.Event.KeyEvent.bKeyDown)
              PCButtonState |= 0x08;
            else
              PCButtonState &= ~0x08;
            TxPCButtonState = TRUE;             // Request TX
            break;
          case 27 :                             // ESC-Key pressed?
            ExitRequested = TRUE;               // Request program exit
            break;
        }

        if (TxPCButtonState)                    // TX requested?
        {
          WriteFile(hCom, &PCButtonState, 1, NULL, &OverlappedOut);
          TxPCButtonState = FALSE;
        }
      }
    }
  }

  CloseHandle(hCom);                            // Close COM ressource
  return 0;
}
//------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -