• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Recieving from serial in hexidecimal

Hi,
I am using a serial program from http://www.tetraedre.com/advanced/serial.php
to recieve 64-bits from the serial port. The problem is when it recieves them it puts them into a string as characters. I want the program to put them into a sring as hexidecmimal numbers cos i have the compare the input with a database containing hex. Is there any hope of being able to alter it. Any help would be appreciated.

Thanks,
Jonathan
0
screach
Asked:
screach
1 Solution
 
dttriCommented:
Hello,
You mean the program return "00111001..." as a string of bit value? If so, use a loop, extract 4 sequence numbers and then use this to calculate its hex value:
0000->0 (hex)
0001->1
0010->2
0011->3
0100->4
0101->5
0110->6
0111->7
1000->8
1001->9
1010->A
1011->B
1100->C
1101->D
1110->E
1111->F

Hope this help
0
 
DRY_GINCommented:
first of all - what languadge are you using ?? (C++, VIsual Basic ... )

i have no idea why you using third party app's to read from com port, because it's quite easy to do it directly
and once you do it yourself you be able to get binary data the way you want.
wherei s also Microsoft MSComm activeX control that will be able to read write and do events on COM ports, quite simple to use.

anyway in order to convert string to bytes array you can use this in visual basic

         For i = 0 To Len(StringData) - 1
            BinData(i) = Asc(Mid(StringData, i + 1, 1))
         Next
or
            BinData(i) = AscB(MidB(StringData, i + 1, 1))



in order to directly read from COM port in VB you can use something like this


 Declare Function CreateFile& Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName$, ByVal dwDesiredAccess&, ByVal dwShareMode&, ByVal lpSecurityAttributes&, ByVal dwCreationDisposition&, ByVal dwFlagsAndAttributes&, ByVal hTemplateFile&)
 Declare Function CloseHandle& Lib "kernel32" (ByVal hObject&)
 Declare Function FlushFileBuffers& Lib "kernel32" (ByVal hFile&)
 Declare Function GetCommState Lib "kernel32.dll" (ByVal nCid&, ByRef lpDCB As Any) As Boolean
 Declare Function SetCommState Lib "kernel32.dll" (ByVal nCid&, ByRef lpDCB As Any) As Boolean
 Declare Function GetCommTimeouts Lib "kernel32.dll" (ByVal hFile&, ByRef lpCommTimeouts As Any) As Boolean
 Declare Function SetCommTimeouts Lib "kernel32.dll" (ByVal hFile&, ByRef lpCommTimeouts As Any) As Boolean
 Declare Function WriteFile& Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite&, lpNumberOfBytesWritten&, ByVal lpOverlapped&)
 Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, Optional ByVal lpOverlapped As Long = 0&) As Long

sub some_example()
Dim bModemCommand(256) As Byte
         CommPort= "COM1"
         OpenPort = CreateFile("COM1", &HC0000000, 0, 0, 3, 0, 0)
         If OpenPort = -1 Then
            Msg = "Unable to open communication port " & CommPort
            exit sub
         End If
         'Read Byte array.
         RetVal = ReadFile(OpenPort, BinaryData(0), 200, RetBytes, 0)
         RetVal = CloseHandle(OpenPort)
end sub

In VC++ it's even easier


I can send you more source code, but im not sure if you want it.


regards

alex

P.S. if you want to modify third party code to provide you array of bytes instead of strings it would be quite hard...


0
 
screachAuthor Commented:
Sorry i wasn't really clear. I am using C in visual studio.net. The function is returning characters in a string as the input from the serial port. I want the string to hold the hexidecimal value of each character, obviously the new string would have to be double the size. Just wondering is there an easy way to convert it.

Thanks,
Screach
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
screachAuthor Commented:
I can only manipulate the character i cannot get to the input string before it is converted from 8 bits into a character.
0
 
DRY_GINCommented:
strings same way as bytes are only in 8 bit per item
If it's a unicode string then it's going to be two 8-bit items per char

if you have pointer to a string let's say str_ptr - you can convert it to the pointer to unsigned char
like this :

unsigned char  * new_pointer;

new_pointer = &(UCHAR)*str_ptr;

and each new_pointer[i] will be a byte

unless http://www.tetraedre.com/advanced/serial.php stripping of high bits of the input - this will give you access to all bits you want.

now if http://www.tetraedre.com/advanced/serial.php actually strip hig bits that means some of the information from serial port is gone forether - so in this case you need to reverse engeneering and modify
http://www.tetraedre.com/advanced/serial.php or use another software/method to read from serial port.
0
 
Shiju SasidharanCommented:
'==================================
Private Sub Command1_Click()
    MsgBox ConvertToHex("", "-")
    MsgBox ConvertToHex("This is Input")
    MsgBox ConvertToHex("This is Input", "-")
End Sub
Private Function ConvertToHex(InputDataString, Optional ByVal Seperator As String) As String
Dim lCount As Long
    For lCount = 1 To Len(InputDataString)
        If ConvertToHex = "" Then
            ConvertToHex = Hex(Asc(Mid(InputDataString, lCount, 1)))
        Else
            ConvertToHex = ConvertToHex & Seperator & Hex(Asc(Mid(InputDataString, lCount, 1)))
        End If
    Next lCount
End Function
'==================================
0
 
screachAuthor Commented:
Its ok lads, found a really simple way of doing it

   
            unsigned int c;
            TCHAR test[20];
            int i;

            
                  c = buffer[0];

                  wsprintf(test,"%x",c);

Test now holds the hex value.

Thanks for the help
Screach
0
 
moduloCommented:
PAQed with points refunded (250)

modulo
Community Support Moderator
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now