Assistance Required with VB.net


Closed Thread
Results 1 to 33 of 33

Hybrid View

  1. #1
    Join Date
    Feb 2010
    Location
    I live in the UK
    Posts
    562


    Did you find this post helpful? Yes | No

    Default

    Hi,

    I'm trying to send data from PIC - PC using the program mackrackit pointed to earlier in this thread.

    http://www.rentron.com/receiving_data.htm

    When I compile it I have this error message: Name 'comEvReceive' is not declared.

    Does anyone have an idea of a fix (with as much of an explaination as you can spare please, I'm new to this). I've done lots of Googling with various answer from 'Select Case' to it can't be done in the 2008 VB-Version.

    Code:
     Private Sub MSComm1_OnComm()
    Dim sData As String ' Holds our incoming data
    Dim lHighByte As Long   ' Holds HighByte value
    Dim lLowByte As Long    ' Holds LowByte value
    Dim lWord As Long       ' Holds the Word result
    
    ' If comEvReceive Event then get data and display
    If MSComm1.CommEvent = comEvReceive Then
    
        sData = MSComm1.Input ' Get data (2 bytes)
        lHighByte = Asc(Mid$(sData, 1, 1)) ' get 1st byte
        lLowByte = Asc(Mid$(sData, 2, 1))  ' Get 2nd byte
        
        ' Combine bytes into a word
        lWord = (lHighByte * &H100) Or lLowByte
        
        ' Convert value to a string and display
        lblRCTime.Caption = CStr(lWord)
        
    End If
    End Sub
    I guess I should add I'm using Visual Basic 2008 Express Edition.

    Any help much appreciated.

    Dave
    Last edited by LEDave; - 27th August 2010 at 17:13.

  2. #2
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by LEDave View Post
    Hi,

    I guess I should add I'm using Visual Basic 2008 Express Edition.

    Dave
    Hi,

    The code posted is VB6 and you are using VB2008.
    Try the code in the post 9, of this thread!
    Thanks and Regards;
    Gadelhas

  3. #3
    Join Date
    Feb 2010
    Location
    I live in the UK
    Posts
    562


    Did you find this post helpful? Yes | No

    Default

    Here's my attempt to modify the code from gadelhas's post above. My FORM has two push buttons, one opens the COM1 PORT the other closes it. I can't get it to show any data in the ListBox1 though from the PIC. The PIC code (shown below) outputs a RANDOM BYTE.

    Any Ideas? Anyone?

    Code:
    Public Class Form1
        Dim WithEvents SerialPort1 As New IO.Ports.SerialPort
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            SerialPort1.PortName = "COM1"
            SerialPort1.Parity = IO.Ports.Parity.None
            SerialPort1.BaudRate = 2400
            SerialPort1.DataBits = 8
            SerialPort1.StopBits = 1
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If SerialPort1.IsOpen = False Then SerialPort1.Open()
            If SerialPort1.IsOpen = True Then MsgBox("com1 port opened sucessfully")
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If SerialPort1.IsOpen = True Then SerialPort1.Close()
            If SerialPort1.IsOpen = False Then MsgBox("com1 port closed sucessfully")
    
        End Sub
    
        Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim recepcao As String
    
            recepcao = SerialPort1.ReadExisting
    
    
    
        End Sub
    
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            SerialPort1.Write(ListBox1.Text)
    PIC code:

    Code:
    A VAR WORD
    X VAR WORD
    Y VAR WORD
    Z VAR BYTE
    
    main:
    Random A
    Y = A * 100
    X = DIV32 25700
    LET Z = X
    
    SEROUT2 PORTC.3, 16780, [DEC z, 10, 13]
    pause 4000
    GOTO MAIN

  4. #4
    Join Date
    Feb 2010
    Location
    I live in the UK
    Posts
    562


    Did you find this post helpful? Yes | No

    Default

    Here's another program which does receive the same data from the PIC. Only it displays it vertically instead of in BYTES across the ListBox. Anyone know how to modify this to display the BYTES properly?

    This is how the data appears in the ListBox v's the serial Communicator which displays them properly.

    Code:
    VB has the numbers
    1
    3
    5
    *
    *
    2
    0
    3
    *
    And Serial Communicator has them
    135
    203
    Here's the program, this maybe the one to adapt / work on as it's nearly there:

    Code:
    Imports System
    Imports System.ComponentModel
    Imports System.Threading
    Imports System.Windows.Forms
    
    
    Public Class Form1
    
    
        'Buffer for receievd data***
    
        Private Delegate Sub AddListBoxItemInvoker(ByVal item As Object)
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            SerialPort1.PortName = "COM1"
            SerialPort1.Parity = IO.Ports.Parity.None
            SerialPort1.BaudRate = 2400
            SerialPort1.DataBits = 8
            SerialPort1.StopBits = 1
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            If SerialPort1.IsOpen = False Then SerialPort1.Open()
            If SerialPort1.IsOpen = True Then MsgBox("com1 port opened sucessfully")
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If SerialPort1.IsOpen = True Then SerialPort1.Close()
    
            If SerialPort1.IsOpen = False Then MsgBox("com1 port closed sucessfully")
    
        End Sub
    
        
    
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    
            Me.AddListBoxItem(SerialPort1.ReadExisting)
    
    
        End Sub
    
        Private Sub AddListBoxItem(ByVal item As Object)
    
            If Me.ListBox1.InvokeRequired Then
                'We are on a secondary thread so delegation is required.
                Me.ListBox1.Invoke(New AddListBoxItemInvoker(AddressOf AddListBoxItem), (item))
    
            Else
                'We are on the primary thread so add the item.
                Me.ListBox1.Items.Add(item)
            End If
        End Sub
    End Class
    Thanks for any help.

    Dave

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default

    Hi Dave,
    When you use something like this (and z is 135):
    Code:
    SEROUT2 PORTC.3, 16780, [DEC z, 10, 13]
    The PIC actually sends 5 bytes, not one (or three). It sends "1", "3", "5", (CR), (LF) that's why your VB program displays them one by one (because you read them from the serial buffer one by one).

    What is being sent is the ASCII codes for the digits 1,3,5 so what is actually being transmitted is 49, 51, 53, 10, 13 where 49 means the digit 1, 51 means the digit 3 and so on.

    If you remove the DEC modifer what is being sent is three bytes ie. 135,10,13 but now it's likely the VB program will fill the listbox with a little c with a hook below it (the character that corresponds to ASCII code 135).

    If you want the number (135) to be a "value" in VB (and not one or more text strings) then you need to read it as such - or convert it. My VB isn't up to speed (never has been) but I think there's something like
    Code:
    DIM myValue as Integer
    myValue = SerialPort1.ReadByte
     ' OR
    myValue = SerialPort1.ReadChar
    Then you populate your listbox with something like
    Code:
    Me.AddListBoxItem(myValue.ToString)
    In this case the ToString basically does for VB what the DEC modifer does for PBP, ie. it converts the value of 135 (a single byte) into the ASCII representation of each digit, "1", "3", "5".

    My VB syntax may not be correct in this example but I think you get the gist of it.

    /Henrik.

  6. #6
    Join Date
    Feb 2010
    Location
    I live in the UK
    Posts
    562


    Did you find this post helpful? Yes | No

    Default

    Hi Henrik,

    Thanks for that as ever.

    Really good explanation there. I'll have a look at it this evening and try and get it working properly.

    Again many thanks.

    Dave

  7. #7
    Join Date
    Feb 2010
    Location
    I live in the UK
    Posts
    562


    Did you find this post helpful? Yes | No

    Default

    Hi Henrik,

    Success, but..........!

    First of all the success. I changed the PIC code to:

    Code:
    SEROUT2 PORTC.3, 16780, [ z]
    Then changed this line in the VB program:

    Code:
     Me.AddListBoxItem(SerialPort1.ReadExisting)
    To:

    Code:
    Me.AddListBoxItem(SerialPort1.ReadByte)
    And away it went reading and displaying Random Bytes, brilliant

    Now for the but part. I actually want the VB program to read a WORD sent from the PIC (a short in VB?) however for the AddListBoxItem(SerialPort1.Readxxxx there is:

    ReadByte
    ReadChar
    ReadExisting
    ReadLine
    ReadTo

    No ReadShort

    Now I'm thinking somehow it's send a High Byte from the PIC then a Low Byte and join them together in VB, unless there's a 'ReadShort' option.

    Dave
    Last edited by LEDave; - 10th September 2010 at 00:01.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts