CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: PDA program

  1. #1
    Join Date
    Feb 2004
    Location
    Singapore
    Posts
    19

    PDA program

    Hi all,
    I'm new to eMbedded Visual C++ programming. Below is a program that my lecturer pass to me. But i don know why it cannot run correctly.

    P/S : sorry, my english not so good.

    Objective of this program : PDA display what was type on the Keyboard in an Edit Box.

    Configuration : PDA download the program from COM port 1 using Active Sync.
    After that, Connect PDA to COM port2, PDA display what i type on the keyboard.

    Problem : PDA failed to display what i type.



    // SENDDlg.cpp : implementation file
    //

    #include "stdafx.h"
    #include "SEND.h"
    #include "SENDDlg.h"

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    DWORD numWrite;
    int input;
    HANDLE hSend;
    BOOL err;
    TCHAR pString[18];
    CHAR *buffer = (CHAR*) malloc (sizeof(CHAR));


    /////////////////////////////////////////////////////////////////////////////
    // CSENDDlg dialog

    CSENDDlg::CSENDDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CSENDDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CSENDDlg)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }

    void CSENDDlg:oDataExchange(CDataExchange* pDX)
    {
    CDialog:oDataExchange(pDX);
    //{{AFX_DATA_MAP(CSENDDlg)
    // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP

    DDX_Control (pDX, IDC_RX1, m_pString1);

    }

    BEGIN_MESSAGE_MAP(CSENDDlg, CDialog)
    //{{AFX_MSG_MAP(CSENDDlg)
    //}}AFX_MSG_MAP
    ON_BN_CLICKED (IDC_BUTTON1, OnButton1)

    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // CSENDDlg message handlers

    BOOL CSENDDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    CString szPortName1;
    DCB dcb;
    COMMTIMEOUTS cto;


    // Set the icon for this dialog. The framework does this automatically
    // when the application's main window is not a dialog


    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    CenterWindow(GetDesktopWindow()); // center to the hpc screen

    //SetTimer(ID_CLOCK, 1000, NULL);

    szPortName1 = "COM2:";

    CloseHandle(hSend);

    hSend = CreateFile (szPortName1, GENERIC_READ | GENERIC_WRITE,
    0, NULL, OPEN_EXISTING, 0, NULL);

    if (hSend == INVALID_HANDLE_VALUE)
    {
    MessageBox(TEXT("ERROR"));
    }

    else
    {
    GetCommState (hSend,&dcb);

    dcb.BaudRate = CBR_9600;
    dcb.fParity = FALSE;
    dcb.fNull = FALSE;
    dcb.StopBits = ONESTOPBIT;
    dcb.Parity = NOPARITY;
    dcb.ByteSize = 8;

    SetCommState (hSend, &dcb);

    // Set the timeouts. Set infinite read timeout.

    cto.ReadIntervalTimeout = 0;
    cto.ReadTotalTimeoutConstant = 0;
    cto.ReadTotalTimeoutMultiplier = 0;
    cto.WriteTotalTimeoutConstant = 1000;
    cto.WriteTotalTimeoutMultiplier = 10;

    SetCommTimeouts (hSend, &cto);
    }

    return TRUE; // return TRUE unless you set the focus to a control
    }





    void CSENDDlg::OnButton1()
    {
    DWORD numRead;
    //CHAR szText1[1];
    CHAR szText[6];

    PurgeComm(hSend, PURGE_RXCLEAR);


    while (1)
    {
    if (hSend != INVALID_HANDLE_VALUE)
    {
    ReadFile (hSend, szText, 1, &numRead, 0);

    if (hSend == INVALID_HANDLE_VALUE)
    {
    MessageBox(TEXT("ERROR IN RECEIVING"));
    }
    else
    {
    input = szText[0];
    HEX();
    m_pString1.SetWindowText(pString);

    }
    }
    }
    }



    void HEX()
    {
    int j,k;

    j = (input & 0xFF) / 16;
    _itoa (j,&buffer[0],16);

    k = input & 0x00FF % 16;
    _itoa(k,&buffer[1],16);

    mbstowcs(pString,buffer,3);
    }

  2. #2
    Join Date
    Nov 2002
    Location
    Israel
    Posts
    182
    Are you sure about this line:
    szPortName1 = "COM2:";

    Maybe it's :
    szPortName = TEXT("COM2:");

    And loop in void CSENDDlg::OnButton1() looks very strange:
    1. how to stop it?
    2. you put a string into m_pString1 but when your program can draw it?
    3. you have an input buffer for ReadFile szText[6] but you use only one character from it.

    Can you change it so:

    DWORD numRead;
    CHAR szText1[1];

    BOOL bRead = ReadFile (hSend, szText, 1, &numRead, NULL);
    if (bRead) {
    input = szText[0];
    HEX();
    m_pString1.SetWindowText(pString);
    }
    Good luck

  3. #3
    Join Date
    Feb 2004
    Location
    Singapore
    Posts
    19
    Hi Caprice,

    Thanks for ur reply
    but too bad, the PDA edit box still display nothing...
    Is that i need to include WM_CHAR in the program???

  4. #4
    Join Date
    Nov 2002
    Location
    Israel
    Posts
    182
    Firstly you need to be sure that you send something and receive something. You can put AfxMessageBox(TEXT("Received")); if ReadFile returns TRUE.
    The code I wrote in the previous message will show (maybe) only one received character.
    If you think the problem is your edit box you can play with it only. For example put a button on the dialog and write something like m_edText.SetWindowText(TEXT("ONE"));
    or
    if you know how to use the class wizard:
    m_strEdit = TEXT("ONE");
    UpdateData(FALSE);
    Good luck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured