Assistance Required with VB.net


Closed Thread
Results 1 to 33 of 33

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    Hi;

    I think i can help, however you need to be more clear about your issue.
    I I'll put here some code,

    VB2008 Net Code;
    First you need to add the serilport class to your project.
    Code:
    Public Class Form1
        Dim WithEvents SerialPort1 As New IO.Ports.SerialPort
    Then, at Form Load, you need to configure and open your SerialPort;
    Code:
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            SerialPort1.PortName = "COM1"
            SerialPort1.Parity = IO.Ports.Parity.None
            SerialPort1.BaudRate = 19200
            SerialPort1.DataBits = 8
            SerialPort1.StopBits = 1
            SerialPort1.Open()
            SerialPort1.Write(Chr(13) & "Hugo Oliveira" & Chr(13))
        End Sub
    This handles the recetion from the buffer of the com port, and then i have a If statment to do the actions that need to be perfomed acording with the received data.
    Code:
        Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim recepcao As String
    
            recepcao = SerialPort1.ReadExisting
    
            If recepcao = "%TE" Then
                GetCursorPos(rect)
                SetCursorPos(rect.X - 1, rect.Y)
            ElseIf recepcao = "%TD" Then
                GetCursorPos(rect)
                SetCursorPos(rect.X + 1, rect.Y)
            ElseIf recepcao = "%TC" Then
                GetCursorPos(rect)
                SetCursorPos(rect.X, rect.Y - 1)
            ElseIf recepcao = "%TB" Then
                GetCursorPos(rect)
                SetCursorPos(rect.X, rect.Y + 1)
            ElseIf recepcao = "%TE1" Then
                GetCursorPos(rect)
                SetCursorPos(rect.X - 10, rect.Y)
            ElseIf recepcao = "%TD1" Then
                GetCursorPos(rect)
                SetCursorPos(rect.X + 10, rect.Y)
            ElseIf recepcao = "%TC1" Then
                GetCursorPos(rect)
                SetCursorPos(rect.X, rect.Y - 10)
            ElseIf recepcao = "%TB1" Then
                GetCursorPos(rect)
                SetCursorPos(rect.X, rect.Y + 10)
            ElseIf recepcao = "%TE2" Then
                GetCursorPos(rect)
                SetCursorPos(rect.X - 20, rect.Y)
            ElseIf recepcao = "%TD2" Then
                GetCursorPos(rect)
                SetCursorPos(rect.X + 20, rect.Y)
            ElseIf recepcao = "%TC2" Then
                GetCursorPos(rect)
                SetCursorPos(rect.X, rect.Y - 20)
            ElseIf recepcao = "%TB2" Then
                GetCursorPos(rect)
                SetCursorPos(rect.X, rect.Y + 20)
            End If
        End Sub

    With this piece of code in VB, you must with 100% sure, receive data from Com Port.

    Where is a part of code, from sendinf the data out of the pic, in PBP. I use the Debug command, but you can use Hserout, if you are using te Uart of the PIC, or Serout, Serout2 command.
    Code:
    Main:
    if JX>70 and JX<=100 then
       DEBUG "%TE"
       pause 20
      endif
      
       if JX>0 and JX<=70 then
       DEBUG "%TE1"
       pause 20 
      endif 
      
      if Jx>180 and Jx<210 then
       DEBUG "%TD"
       pause 20
      endif 
      
      if Jx>=210 then
       DEBUG "%TD1"
       pause 20
      endif
    '                             JoyStick Eixo Y
    ' ====================================================================  
      if JY>70 and JY<=100 then
       DEBUG "%TB"
       pause 20 
      endif
      
      if JY>0 and JY<=70 then
       DEBUG "%TB1"
       pause 20 
      endif  
      
      if Jy>180 and Jy<210 then
       DEBUG "%TC" 
       pause 20
      endif
      
      if Jy>=210 then
       DEBUG "%TC1" 
       pause 20
      endif    
    '                             Axis -  Eixo X
    ' ====================================================================
      if Ax<100 then
       DEBUG "%TE2"
       pause 20 
      endif
      
     if Ax>140 then
       DEBUG "%TD2" 
       pause 20
      endif
    '                             Axis -  Eixo Y
    ' ====================================================================  
      if Ay<110 then
       DEBUG "%TB2" 
       pause 20
      endif
      
      if Ay>140 then
       DEBUG "%TC2" 
       pause 20
      endif
    '                             Botões
    ' ====================================================================    
      IF BE = 0 then
       DEBUG "%RL1" 
       pause 80
      endif
    
      IF BD = 0 then
       DEBUG "%RD1" 
       pause 80
      endif    
    goto Main
    Hope it can hep.

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Here are two additions from me:

    Code:
    ComboBox1.Items.AddRange(SerialPort1.GetPortNames())
    This way, you can get the available comports (port list) from your PC hardware directly into a combo box.

    Then,
    Code:
    SerialPort1.PortName = ComboBox1.SelectedItem
    This way, you can select a com port (if there is one).

    Try all of these operations in try/catch so that if a comport is not available, you can prompt a message to the user.
    Also, if there is comport but not selected in combobox, you can also catch it and prompt to the user.


    ----------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default Thanks.

    Guys, thanks for the assistance, I'll have a play when I get home from work

    Much appreciate

  4. #4
    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.

  5. #5
    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

  6. #6
    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

  7. #7
    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

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