Thread: Hexnr. through serial COM port

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    21

    Hexnr. through serial COM port

    Hi,
    IŽd like to send hexnumbers through the serial interface of a PC to a ” processor.
    It works with a terminal, when I hit a key, for example 'a', 0100001 is stored in the ”p register.
    This I`d like to accomplish with the following C++ routine:

    while(i<rows &&fgets(buff, sizeof(buff), HexCode) != NULL
    {
    for ( j=0; j<=1; j++ )
    {
    if (sscanf(&buff[0],"%c%c",&x[i][j])==1)
    i++ ;

    ( Im intending to grab the first two chars of a textfile, resembling the byte in Hex format, like:
    FD
    9A
    DF.......


    The send routine through Com1 looks like this:

    WriteFile(hCom, &x[][], sizeof (x[][] ), &dwWritten, NULL);


    The PC crashes upon compilation, though.

    Im I doing the right thing to achiev what I want?
    I`d appreciate any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So are you trying to send say FD as one byte - 0xFD or as two bytes of a string "FD"

    And what's with all the x[][] ?

    PS
    Don't forget the code tags
    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
    Jan 2003
    Posts
    21
    I`m not shure.I think it should be sent as one byte, sunce that`s what the ”p processes at he other end.
    Is that what happens when I hit a key of the keyboard?

    the [][] contain the indexes of the for loops.

    Do hou have any idea how to send this data as one byte?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I guess something like this
    Code:
        char buff[] = "FCDA\n"; // example buffer from fgets
        int x;
        unsigned char c;
        sscanf(buff,"%02x",&x); // take only the first 2 chars
        printf("%x\n",x);
        c=x;                    // make it a single char
        WriteFile(hCom, &c, 1, &dwWritten, NULL);
    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 to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  2. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 05:56 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Reading and writing to a serial port
    By SwarfEye in forum C Programming
    Replies: 2
    Last Post: 08-18-2006, 12:28 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM