Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a C++ program which is used to get the data from a device. The device sends Hex data to the PC. I have written C++ program to get the data and convert it from HEX to ascii. However, i only get some strange characters.


main.cpp
-----------------------------------------------------------------------
C++
#include "windows.h"
#include<iostream>;
#include "fyph.h"
using namespace::std;

int main() 
{
	

    char	*dest;
    int		*destElem;
    int destIndex;
    dest = NULL;
    destIndex = 300;
    destElem = &destIndex;
	

    int n3 = 300;
    HANDLE hSerial;
    hSerial = CreateFile("COM1",
        GENERIC_READ | GENERIC_WRITE,
        0,
        0,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        0);
    if(hSerial==INVALID_HANDLE_VALUE){
        if(GetLastError()==ERROR_FILE_NOT_FOUND){
            cout<<"serial port does not exist. Inform user."<<endl;
        }
        cout<<"some other error occurred. Inform user."<<endl;
    }

    DCB dcbSerial2;
    FillMemory(&dcbSerial2,sizeof(dcbSerial2),0);
    dcbSerial2.DCBlength = sizeof(dcbSerial2);
    DCB dcbSerialParams2 = {0};
    dcbSerial2.DCBlength=sizeof(dcbSerialParams2);
    if (!GetCommState(hSerial, &dcbSerialParams2)) {

    }
    dcbSerialParams2.BaudRate=CBR_57600;
    dcbSerialParams2.ByteSize=8;
    dcbSerialParams2.StopBits=ONESTOPBIT;
    dcbSerialParams2.Parity=NOPARITY;

    COMMTIMEOUTS cmt; //create the object
    cmt.ReadIntervalTimeout = 1000;
    cmt.ReadTotalTimeoutMultiplier = 1000;
    cmt.ReadTotalTimeoutConstant = 1000;


    char szBuff3[300]="";
    char szBuff[300]="";
    DWORD dwBytesRead2 = 0while(1)
    {
        if(ReadFile(hSerial, szBuff3, n3, &dwBytesRead2, NULL)==true)
    {

  
	szBuff3[dwBytesRead2]=0;
	dest = hexToAscii(szBuff3,destElem,destIndex);

        dest[dwBytesRead2-1]=0;
	cout<<"dest: "<<dest<<endl;


  
  
        if(!ReadFile(hSerial, szBuff3, n3, &dwBytesRead2, NULL)){
        cout<<"error66"<<endl;
    }
    startval =0;
    finalval =0;
    tt = 0;
    CloseHandle(hSerial);
}

--------------------------------------------------------------------

C++
#include"stdio.h";
#include "malloc.h";
#include<iostream>;
#include <fyph.h>;
using namespace std;





char *hexToAscii (char *srcHex, int *destElements, int srcElements)
{
    int                index, destIndex;
    char    valUpper, valLower, *ascArray;
    int                lowerFlag;
    
    lowerFlag = 0;
    ascArray = NULL;
    *destElements = srcElements/2;
    ascArray = (char *)malloc (*destElements);
    if (ascArray == NULL) {
        return NULL;
    }
    for (destIndex = 0; destIndex < *destElements; destIndex++) {
        for (index = 0; index < 2; index++) {
            if (lowerFlag == 0) {
                if (srcHex[(destIndex * 2) + index] >= '0' && 
                    srcHex[(destIndex * 2) + index] <= '9') {
                    valUpper = srcHex[(destIndex * 2) + index] - '0';
                } else if (tolower (srcHex[(destIndex * 2) + index]) >= 'a' || 
                            tolower (srcHex[(destIndex * 2) + index]) <= 'f'){
                    valUpper = tolower(srcHex[(destIndex * 2) + index]) - 'a' + 10;
                }
                valUpper <<=4;
                lowerFlag = 1;
            } else {
                if (srcHex[(destIndex * 2) + index] >='0' && 
                    srcHex[(destIndex * 2) + index] <= '9') {
                    valLower = srcHex[(destIndex * 2) + index] - '0';
                } else if (tolower (srcHex[(destIndex * 2) + index]) >='a' || 
                            tolower (srcHex[(destIndex * 2) + index]) <= 'f'){
                    valLower = tolower (srcHex[(destIndex * 2) + index]) - 'a' + 10;
                }  
                lowerFlag = 0;
            }
        }
        ascArray[destIndex] = valUpper | valLower;
    }
    return ascArray;
}
Posted
Updated 18-Feb-10 7:10am
v5

I have corrected your code formatting above. Also you did not point out where your code is going wrong but I suspect the following two lines are not correct:
C++
} else if (tolower (srcHex[(destIndex * 2) + index]) >= 'a' ||
tolower (srcHex[(destIndex * 2) + index]) <= 'f')
{

the logical operator between the two expressions should be && as both conditions need to be true.
 
Share this answer
 
v2
Your code is largely unreadable as you forgot to put it between <pre></pre> tags; use the 'code block' button. Try and post just the portion that is going wrong, with comments around the actual lines in error. Note please do not post this into a new answer but edit your original question; and use the 'Quick Preview' tab to verify it before posting.
 
Share this answer
 
In addition to Richard's answer, are you sure the device sends hexadecimal characters? Doesn't it send binary data?
:)
 
Share this answer
 
v2
I would suggest you replace your read loop with the following
DWORD dwBytesRead2 = 0;
while(1)
{
    if(ReadFile(hSerial, szBuff3, 300, &dwBytesRead2, NULL)==true)	// fixed size 300
    {
        szBuff3[dwBytesRead2]=0;
        dest = hexToAscii(szBuff3,destElem,dwBytesRead2);	// use actual input count
// remove			dest[dwBytesRead2-1]=0;
		
        cout<<"dest: "<<dest<<endl;
			
        if(!ReadFile(hSerial, szBuff3, 300, &dwBytesRead2, NULL))	// fixed size 300
        {
            cout<<"error66"<<endl;
        }
    }
}


And add the following changes to the hexToAscii() function, to allow space for, and add the null terminator
// allocate space plus 1 for null
ascArray = (char *)malloc (*destElements + 1);

ascArray[destIndex] = '\0';    // add this before return
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900