Thread: Windows 7 Machine: Simple "C" Program to Read Keyboard Input and Write To Serial Port

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    9

    Windows 7 Machine: Simple "C" Program to Read Keyboard Input and Write To Serial Port

    I should know how to do this, but I haven't written any "C" code in years!

    We have these proprietary keyboards here that have either failed or are in the process of failing. We have tried to get them repaired or replaced, to no avail. They have special graphics keys on them. They are self-powered and connect directly to a proprietary Graphics Generating "Box" that drives an HMI development environment.

    We have the mappings for the keys. All we need is a simple "C" program to run on a Windows 7 Desktop that will read keyboard input (from a Logic Controls KB-1800 programmable keyboard), and then send the appropriate 2-byte sequence out the Serial Port (DB9 Connector) to the Graphics Box (DB25 connector).

    I think I need to use getc(stdin) (???) ... but how is the data written out the Serial Port? Is there a special function for that (which would also set up the Comms parameters (like 9600, 8 bits, 1 stop bit, No parity).

    First Key to make work: LOGIN. When this key is pressed, the HEX sequence "10 0D" is sent to the Graphics Box from the (failing) proprietary keyboard
    Last edited by Colt Hero; 02-24-2022 at 03:35 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,126
    Console Functions - Windows Console | Microsoft Docs
    For getting lower level access to each keypress on your input keyboard.

    Communications Resources - Win32 apps | Microsoft Docs
    For writing things out to a serial port.

    Some example ideas here
    Serial Port Programming on Windows using Win32 API | xanthium enterprises
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    92
    A lot is going to depend on what kind of computer you are using. If you're on Windows, versus Linux, the way to code this will change. If you're talking about building an Arduino board to replace the keyboard's internal hardware, the code will be different. So what kind of program are you writing?

  4. #4
    Registered User
    Join Date
    Feb 2022
    Posts
    9
    I've tried to reply to this thread twice now, and even after waiting overnight and a few hours today, neither post has appeared

    HUH! Whadaya know? This short Post finally appeared!!

    ============
    @Salem: Thanks for those Links! I *did* get it working. Really wasn't very difficult. More just silly mistakes with the Connectors and trivial programming stuff like defining the array properly so I'm not inadvertently sending zeros. Still haven't "fleshed" all the code out ...will probably just be a massive CASE structure (and maybe some surprises coming with regard to building Displays in the EDIT Mode) ... but I can at least LOGIN now, and it's changing screens correctly ... so that's the big thing.

    @aghast: It's running on a Windows 7 machine. There's no "board". Just a desktop PC program to map about 170 keys and sent in short buffer to the proprietary Graphics Box.

    But here's a question for you guys ... same as in the original post: when a key is pressed on the keyboard, it has to be displayed (if it's a screen-printable character), or it's function (select object, for example) has to take effect immediately. Can't have to press ENTER, or wait for some buffer to fill before the key press works.

    So - which function is it that achieves that? When I first started with this I thought it was just "getce" or "getche" ... but those both seemed to be buffering the input ... instead of the key just immediately being read into the program as it was depressed.

    This is just going to be a big WHILE loop with a CASE structure (or is it FOR? in "C"?) inside of it that should read a single key press, make it immediately available to the program, so it can be packaged into a framed buffer and sent out the COM1 port to the proprietary Graphics Box.

    Also ... what is is BEEP Function call? Because along the way of playing with the Comms Parameters (and the DB9 Connectors) to get this program to talk to the Graphics Box, it started replying with a Code-7, which I subsequently found in the System Programming Manuals to mean the Computer "BEEP" (meaning ... "that buffer you just sent me is not valid"). So ... this "C" program should be able to make the keyboard BEEP if that status comes back ... just like the native keyboard did...

    UPDATE: OK … I just ran across some things to try in the FAQs! Saw this via the “Welcome To The Site” PM…

  5. #5
    Registered User
    Join Date
    Feb 2022
    Posts
    9
    Salem: Thanks for those Links! I *did* get it working. Really wasn't very difficult. More just silly mistakes with the Connectors and trivial programming stuff like defining the array properly so I'm not inadvertently sending zeros. Still haven't "fleshed" all the code out ...will probably just be a massive CASE structure (and maybe some surprises coming with regard to building Displays in the EDIT Mode) ... but I can at least LOGIN now, and it's changing screens correctly ... so that's the big thing.

    aghast: It's running on a Windows 7 machine. There's no "board". Just a desktop PC program to map about 170 keys and sent in short buffer to the proprietary Graphics Box.

    Looks like "_getch()" is reading the keypresses and passing them immediately into the program, so that's good.

    What about a Function to make the "BEEP" sound (when incorrect data is received or entered)?
    Last edited by Colt Hero; 03-18-2022 at 12:00 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,126
    Yeah, saying @someone will get your post moderated.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Feb 2022
    Posts
    9
    Salem: What do you mean "saying ATSIGNsomeone will get your post moderated"? On other Forums, "ATSIGNusername" is a "Mention" that gives that user a notification; I tried it here, and maybe that's why the post never got posted? Does it crash this system, or otherwise cause problems?

    ====

    Here's another thing I thought of: Since this program will only be used on this proprietary system, is it possible to program a keyboard as just a straight byte numerical index from 1-255? So ... just number the keys 1-255. Then the translation could just be a lookup table ... or a table of pointers to variable-length strings (instead of a lot of gory logic). I have a programmable keyboard with 22 keys x 7 rows = 154 keys (actually less because some keys take up 2-6 "X/Y locations" ... ENTER, SPACEBAR, for example). So with over 100 more options (to a max of 255), that would surely be enough to map all the Graphics keys on the proprietary keyboard (in addition to the "standard stuff").


    If I program the data strings into each key (which is what I was thinking I'd have to do), then the program gets these multi-byte strings (a byte at a time) and they have to be parsed to determine if it's one of the strings, or just a single, standard ASCII key that was pressed.

    UPDATE: 2 or 3 posts have now gone POOF and disappeared, either upon initial entry, or after editing and pressing "Save Changes". Is there something I need to read to prevent this from happening? It appears the ATSIGN is taboo here ... and if you use it, your posts get auto-deleted? Is that correct?
    Last edited by Colt Hero; 03-20-2022 at 11:58 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,126
    > Is there something I need to read to prevent this from happening?
    Do NOT put @ in your posts.
    I don't know at what point you would level up to the point where you can put @ and it not moderate your post.
    Also, saying @someone does NOT cause them to be notified in any way either.
    This isn't like discord or reddit.


    > and if you use it, your posts get auto-deleted? Is that correct?
    > Does it crash this system, or otherwise cause problems?
    No, it gets posted, but only us mods can see it.
    We then have to tick a box and approve the post.
    You presently have 4 posts "in moderation" at the moment, but all are effectively obsolete with your reposts.

    To answer the real question, a lookup table seems a reasonable approach if you only have one style of keyboard.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial Port Comms - How to "argument" the COM Port I want
    By migadrenaline in forum Windows Programming
    Replies: 4
    Last Post: 03-08-2017, 11:51 AM
  2. read and write in serial port
    By neda in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2016, 10:31 PM
  3. read write serial port
    By Marvin Gorres in forum C Programming
    Replies: 16
    Last Post: 06-23-2013, 07:41 AM
  4. Replies: 1
    Last Post: 02-10-2009, 11:02 AM
  5. Write and read the book called "registry""
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 03-10-2002, 10:45 AM

Tags for this Thread