Thread: how its done(hyperterminal)?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    how its done(hyperterminal)?

    Hello, all,
    I cant figure out what kind of window /control to use in program for communication:
    - user will input trough keyboard, on 'Carriage Return' will send entered string via serial port - so user's input should be written into window as keystrokes are detected.
    - remote device will answer trough serial port. On some character program will trigger and
    output received string on same window, possibly - in different color, or in right half of
    window
    - some 'history' of sended/received messages is desired .and scrolling through this history
    is desired too.


    I'm absolute beginner and need to learn much. Any advise - where and what to look will be helpful.

    regards
    Stefan

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    First you need to decide on what framework you're going to use:
    - Win32
    - MFC
    - .NET
    - QT
    - etc

    Next you will need to learn how to use that framework. This can be done by searching for tutorials on the internet, or buying a book.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    sorry,bithub

    1.Win32 API, compiler Dev-C
    2.I'm trying to learn and i'm reading some tutorials. But i cant answer (from learned to this moment) - what primary control to use in this task.
    So, do You have suggestion?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    There probably isn't any Win32 control that would work for this. You could try the EDIT or RICHEDIT controls, and see if you could make them work. My guess is that you probably need to do the drawing yourself. You could use an EDIT control for user input, then just use TextOut to draw the text to the main window's client area. You will need to use GetTextExtentPoint32 to measure the text you write out to do line breaks, and also read up on Window Scrolling for your vertical scroll bar.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stefan63 View Post
    Hello, all,
    I cant figure out what kind of window /control to use in program for communication:
    - user will input trough keyboard, on 'Carriage Return' will send entered string via serial port - so user's input should be written into window as keystrokes are detected.
    - remote device will answer trough serial port. On some character program will trigger and
    output received string on same window, possibly - in different color, or in right half of
    window
    - some 'history' of sended/received messages is desired .and scrolling through this history
    is desired too.


    I'm absolute beginner and need to learn much. Any advise - where and what to look will be helpful.

    regards
    Stefan
    What you're describing here seems to be a pretty basic chat program...

    Basically you would use a single line edit control at the bottom of the window for your operator to type into. Above that have a listbox where you stack up the archived text...

    Clicking "send" or hitting Enter would then send the string via the port and add it (using LB_ADDSTRING) to the list box above the entry window. When the response comes back just add it to the listbox control.

    If you want to scroll back just use the vertical scroll bar on the listbox...

    Pretty simple really.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    need Your advises

    Hello, people,
    i done some progeress and need Your advises.


    What i have done:
    - small "input" edit control , where user enters trough keyboard
    (at the bottom of window, above status line) .
    Here i found problem with "Enter" key detection - solution i did : trigger on EN_MAXTEXT notification.

    - bigger "history" window - multiline edit control(above "input" window);
    Because here will be displayed both USER messages and REMOTE(received by comport) messages,
    i should mark somehow source of text line. One way is to add some prefixes - for example "U>" and
    "R>". But i hope i can change color for 2 different (by source) textlines. Unfortunatelyy i didnt

    find in MSDN - is this possible and if possible - how.

    Any comment and advise will be helpful.
    regards
    Stefan

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stefan63 View Post
    Hello, people,
    i done some progeress and need Your advises.


    What i have done:
    - small "input" edit control , where user enters trough keyboard
    (at the bottom of window, above status line) .
    Here i found problem with "Enter" key detection - solution i did : trigger on EN_MAXTEXT notification.
    Did you try to catch WM_COMMAND messages with IDOK as the command parameter? Generally the Enter key outputs IDOK and the Esc(ape) key outputs IDCANCEL... Which you should be able to catch in your main window's message loop. (Single line edit controls don't actually use these keys, so they are passed up to the main window)

    The alternative is to make an accellerator table in your program's resources. The accellerators could then be used to catch the Enter key. You could also add a "send" button the user can click instead of hitting enter.

    For these to work your message dispatcher has to look something like this...

    Code:
        // dispatch window messages
        while (GetMessage(&msg,NULL,0,0) > 0)
          if (!TranslateAccellerator(GetForegroundWindow(),hAccellerator,&msg)
            if(!IsDialogMessage(GetForegroundWindow(),&msg))
              { TranslateMessage(&msg);
                 DispatchMessage(&msg); }

    - bigger "history" window - multiline edit control(above "input" window);
    Because here will be displayed both USER messages and REMOTE(received by comport) messages,
    Actually a listbox is probably a better choice than an edit control. With edit controls, programatically adding text requires you to locate the end of the text buffer, careate a highlight then insert text at the marker. With listboxes you can just use the LB_ADDSTRING message and text will automatically go at the end, complete with automatic scroll bars. Another reason to use a list box there is buffer maintenance... If you decide you only want to keep, say, 150 lines of history, deleting a line from the top of a listbox before adding a new one to the bottom is a whole lot easier than marking text, deleting it, moving to the end then marking text and inserting in an edit box.

    This is because an edit window is continuous text in a single buffer where a list box is a line oriented device. If you aren't planning to edit the contents of the history window a list box is actually a lot easier to deal with.


    i should mark somehow source of text line. One way is to add some prefixes - for example "U>" and
    "R>". But i hope i can change color for 2 different (by source) textlines. Unfortunatelyy i didnt find in MSDN - is this possible and if possible - how.
    Most standard Windows text controls are monochrome... so your best bet is to just prepend a symbol on the beginning of each line. It doesn't need to be anything dramatic... <-- for input and --> for output should suffice.

    FWIW... looks like you're off to a pretty good start.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    thanks,Tater,

    About "Enter" catching:
    - i monitored WM_COMMAND , but didnt find IDOK and IDCANCEL.
    On pucture You can see what i see (not very helpful probably).
    Here 140 is ID for "history", 131 is ID for "input". (hiL,loL,hiW,loW - LPARAM,WPARAM)
    EN_MAXTEX notificatios appeared after i settled "input" to be multyline style.
    Even now is working, but probably i'm missing something.

    -About "history" window.
    I tryed first Listbox , but didnt find "scrolling" possibilities/keys,
    history was without scrollbars and "stopped" on last visible line,
    so i switched to Edit control. Tomorrow i'll search again.

    About color.
    Edit Control Text Operations (Windows)
    Here i read something(Complex Script in Edit Controls), which isnt very clear for me.
    I understand this, that text in Edit control can contain control/script sequences, which can
    change display properties of text even at line or character level. But probably i'm wrong.

    Thank You very much for answer and suggestions!
    Stefan

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stefan63 View Post
    About "Enter" catching:
    - i monitored WM_COMMAND , but didnt find IDOK and IDCANCEL.
    On pucture You can see what i see (not very helpful probably).
    Here 140 is ID for "history", 131 is ID for "input". (hiL,loL,hiW,loW - LPARAM,WPARAM)
    EN_MAXTEX notificatios appeared after i settled "input" to be multyline style.
    Even now is working, but probably i'm missing something.
    Did you monitor it with my message dispatcher?
    IDOK and IDCANCEL are produced by IsDialogMessage()...


    -About "history" window.
    I tryed first Listbox , but didnt find "scrolling" possibilities/keys,
    history was without scrollbars and "stopped" on last visible line,
    so i switched to Edit control. Tomorrow i'll search again.
    Try something like this...
    To create your listbox...
    Code:
        // Log window
        LogWind = CreateWindowEx(WS_EX_STATICEDGE,L"LISTBOX",NULL,
                      WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | WS_TABSTOP |
                      LBS_NOINTEGRALHEIGHT | LBS_NOSEL,
                      238,5,300,250,Wind[0],NULL,PgmInst,NULL);
    Then manage it like this....
    (This has a bunch of extra stuff in it, but I bolded the parts you should be interested in)

    Code:
    // write a log entry  
    VOID AddToLog(PWCHAR Action, PWCHAR Extra)
      { TCHAR       ls[MAX_LOGLINE] = {0}; // log string
        DWORD       sp = 0;        // array pointer   
        INT         li;            // list item
        // insert the date
        sp = GetDateFormat(LOCALE_USER_DEFAULT,DATE_SHORTDATE,NULL,NULL,ls,20);
        // add a space
        ls[sp-1] = L' ';
        // add the time
        sp += GetTimeFormat(LOCALE_USER_DEFAULT,TIME_FORCE24HOURFORMAT,NULL,NULL,&ls[sp],20); 
        // another space
        lstrcat(ls,L" ->  ");
        // space
        lstrcat(ls,Action);
        lstrcat(ls,L" ->  ");
        // tag on extra info    
        if (Extra)
          lstrcat(ls,Extra); 
        // trim log
        while (SendMessage(LogWind,LB_GETCOUNT,0,0) > Setting.LogLines)
          SendMessage(LogWind,LB_DELETESTRING,0,0); 
        // add new message  
        li = SendMessage(LogWind,LB_ADDSTRING,0,(LPARAM) &ls);   
        // scroll if not selected
        if (GetForegroundWindow() != LogWind)
          { SendMessage(LogWind,LB_SETCURSEL,li,0);    
            SendMessage(LogWind,LB_SETCURSEL,-1,0); }
        UpdateWindow(LogWind); }   

    About color.
    Edit Control Text Operations (Windows)
    Here i read something(Complex Script in Edit Controls), which isnt very clear for me.
    I understand this, that text in Edit control can contain control/script sequences, which can
    change display properties of text even at line or character level. But probably i'm wrong.
    Yep, there are wasy to do that. In fact the RichEdit control has most of that built in... but it's a lot of work to manage...


    How about posting your code so I can get a better look?
    Last edited by CommonTater; 03-23-2011 at 10:47 AM.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    Hi,Tater,

    Did you monitor it with my message dispatcher?
    Hi, Tater!
    Do You mean some tool/application?

    Tomorrow I'll try Your example Listbox , will investigate about IsDialogMessage() and
    show what i did.
    regards

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stefan63 View Post
    Hi, Tater!
    Do You mean some tool/application?
    No... I mean the message dispatcher in your windows program...

    It should look something like this...
    Code:
        // dispatch window messages
    
        while (GetMessage(&msg,NULL,0,0) > 0)
          if (!TranslateAccellerator(GetForegroundWindow(),hAccellerator,&msg)
            if(!IsDialogMessage(GetForegroundWindow(),&msg))
              { TranslateMessage(&msg);
                 DispatchMessage(&msg); }

    Tomorrow I'll try Your example Listbox , will investigate about IsDialogMessage() and
    show what i did.
    regards
    Okey dokey.... Again, the most helpful thing at this point is for you to post your source... at least the parts about the UI so we can get it all tweaked up...

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    default sourcecode ,1

    Hello ,Tater
    I played around Your suggetsions and learned a little about "accellerator"s, which was
    totally unknown for me. Thank , You, for suggestions!
    I also tryed "history" as LISTBOX and as MULTILINE EDIT,
    "input" as MULTILINE and SINGLELINE EDIT control - too.
    At last i worked a little about check boxes.
    Visual appearence of check boxes and status indicators(CTS,RTS,DSR,DTR)
    is not good and i should remake another way later.
    At this moment default tasks seem to work correct, so i'll go to fight with serial port.
    Fortunatelly i found good (probably) library.

    Attached is main.cpp.
    Cboard doesnt allow ZIP files, sorry for long attachment.
    I'm new in C and WinApi and any comment will be helpful.

    regards
    Stefan

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stefan63 View Post
    Hello ,Tater
    I played around Your suggetsions and learned a little about "accellerator"s, which was
    totally unknown for me. Thank , You, for suggestions!
    I also tryed "history" as LISTBOX and as MULTILINE EDIT,
    "input" as MULTILINE and SINGLELINE EDIT control - too.
    At last i worked a little about check boxes.
    Visual appearence of check boxes and status indicators(CTS,RTS,DSR,DTR)
    is not good and i should remake another way later.
    At this moment default tasks seem to work correct, so i'll go to fight with serial port.
    Fortunatelly i found good (probably) library.

    Attached is main.cpp.
    Cboard doesnt allow ZIP files, sorry for long attachment.
    I'm new in C and WinApi and any comment will be helpful.

    regards
    Stefan
    I took a look at your code and I hope you don't mind a few suggestions here...

    First... if you're working in C set your compiler to C mode and use a .c extension on your files. Also note that most combination C and C++ compilers absolutely favour the C++ extensions and are often very non-standard when compiling C.

    Second ... (and no personal connotation is intended)... that code is a mess. You seriously need to clean it up and get it organized in a clearn and readable manner.

    Third ... In windows message loops, where you have extrodinarily complex switch() statements to deal with, it is very unwise to put code in cases... It is far better to make each case into a function and call it from the case statement. While this may not matter for fairly simple code, you will understand why the first time you have to debug a switch() chain that's gone wrong. Also making everything a function call compartmentalizes your code and makes it a lot easier to debug...

    Fourth ... I generally avoid the use of prototypes in the same file as the functions themselves. That is I organize the functions above WinMain() in their natural order so that each function is known before it's called. Again this may not make much difference in a smallish program, but when you're trying to make sense of a chain of errors in something larger, the by order layout of your functions is very helpful in finding problems.

    As odd as it may sound to you... the simple text formatting of your source code can be, and often is, the key to coming back to it in 3 or 5 years and still being able to follow what you did... Things like blank lines between functions, comments about non-obvious things, simple formatting rules ("indentation") etc. can make all the difference between fixing a bug and having to re-write.

    But it's good to see you are learning here... I've been messing with WinApi in C for about 6 years now. I figure I've mastered about 10% of it... But what a great way to challenge yourself!

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    thanks, Tater,

    Thank You for comments and help!
    I'm so new , that even difference between C/C++/C# is too smooth in my head,
    hope i'll learn it.
    regards
    Stefan

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stefan63 View Post
    Thank You for comments and help!
    I'm so new , that even difference between C/C++/C# is too smooth in my head,
    hope i'll learn it.
    regards
    Stefan
    You will... What I was suggesting is to form good coding habits early so you don't trip over the bad ones later on.

    Always ask yourself the same question: "If I have to revise this code after not looking at it for 5 years, will I still be able to follow it?" ... if you even suspect the answer might be "No", it's time to do some cleaning up.

    As for the different programming languages... It is, I think, a mistake to meddle in all three at once. Learn one, get kinda good at it, then move on to the next. It's a whole lot less confusing that way... C++ and C# are both very "C-like" so C is the natural starting point... IMO.

Popular pages Recent additions subscribe to a feed