Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Visual Basic

Options
  • 16-12-2004 2:42pm
    #1
    Registered Users Posts: 1,366 ✭✭✭


    Ok guys, heres my problem

    Have a microcontroller connected to my PC. I'm communicating with the controller using VB and the mscomm control.

    I can send commands to the controller, and they work fine.
    The problem is dealing with data sent from the controller to the PC. I want to be able to see the responses.



    When data is received a com event is triggered.

    There is a byte of data on the input buffer.

    When I use mscomm1.input to retrieve the data, it returns a string. This is really a byte to information that I need to do some processing on....

    How do I convert from the string format to a byte format where the result is in the form:

    00100010

    I've tried using the Cbyte function, but it says type mismatch.

    Also would anyone know how to extract the upper and lower nibble from the byte. I'd imagine you would use the mid$ function?

    Cheers in advance,

    Martin


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    If you have bit shifting in VB (I forget) you just shuffle your bits across by 4.


  • Closed Accounts Posts: 54 ✭✭charlo_b


    Does this help??? Could probably be modified to suit your needs

    Private Sub Form_Load()
    Dim sCharstring As String
    Dim colBytes As Collection
    
        sCharstring = "and"
        Set colBytes = CharStringToBitStringCol(sCharstring)
        
        MsgBox "The word is :" & sCharstring & vbCrLf & _
               "-------------------------------------" & vbCrLf & _
               Mid(sCharstring, 1, 1) & " = " & colBytes(1) & " : Upper Nibble = " & Mid(colBytes(1), 1, 1) & " : Lower Nibble = " & Mid(colBytes(1), 8, 1) & vbCrLf & _
               Mid(sCharstring, 2, 1) & " = " & colBytes(2) & " : Upper Nibble = " & Mid(colBytes(2), 1, 1) & " : Lower Nibble = " & Mid(colBytes(2), 8, 1) & vbCrLf & _
               Mid(sCharstring, 3, 1) & " = " & colBytes(3) & " : Upper Nibble = " & Mid(colBytes(3), 1, 1) & " : Lower Nibble = " & Mid(colBytes(3), 8, 1)
        
    End Sub
    
    Private Function CharStringToBitStringCol(ByVal sChar As String) As Collection
    Dim bByte() As Byte
    
        Set CharStringToBitStringCol = New Collection
        ReDim bByte(Len(sChar))
        
        For i = 1 To Len(sChar)
            bByte(i - 1) = CByte(Asc(Mid(sChar, i, 1)))
            CharStringToBitStringCol.Add (bByte(i - 1) And 128) / 128 & _
                                        (bByte(i - 1) And 64) / 64 & _
                                        (bByte(i - 1) And 32) / 32 & _
                                        (bByte(i - 1) And 16) / 16 & _
                                        (bByte(i - 1) And 8) / 8 & _
                                        (bByte(i - 1) And 4) / 4 & _
                                        (bByte(i - 1) And 2) / 2 & _
                                        (bByte(i - 1) And 1) / 1
                                        
        Next
        
        
        
    End Function
    


  • Registered Users Posts: 1,366 ✭✭✭king_of_inismac


    Thanks for the help so far guys

    well, Ive been working on the code myself, and I've got this much working:

    count = MSComm1.InBufferCount

    sData = MSComm1.Input

    Debg.Text = "In buffer: " & count
    TotalByte = sData

    arrLen = UBound(TotalByte)

    buffer.Text = "Array length: " & arrLen + 1

    For i% = 0 To (1) Step 1
    Int_byte(i%) = CInt(TotalByte(i%))
    Hex_byte(i%) = Hex(Int_byte(i%))
    Next i%

    receivedData.Text = Hex_byte(0) & Hex_byte(1)


    This piece of code converts mscomm1.input from string to decimal and from decimal to hex as required.

    It works fine with 1 byte returned. However, the response from 1 particular command is 4 bytes long, and something weird happens. The input buffer length is 4 as expected, but the totalByte byte array size is only 2....

    What happened to the other bytes?

    TotalByte is declared as Dim TotalByte() As Byte

    Dunno why this is happening?


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,687 Mod ✭✭✭✭Capt'n Midnight


    Does it return a single character A$ ?

    [php]
    v=asc(a$)
    Lnibble =v and 15
    Lnibble =v mod 16

    Hnibble = v \ 16
    Hnibble = (v and 240 ) /16
    [/php]

    If it returns a 8 character string then there should be a built in function to return it value, though you may need to add formating characters to it to indicate it's a Binary represtentatin..


Advertisement