Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[General] Controlling PictureBox image using serial communication

Status
Not open for further replies.

jajek1234

Newbie level 1
Newbie level 1
Joined
Aug 20, 2019
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
17
Good day!

I made a basic Arduino program that prints "1" and "0" at a delay of 500ms per string using the following Arduino UNO code:


Code:
void setup() {
  // put your setup code here, to run once:
Serial.begin (9600);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println("1");
delay(1000);
Serial.println("0");
delay(1000);
}

My goal is to change the image of a PictureBox (in this case, named PictureBox1) into a specific image from my resources if it receives a "1" and another image for "0"
Now, in my VB program, I used a code posted by the user boyguitar1:

Code:
Public Class Form1
    Dim port_name As String()
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim THIS As String
    Dim send_data As String
    Dim input As Integer = 0
    Dim receive_data As String
    Dim Ctr As Control
    Dim BRx(256) As Byte

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Set up serialport 1

        SerialPort1.DataBits = 8
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.Parity = IO.Ports.Parity.None

        'Set up Combobox comport 1
        cbb_comport.DropDownStyle = ComboBoxStyle.DropDownList
        cbb_baudrate.DropDownStyle = ComboBoxStyle.DropDownList
        cbb_stopbit.DropDownStyle = ComboBoxStyle.DropDownList
        cbb_parity.DropDownStyle = ComboBoxStyle.DropDownList
        cbb_databit.DropDownStyle = ComboBoxStyle.DropDownList
        cbb_flow.DropDownStyle = ComboBoxStyle.DropDownList

        cbb_comport.Sorted = True
        port_name = IO.Ports.SerialPort.GetPortNames()
        For Each comp1 In port_name
            cbb_comport.Items.Add(comp1)
        Next
        cbb_comport.SelectedIndex = 0
        'Set up combobox baudrate 1
        cbb_baudrate.Items.Add(2400)
        cbb_baudrate.Items.Add(4800)
        cbb_baudrate.Items.Add(9600)
        cbb_baudrate.Items.Add(14400)
        cbb_baudrate.Items.Add(19200)
        cbb_baudrate.Items.Add(38400)
        cbb_baudrate.Items.Add(56000)
        cbb_baudrate.Items.Add(57600)
        cbb_baudrate.Items.Add(115200)
        cbb_baudrate.Items.Add(128000)
        cbb_baudrate.Items.Add(256000)

        cbb_baudrate.SelectedIndex = 2
        'Set up combobox databit 1

        'cbb_databit.SelectedIndex = 1




        btn_DisConnect.Enabled = False

    End Sub

    Private Sub btn_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Connect.Click
        Timer1.Enabled = True
        Timer1.Interval = 500
        SerialPort1.PortName = cbb_comport.SelectedItem
        SerialPort1.BaudRate = cbb_baudrate.SelectedItem



        SerialPort1.Open()
        Me.SerialPort1.DiscardOutBuffer()           'clear output buffer
        Me.SerialPort1.DiscardInBuffer()            'clear input buffer
        Me.SerialPort1.RtsEnable = False
        Me.SerialPort1.DtrEnable = False

        btn_DisConnect.Enabled = True
        btn_Connect.Enabled = False

        '----------------------------------------

        Me.Text = "Serial Communication" & "    " & "(Connected)"
        Panel1.BackColor = Color.Lime


        cbb_comport.Cursor = Cursors.No

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        For Me.i = 0 To 255
            BRx(i) = 0
        Next
        receive_data = SerialPort1.BytesToRead
        If receive_data = 0 Then Exit Sub
        If receive_data > 256 Then
            receive_data = 256
        End If
        For Me.j = 1 To receive_data

            BRx(input) = SerialPort1.ReadByte
            txt_receive.Text = txt_receive.Text & ChrW(BRx(input))
            input = input + 1
            If ChrW(BRx(input)) = "1" Then
                PictureBox1.Image = WindowsApplication1.My.Resources.Resources._1
            ElseIf ChrW(BRx(input)) = "0" Then
                PictureBox1.Image = WindowsApplication1.My.Resources.Resources._0
            End If
        Next

        input = 0
        Timer1.Enabled = False


        For Me.i = 0 To 255
            BRx(i) = 0
        Next
        Timer1.Interval = 500
        Timer1.Enabled = True

    End Sub

    Public Sub delay(ByVal wait As Long)
        Dim i As Long
        For i = 0 To wait
        Next i
    End Sub

    Private Sub btn_DisConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_DisConnect.Click
        SerialPort1.Close()
        Timer1.Stop()
        btn_DisConnect.Enabled = False
        btn_Connect.Enabled = True

        Panel1.BackColor = Color.Transparent

        btn_Connect.Enabled = True
        btn_DisConnect.Enabled = False
        cbb_comport.Enabled = True
        cbb_baudrate.Enabled = True
        cbb_databit.Enabled = True
        Me.Text = "MCU Firmware CheckSum" & "    " & "(Disconnected)"
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txt_receive.Clear()
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

        send_data = 0
        For Me.i = 0 To Len(txtSend.Text) - 1
            send_data = Mid(txtSend.Text, 1 + i, 1)
            Me.SerialPort1.Write(send_data)
            Call delay(90000)
        Next i
    End Sub
End Class

But it seems like my IF statement doesn't work at all.
 

Check if your PC's baud rate settings in Control Panel > Device Manager > COM Ports > Baudrate settings are the same as that in your VB application. Both should have same baudrate. Also it should match with Arduino programs baudrate.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top