Click here to Skip to main content
15,898,222 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo12-Jan-12 6:19
JDBravo12-Jan-12 6:19 
GeneralRe: VB2010 Serial Port Tutorial? Pin
Simon_Whale12-Jan-12 6:26
Simon_Whale12-Jan-12 6:26 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo12-Jan-12 6:38
JDBravo12-Jan-12 6:38 
AnswerRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn12-Jan-12 6:30
sitebuilderLuc Pattyn12-Jan-12 6:30 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo12-Jan-12 6:46
JDBravo12-Jan-12 6:46 
GeneralRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn12-Jan-12 6:57
sitebuilderLuc Pattyn12-Jan-12 6:57 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo12-Jan-12 7:17
JDBravo12-Jan-12 7:17 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo13-Jan-12 6:11
JDBravo13-Jan-12 6:11 
Hi Luc,

I'm happy to report I have this thing working both ways with the PCs. I'm sending and receiving bytes and they are changing controls on the application as I wanted. I do have a question, as I copied that part of the code and only understood it well enough to make it work, but I do need more pointers as explanations are not that clear (at least for me) on the tutorials I found (I intend to learn C later--don't like the syntax). The code below works as is. It does what I want and I can have it send/receive data (if required I can modify the chip software as it is easier for me Wink | ;) )

I want to make this program able to receive 2 data bytes (or more) instead of just one, using the delegation method (which I copied here and works fine here with one byte at a time). Those comments on the serial declarations and routines are what I understood from the explanations (please correct me if I'm wrong!)

VB
'**********************************************************************************
'Stuff that must preceed all code !!!
'**********************************************************************************
Imports System.Byte 'or Text if receiving/sending text instead
Imports System
Imports System.IO.Ports
Imports System.IO
Imports Microsoft.VisualBasic
Imports System.ComponentModel
Imports System.Threading
Imports Microsoft.Win32
Imports System.Runtime.Remoting.Messaging
'**********************************************************************************
'**********************************************************************************
'**********************************************************************************

Public Class Form1  'SERIAL PORT SET AT 9600BPS,8BIT,NO PARITY, NO STOP BIT, NO HW HANDSHAKE.

    Dim LEDOUT1, LEDOUT2 As Byte    'INTERNAL PROCESSING
    Dim LEDIN1, LEDIN2 As Byte
    Dim ADIN As Integer
    Dim LEDMSGOUT As Byte   'OUTPUT VARIABLES
    Dim PWMOUT As Byte
    Dim LEDMSGIN As Byte            'INPUT VARIABLES
    Dim AD1, AD2 As Byte            'INPUT VARIABLES

    Private mySerialPort As New SerialPort  'NAME THE PORT WHATEVER (MYSERIALPORT HERE)
    Private comBuffer As Byte()    'INPUT ARRAY VARIABLE
    Private Delegate Sub UpdateFormDelegate()   'THESE 2 LINES TO RECEIVE AUTOMATICALLY OR IT WON'T WORK
    Private UpdateFormDelegate1 As UpdateFormDelegate
    '**********************************************************************************
    '**********************************************************************************
    'THESE PROCEDURES RUN IN THE BACKGROUND IN ANOTHER THREAD
    'AUTOMATIC RECEPTION HAPPENS HERE
    Private Sub mySerialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
        'Handles serial port data received events
        UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)
        Dim n As Integer = mySerialPort.BytesToRead 'find number of bytes in buf
        comBuffer = New Byte(n - 1) {} 're dimension storage buffer
        mySerialPort.Read(comBuffer, 0, n) 'read data from the buffer
        Me.Invoke(UpdateFormDelegate1) 'call the delegate
    End Sub

    Private Sub UpdateDisplay()
        INCOMING.Text = CStr(comBuffer(0))
        'ProgressBar1.Value = comBuffer(1)-> doesn't work - a/d value from chip
        If INCOMING.Text = "50" Then
            If OvalShape1.BackColor = Color.Gray Then
                OvalShape1.BackColor = Color.Red
            Else
                OvalShape1.BackColor = Color.Gray
            End If
        End If
        If INCOMING.Text = "32" Then
            If OvalShape2.BackColor = Color.Gray Then
                OvalShape2.BackColor = Color.Red
            Else
                OvalShape2.BackColor = Color.Gray
            End If
        End If
        'PUT COMBUFFER RECEIVED DATA IN PROGRAM VARIABLES HERE for auto updating
    End Sub
    '**********************************************************************************
    '**********************************************************************************
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        'BYE
        End
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'DETERMINE WHAT COMMAND TO SEND TO CIRCUIT TO TURN LEDS OR NOT - CHECK LED1 BUTTON
        Dim OUTPUT(3) As Byte
        If LEDOUT1 = 0 Then     'TOGGLE LEDS
            LEDOUT1 = 1
        Else
            LEDOUT1 = 0
        End If
        LEDMSGOUT = 40 + LEDOUT1 + LEDOUT2 'THIS IS A BYTE VARIABLE TO OUTPUT -added 40 to see it in laptop
        'OUTPUT FORMAT: LEDS,PWM
        OUTPUT(0) = LEDMSGOUT
        OUTPUT(1) = PWMOUT
        mySerialPort.Write(OUTPUT, 0, 2) 'USE MYSERIALPORT AND SEND OUT COMBUFFER ARRAY, START AT BYTE 0, SEND 2 BYTES

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'DETERMINE WHAT COMMAND TO SEND TO CIRCUIT TO TURN LEDS OR NOT - CHECK LED2 BUTTON
        Dim OUTPUT(3) As Byte
        If LEDOUT2 = 0 Then     'TOGGLE LEDS
            LEDOUT2 = 1
        Else
            LEDOUT2 = 0
        End If
        LEDMSGOUT = 40 + LEDOUT1 + LEDOUT2   'THIS IS A BYTE VARIABLE TO OUTPUT
        'OUTPUT FORMAT: LEDS,PWM
        OUTPUT(0) = LEDMSGOUT
        OUTPUT(1) = PWMOUT
        mySerialPort.Write(OUTPUT, 0, 2) 'USE MYSERIALPORT AND SEND OUT COMBUFFER ARRAY, START AT BYTE 0, SEND 2 BYTES
    End Sub



    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'TO DO TIMED STUFF - SERIAL PORT: CAN SEND/RECEIVE EVERY TICK (NO LESS THAN 1 SECOND INTERVALS FOR TEST PROGRAM)THIS WORKS FROM ANOTHER TEST BUT NOT USED IN THIS CODE
    
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler mySerialPort.DataReceived, AddressOf mySerialPort_DataReceived
        'AT START OF PROGRAM ->SETUP COM1
        With mySerialPort           'I NEED IT TO RUN AT 9600 OR BETTER - SETTING 9600
            .PortName = "COM1"
            .BaudRate = 9600
            .DataBits = 8
            .Parity = Parity.None
            .StopBits = StopBits.One
            .Handshake = Handshake.None
        End With
        'AND OPEN PORT IF POSSIBLE
        Try
            mySerialPort.Open()         'OPEN COM1
        Catch ex As Exception
            MessageBox.Show(ex.Message) 'SEE IF ANY OTHER PROGRAM TOOK OVER COM1 AND GIVE WARNING
        End Try
    End Sub

    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        'TO ADD PWM SETTING TO SEND OUT TO CIRCUIT - BYTE SENT WORKS! UPDATES FAST
        Dim OUTPUT(3) As Byte
        PWMOUT = TrackBar1.Value
        OUTPUT(0) = LEDMSGOUT
        OUTPUT(1) = PWMOUT
        mySerialPort.Write(OUTPUT, 0, 2) 'USE MYSERIALPORT AND SEND OUT COMBUFFER ARRAY, START AT BYTE 0, SEND 2 BYTES
    End Sub


    
End Class


As I said, it works fine with the laptop but I'd like the ability to receive (at any time, as it is happening here) 2 bytes or more at a time.
AnswerRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn13-Jan-12 6:58
sitebuilderLuc Pattyn13-Jan-12 6:58 
AnswerRe: VB2010 Serial Port Tutorial? Pin
JDBravo13-Jan-12 10:39
JDBravo13-Jan-12 10:39 
AnswerRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn13-Jan-12 13:51
sitebuilderLuc Pattyn13-Jan-12 13:51 
Questionvb.net access controls by name Pin
alejx9-Jan-12 16:10
alejx9-Jan-12 16:10 
AnswerRe: vb.net access controls by name Pin
Luc Pattyn9-Jan-12 16:28
sitebuilderLuc Pattyn9-Jan-12 16:28 
AnswerRe: vb.net access controls by name Pin
Abhinav S9-Jan-12 21:28
Abhinav S9-Jan-12 21:28 
AnswerRe: vb.net access controls by name Pin
Simon_Whale9-Jan-12 21:52
Simon_Whale9-Jan-12 21:52 
QuestionADF Scanning Pin
ivo759-Jan-12 6:37
ivo759-Jan-12 6:37 
AnswerRe: ADF Scanning Pin
Simon_Whale9-Jan-12 12:37
Simon_Whale9-Jan-12 12:37 
GeneralRe: ADF Scanning Pin
ivo7510-Jan-12 2:23
ivo7510-Jan-12 2:23 
GeneralRe: ADF Scanning Pin
Richard MacCutchan10-Jan-12 2:39
mveRichard MacCutchan10-Jan-12 2:39 
QuestionCreate Guitar Tunner using Vb.net Pin
Mangore759-Jan-12 4:44
Mangore759-Jan-12 4:44 
AnswerRe: Create Guitar Tunner using Vb.net Pin
Simon_Whale9-Jan-12 4:55
Simon_Whale9-Jan-12 4:55 
GeneralRe: Create Guitar Tunner using Vb.net Pin
Mangore759-Jan-12 18:04
Mangore759-Jan-12 18:04 
GeneralRe: Create Guitar Tunner using Vb.net Pin
Simon_Whale9-Jan-12 21:49
Simon_Whale9-Jan-12 21:49 
GeneralRe: Create Guitar Tunner using Vb.net Pin
Mangore759-Jan-12 22:52
Mangore759-Jan-12 22:52 
GeneralRe: Create Guitar Tunner using Vb.net Pin
Dave Kreskowiak10-Jan-12 2:07
mveDave Kreskowiak10-Jan-12 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.