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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

VB.net, reading into 2 bytes from instrument, how do I separate them?

  • 11-03-2010 4:29pm
    #1
    Registered Users, Registered Users 2 Posts: 1,204 ✭✭✭


    I'm using VB.net to take a reading from an A/D converter. Using
    txtBytesToRead.Text = SerialPort.BytesToRead
    

    I can see that there are 2 bytes there to be read. What I'm reading in is a 12 bit digital signal from the A/D.

    What command can I use to see the 2 bytes? The .ReadLine method justs times out because my A/D doesn't send a carriage return and the .Read method is giving an error

    "Overload resolution failed because no accessible 'Read' accepts this number of arguments"

    before running the program.

    Does anyone here have any experience with this?


Comments

  • Registered Users, Registered Users 2 Posts: 1,181 ✭✭✭ronkmonster


    Are you trying to split the string in txtBytesToRead.Text to 2 individual parts?


  • Registered Users, Registered Users 2 Posts: 1,204 ✭✭✭Recon


    not in that textbox but later in the code, I just want to try to read the data from the A/D first and take it from there. The A/D returns 2 bytes that I'll need to work with separately before combining them. I'll assign each of these bytes to two variables (MSB and LSB).

    After looking through the help in Visual Studio 2005 I found
    Dim instance As SerialPort
    Dim buffer As Byte()
    Dim offset As Integer
    Dim count As Integer
    Dim returnValue As Integer
    
    returnValue = instance.Read(buffer, offset, count)
    

    where

    buffer
    The byte array to write the input to.

    offset
    The offset in the buffer array to begin writing.

    count
    The number of bytes to read.

    I'm getting an error from this saying that 'buffer' needs to be assigned a value but I'm not sure what Byte value to assign it!


  • Registered Users, Registered Users 2 Posts: 1,204 ✭✭✭Recon


    I have a demo of a labview program which works but I need to get it going in VB.net

    232spdalabviewprogram.jpg


  • Registered Users, Registered Users 2 Posts: 515 ✭✭✭NeverSayDie


    You probably need to initialise that Byte array to hold however many bytes you're planning on reading. You don't add Bytes to it, the Read() method will do that, but there presumably needs to be room in there for them. (I'm assuming you're setting the values for the count and offset params)


  • Registered Users, Registered Users 2 Posts: 1,181 ✭✭✭ronkmonster


    Dim buffer As Byte() = New Byte(2); Have to set the size of the array before use


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,456 ✭✭✭FSL


    I have not used VB net but here is an extract from a VB6 programme which grabs data from an instrument attached to a serial port
    MSComm2 is a comm control
    buf is a variant and d is a byte array

    Private Sub MSComm2_OnComm()

    If Me.MSComm2.CommEvent = comEvReceive Then
    buf = MSComm2.Input
    d = buf
    '

    'the rest of the code assigns specific byte(s) to
    'various variables
    e.g. tmst = Chr(d(295)) & Chr(d(296)) & Chr(d(297)) & Chr(d(298)) & Chr(d(299))
    '
    '
    end if

    end sub


  • Registered Users, Registered Users 2 Posts: 901 ✭✭✭EL_Loco


    so why not initialise the buffer() array with something?

    it should get filled with what's been written from the instance.read function/method anyway? (which is strange because if it's going to be filled with info from your .read part, why assign it a value before hand)

    Dim instance As SerialPort
    Dim buffer As Byte()
    Dim offset As Integer
    Dim count As Integer
    Dim returnValue As Integer
    
    buffer(0) = 1; // do arrays start at 0 or 1 in VB?
    
    // what are the values for the rest of the parametres in that statement?
    
    offset = 0; // start reading at the beginning
    count = 2; // to count 2 bytes
    
    // all the parametres should have values before you call this next line
    
    returnValue = instance.Read(buffer, offset, count)
    
    // maybe check the length of your array and see how many elements in the array got filled
    
    msgbox(ubound(buffer))
    
    


  • Registered Users, Registered Users 2 Posts: 515 ✭✭✭NeverSayDie


    EL_Loco wrote: »
    so why not initialise the buffer() array with something?

    it should get filled with what's been written from the instance.read function/method anyway? (which is strange because if it's going to be filled with info from your .read part, why assign it a value before hand)

    You don't have to put stuff in it beforehand, but you will have to specify what size it is - arrays in VB.NET aren't dynamic.


  • Registered Users, Registered Users 2 Posts: 1,204 ✭✭✭Recon


    Thanks for all the replies so far, when I try to use
    Dim buffer As Byte() = New Byte(2)
    

    I get an error "Type 'Byte' has no constructors", but it does take away the other error I was getting!


  • Registered Users, Registered Users 2 Posts: 515 ✭✭✭NeverSayDie


    Recon wrote: »
    Thanks for all the replies so far, when I try to use
    Dim buffer As Byte() = New Byte(2)
    

    I get an error "Type 'Byte' has no constructors", but it does take away the other error I was getting!

    That should probably be;
    Dim buffer As Byte(2)

    You can declare the array uninitialised if you want, but I don't think you can then initialise it to bounds in the same line.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 901 ✭✭✭EL_Loco


    damn you VB arrays. ;) hope it's working out anyway OP.


  • Registered Users, Registered Users 2 Posts: 515 ✭✭✭NeverSayDie


    EL_Loco wrote: »
    damn you VB arrays. ;) hope it's working out anyway OP.

    Well, if it's any consolation, that's how it works in C++, Java and C# as well :)

    If you do want dynamic collections btw, the Systems.Collections namespace has lots of handy classes in it - ArrayLists, generic lists and hashtables/dictionaries, stacks, queues, etc.


  • Registered Users, Registered Users 2 Posts: 1,204 ✭✭✭Recon


    Thanks again for the help, this is what the solution was...
    Dim MSB As Byte
                Dim LSB As Byte
    
                txtBytesToRead.Text = SerialPort.BytesToRead 'just to check that there's only 2 bytes there to be read
    
                MSB = SerialPort.ReadByte 'all I needed to do what read a byte twice...
                LSB = SerialPort.ReadByte
    
                txtMSB.Text = MSB 'just to show the values while testing
                txtLSB.Text = LSB
    
                voltage = ((((MSB * 256) + LSB) / 4095) * 5) 'it's a 12 bit A/D
    
                txtVolt.Text = voltage
    


Advertisement