Serial Controled circuit with picaxe

djsoftlayer

New Member
How to build a circuit controlled with a serial interface???...

HI! i want to build a circuit with a picaxe and i want send to the circuit bytes commmands from my pc across the serial port to it... The idea of the proyect is to build a dialler, i want to dial a number using a visual basic program and form it send commands to my picaxe conected in the serial port... but need to know were i connect the interface (PIN number and configuration).... i dont have problems with the visual basic program (im using winsock since several years) but im very new using the picaxe...

At the end i want to:

Build a circuit conected to the pc across the serial port that can send a recive bytes commands from a visual basic program...

Thanks for the support!
P.D: If anyone have a electronic diagram you can send it to my mail: djsoftlayer@yahoo.es ..... sorry about my english...
 
If you know how to get VB to send a serial command out of your PC's serial port, you are in good shape!

Just use the SERIN command on the PICAXE. You can find all the details (including the suggested use of resistors and which pins to connect to what) in Section 2, page 89 of the manual.
 

picaxester

Senior Member
I'm getting some errors :(

1)
Error 1 'PICAXE_thing.Form1' cannot refer to itself through its default instance; use 'Me' instead. C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\PICAXE thing\Form1.vb 20 9 PICAXE thing

2)
Error 2 Name 'Label1' is not declared. C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\PICAXE thing\Form1.vb 42 9 PICAXE thing

3)
Error 3 Name 'Label1' is not declared. C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\PICAXE thing\Form1.vb 98 13 PICAXE thing

4)
Error 4 Name 'Label1' is not declared. C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\PICAXE thing\Form1.vb 104 13 PICAXE thing

5)
Error 5 'PICAXE_thing.Form1' cannot refer to itself through its default instance; use 'Me' instead. C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\PICAXE thing\Form1.vb 112 9 PICAXE thing

6)
Error 6 'PICAXE_thing.Form1' cannot refer to itself through its default instance; use 'Me' instead. C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\PICAXE thing\Form1.vb 120 9 PICAXE thing
 

moxhamj

New Member
Hmm - need some more info though I'm not sure how to get it short of doing screen captures. If you copied the text from the instructable and wordwrap is on then some lines might end up as lines of code when they are supposed to be comments.

Errors 2,3 and 4 sound like the label1 has not been created - go to the toobox and put a label1 on the form. I didn't explicitly take screenshots of the label being created though this is in the text.

Error 1 5 and 6 are the same error so we only need to find where there is the faulty line. Can you select all with the text of the code and copy and paste it into this forum and I'll see if I can replicate the error.
 

picaxester

Senior Member
Adding label1 fixxed some of the errors :)

Heres the code:
Code:
Imports System.IO

Imports Strings = Microsoft.VisualBasic ' so can use things like left( and right( for strings

Public Class Form1

    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer) ' for sleep statements

    Dim WithEvents serialPort As New IO.Ports.SerialPort ' serial port declare

    Dim PicaxeRegisters(0 To 13) As Byte ' registers b0 to b13

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Timer1.Enabled = True ' put this in code as defaults to false when created

        Timer1.Interval = 5000 ' 5 seconds

        Form1.BackColor = Color.Red ' set to position 'red'

        Array.Clear(PicaxeRegisters, 0, 13) ' probably not needed as array declared blank

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        ' timer ticks every 5 seconds

        Call SerialTxRx() ' talk to picaxe

    End Sub

    Sub SerialTxRx()

        Dim LabelString As String ' string to display byte values

        Dim DataPacket(0 To 17) As Byte ' entire data packet "Data"+14 bytes

        Dim i As Integer ' i is always useful for loops etc

        Label1.Text = "" ' clear the text on the screen

        For i = 0 To 3

            DataPacket(i) = Asc(Mid("Data", i + 1, 1)) ' add the word "Data" to the packet

        Next

        For i = 0 To 13

            DataPacket(i + 4) = PicaxeRegisters(i) ' add all the bytes to the packet

        Next

        If serialPort.IsOpen Then

            serialPort.Close() ' just in case already opened

        End If

        Try

            With serialPort

                .PortName = "COM1" ' Most new computers default to com1 but any pre 1999 computer with a serial mouse will probably default to com2

                .BaudRate = 2400 ' 2400 is the maxiumum speed for small picaxes

                .Parity = IO.Ports.Parity.None ' no parity

                .DataBits = 8 ' 8 bits

                .StopBits = IO.Ports.StopBits.One ' one stop bit

                .ReadTimeout = 1000 ' milliseconds so times out in 1 second if no response

                .Open() ' open the serial port

                .DiscardInBuffer() ' clear the input buffer

                .Write(DataPacket, 0, 18) ' send the datapacket array

                Call Sleep(300) ' 100 milliseconds minimum to wait for data to come back and more if data stream is longer

                .Read(DataPacket, 0, 18) ' read back in the data packet array

                .Close() ' close the serial port

            End With

            For i = 4 To 17

                LabelString = LabelString + " " + Str(DataPacket(i)) ' turn into a text string

            Next

            Label1.Text = LabelString ' put the text string on the screen

        Catch ex As Exception

            'MsgBox(ex.ToString)' uncomment this if want to see the actual error message

            Label1.Text = "Timeout" ' will display this if picaxe not connected etc

        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Form1.BackColor = Color.Red ' change the box to red

        PicaxeRegisters(0) = 120 ' an arbitrary value for the servo

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Form1.BackColor = Color.Green ' box to green

        PicaxeRegisters(0) = 160 ' arbitrary value for the servo

    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub
End Class
 

picaxester

Senior Member
I just notested a warning:

Warning 2 Variable 'LabelString' is used before it has been assigned a value. A null reference exception could result at runtime. C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\PICAXE thing\Form1.vb 94 31 PICAXE thing
 

moxhamj

New Member
You can ignore the warning 2. Ok, working on the 3 error messages... Can you do a screen shot of the code view as well - where it is saying line 20,112 and 120 have errors I'm not sure which lines these are as the text above is double spaced so maybe line 20 is actually line 40. VB seems to put in little flags next to lines that have errors before you even run the code and a screenshot of the code view would show these up. There are a couple of wrap errors above and I'm not sure if they have occurred when you pasted my code or when you pasted your code back to the forum. A screenshot will help clarify. Cheers
 
Last edited:

picaxester

Senior Member
Photobucket is down for maintenance right now, but heres the code:

Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Timer1.Enabled = True ' put this in code as defaults to false when created

        Timer1.Interval = 5000 ' 5 seconds

        Form1.BackColor = Color.Red ' set to position 'red'

        Array.Clear(PicaxeRegisters, 0, 13) ' probably not needed as array declared blank

    End Sub
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Form1.BackColor = Color.Red ' change the box to red

        PicaxeRegisters(0) = 120 ' an arbitrary value for the servo

    End Sub
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Form1.BackColor = Color.Green ' box to green

        PicaxeRegisters(0) = 160 ' arbitrary value for the servo

    End Sub
ps. sorry about this, I probably did something stupid.
 

moxhamj

New Member
No need to be sorry - I'm actually quite grateful you are posting this because if you have an error then I can edit the text so others don't have the same problem. Am installing VB.Net on a new machine and doing a copy and paste from your code.
 

moxhamj

New Member
Ok, found the errors. You could just delete all the text and repaste it off the instructable, but specifically the errors references to picturebox1.backcolor which in your code seem to be changed to form1.backcolor eg In form1load subroutine, 3rd line change form1.backcolor to picturebox1.backcolor, in button1 code near the bottom do the same thing and in button2 code also change it. These three error lines should have wavy blue lines under them as that is what is coming up in my code. The code changes the color of the picture box, not the color of the form!
 

rikki_max

New Member
you have some simple errors

hi im a vb programmer and im still a secondary student but you do have simple errors and i would like to see all of your code and maybe send me your whole project i hope i could help but here's a tip send this code of to dreamincode.net and start a forum there i help out there most of the time i can.

by the way i need this system myself so when you done please make it so i can get the whole code your using e.g. vb, picaxe

good luck
 
Top