The Free Site   |  vBuddy - make friends, share photos, blogs, have fun   |  Cheap Web Hosting - starting at $5

********************* VB.Net picaxe serial interface and ftp interface **********

Contains vb.net code, vb.net form designer and picaxe 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

'Dim Filename As String

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' need all this rubbish stuff - .net puts it in automatically when go form1events above/load

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

Timer1.Interval = 5000 ' 5 seconds

PictureBox1.BackColor = Color.Gray

PictureBox2.BackColor = Color.Gray

End Sub

Sub SerialTxRx()

Dim SendString As String

Dim ResponseString As String = ""

Dim LabelString As String

Dim i As Integer

SendString = "Data"

For i = 0 To 13

SendString = SendString + Chr(PicaxeRegisters(i))

Next

If serialPort.IsOpen Then

serialPort.Close()

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

.Parity = IO.Ports.Parity.None

.DataBits = 8

.StopBits = IO.Ports.StopBits.One

.ReadTimeout = 1000 ' milliseconds

.Open()

.DiscardInBuffer()

.Write(SendString)

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

ResponseString = serialPort.ReadExisting

.Close()

End With

If Len(ResponseString) <> 0 Then

'got a response so store in array and display

PictureBox1.BackColor = Color.Green

For i = 0 To 13 ' string is Data+14 bytes - store in array

PicaxeRegisters(i) = Asc(Mid(ResponseString, i + 5, 1))

Next

' display to, from and the data

LabelString = "To: "

For i = 0 To 2

LabelString = LabelString + Str(PicaxeRegisters(i)) + "."

Next

LabelString = LabelString + Str(PicaxeRegisters(3))

LabelString = LabelString + " From: "

For i = 4 To 6

LabelString = LabelString + Str(PicaxeRegisters(i)) + "."

Next

LabelString = LabelString + Str(PicaxeRegisters(7)) + " Data:"

For i = 8 To 13

LabelString = LabelString + " " + Str(PicaxeRegisters(i))

Next

Else

' no response - maybe not connected or not powered up or chip not programmed

PictureBox1.BackColor = Color.Red

For i = 0 To 13

PicaxeRegisters(i) = 0 ' returns zeros

Next

LabelString = "Picaxe disconnected"

End If

Label1.Text = LabelString

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

Sub FTPGetDirectoryListing()

Const host As String = "ftp://ftp.0catch.com" ' note the 0 is a zero not a character O

Const username As String = "picaxe.0catch.com"

Const password As String = "picaxetester"

Dim URI As String

URI = host

Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)

ftp.Credentials = New System.Net.NetworkCredential(username, password)

ftp.KeepAlive = False

ftp.UseBinary = True

ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails

Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)

Using responseStream As IO.Stream = response.GetResponseStream

Using fs As New IO.FileStream("c:\listfiles.txt", IO.FileMode.Create)

Dim buffer(2047) As Byte

Dim read As Integer = 0

Do

read = responseStream.Read(buffer, 0, buffer.Length)

fs.Write(buffer, 0, read)

Loop Until read = 0

responseStream.Close()

fs.Flush()

fs.Close()

End Using

responseStream.Close()

End Using

response.Close()

End Using

End Sub

Sub FTPUpload(ByVal Filename As String)

Dim localFile As String 'place to store data

Dim remoteFile As String ' filename is case sensitive this is really important

Const host As String = "ftp://ftp.0catch.com" ' note the 0 is a zero not a character O

Const username As String = "picaxe.0catch.com"

Const password As String = "picaxetester"

Dim URI As String

localFile = "C:\" + Filename ' store in root directory but can change this

remoteFile = "/" + Filename

URI = host + remoteFile

Try

Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)

ftp.Credentials = New System.Net.NetworkCredential(username, password)

ftp.KeepAlive = False

ftp.UseBinary = True

ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile

Dim fs As New FileStream(localFile, FileMode.Open)

Dim filecontents(fs.Length) As Byte

fs.Read(filecontents, 0, fs.Length)

fs.Close()

Dim requestStream As Stream = ftp.GetRequestStream()

requestStream.Write(filecontents, 0, filecontents.Length)

requestStream.Close()

Catch 'can't connect

PictureBox2.BackColor = Color.Red

Label2.Text = "FTP disconnected"

End Try

End Sub

Sub ftpdownload(ByVal Filename As String)

' downloads remotefile to localfile

Dim localFile As String 'place to store data

Dim remoteFile As String ' filename is case sensitive this is really important

Const host As String = "ftp://ftp.0catch.com"

Const username As String = "picaxe.0catch.com"

Const password As String = "picaxetester"

Dim URI As String

localFile = "C:\" + Filename ' store in root directory but can change this

remoteFile = "/" + Filename

URI = host + remoteFile

Try

Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)

ftp.Credentials = New System.Net.NetworkCredential(username, password)

ftp.KeepAlive = False

ftp.UseBinary = True

ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

' read in pieces as don't know how big the file is

Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)

Using responseStream As IO.Stream = response.GetResponseStream

Using fs As New IO.FileStream(localFile, IO.FileMode.Create)

Dim buffer(2047) As Byte

Dim read As Integer = 0

Do

read = responseStream.Read(buffer, 0, buffer.Length)

fs.Write(buffer, 0, read)

Loop Until read = 0

responseStream.Close()

fs.Flush()

fs.Close()

End Using

responseStream.Close()

End Using

response.Close()

End Using

Catch ' put error codes here

End Try

End Sub

Sub TestFTP()

' send a blank file and read it back then send a file with data and check it comes back

Dim Filename As String

Filename = "Test.txt"

Dim fullfilename As String

fullfilename = "C:\" + Filename

Call Deletelocalfile(Filename)

Dim fs As New FileStream(fullfilename, FileMode.Create, FileAccess.Write)

Dim s As New StreamWriter(fs)

's.BaseStream.Seek(0, SeekOrigin.End)

s.WriteLine("Echo to ftp")

s.Close()

Call FTPUpload(Filename)

Call Deletelocalfile(Filename) ' make sure local one is gone

Call ftpdownload(Filename)

Dim readline As String

Try

Dim fr As New FileStream(fullfilename, FileMode.Open, FileAccess.Read)

Dim r As New StreamReader(fr)

readline = r.ReadLine()

r.Close()

Catch ' file doesn't exist

readline = ""

End Try

If readline = "Echo to ftp" Then

PictureBox2.BackColor = Color.Green

Label2.Text = "FTP connected"

Else

PictureBox2.BackColor = Color.Red

Label2.Text = "FTP disconnected"

End If

End Sub

Sub Deletelocalfile(ByVal Filename As String)

Dim Location As String = "C:\"

Try

Kill(Location + Filename)

Catch ex As Exception

' MsgBox(ex.ToString)

End Try

End Sub

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

Static counter As Integer

Call SerialTxRx() ' talk to picaxe

If counter < 0 Then ' one minute

counter = 11

Call TestFTP()

End If

counter = counter - 1

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Call FTPGetDirectoryListing()

MsgBox("done")

End Sub

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

Call MessageToFTP()

End Sub

Sub MessageToFTP()

' if picaxe is connected and if ftp is active then send the data to

Dim Filename As String = ""

Dim CompleteFileName As String

Dim i As Integer

Call CreateFileName(Filename)

CompleteFileName = "c:\" + Filename

' save picaxe data to filename

Dim fs As New FileStream(CompleteFileName, FileMode.Create, FileAccess.Write)

Dim s As New StreamWriter(fs)

For i = 8 To 13 ' no need to send the from and to address as these are in the filename.

s.WriteLine(Str(PicaxeRegisters(i)))

Next

s.Close()

' upload this file

Call FTPUpload(Filename)

MsgBox(Filename)

End Sub

Public Sub CreateFileName(ByRef Filename As String)

'note can't pass variables unless use public sub, not sub, and also byref, not byval

Dim i As Integer

Filename = ""

For i = 0 To 7

filename = filename + Strings.Right("000" + Trim(Str(PicaxeRegisters(i))), 3) + "x"

Next

filename = Strings.Left(filename, Len(filename) - 1)

filename = filename + ".txt"

End Sub

End Class

 

 

 

 

************************ Form designer ****************************

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

Partial Class Form1

Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.

<System.Diagnostics.DebuggerNonUserCode()> _

Protected Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing AndAlso components IsNot Nothing Then

components.Dispose()

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> _

Private Sub InitializeComponent()

Me.components = New System.ComponentModel.Container

Me.Label1 = New System.Windows.Forms.Label

Me.PictureBox1 = New System.Windows.Forms.PictureBox

Me.Timer1 = New System.Windows.Forms.Timer(Me.components)

Me.PictureBox2 = New System.Windows.Forms.PictureBox

Me.Label2 = New System.Windows.Forms.Label

Me.Button3 = New System.Windows.Forms.Button

Me.Button1 = New System.Windows.Forms.Button

Me.Label3 = New System.Windows.Forms.Label

CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()

CType(Me.PictureBox2, System.ComponentModel.ISupportInitialize).BeginInit()

Me.SuspendLayout()

'

'Label1

'

Me.Label1.AutoSize = True

Me.Label1.Location = New System.Drawing.Point(34, 11)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(86, 13)

Me.Label1.TabIndex = 0

Me.Label1.Text = "Picaxe Registers"

'

'PictureBox1

'

Me.PictureBox1.Location = New System.Drawing.Point(12, 11)

Me.PictureBox1.Name = "PictureBox1"

Me.PictureBox1.Size = New System.Drawing.Size(16, 15)

Me.PictureBox1.TabIndex = 13

Me.PictureBox1.TabStop = False

'

'Timer1

'

Me.Timer1.Interval = 5000

'

'PictureBox2

'

Me.PictureBox2.Location = New System.Drawing.Point(12, 43)

Me.PictureBox2.Name = "PictureBox2"

Me.PictureBox2.Size = New System.Drawing.Size(16, 15)

Me.PictureBox2.TabIndex = 15

Me.PictureBox2.TabStop = False

'

'Label2

'

Me.Label2.AutoSize = True

Me.Label2.Location = New System.Drawing.Point(34, 43)

Me.Label2.Name = "Label2"

Me.Label2.Size = New System.Drawing.Size(58, 13)

Me.Label2.TabIndex = 16

Me.Label2.Text = "FTP status"

'

'Button3

'

Me.Button3.Location = New System.Drawing.Point(60, 128)

Me.Button3.Name = "Button3"

Me.Button3.Size = New System.Drawing.Size(115, 27)

Me.Button3.TabIndex = 17

Me.Button3.Text = "Listdirectories"

Me.Button3.UseVisualStyleBackColor = True

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(62, 176)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(112, 29)

Me.Button1.TabIndex = 18

Me.Button1.Text = "Upload picaxe data"

Me.Button1.UseVisualStyleBackColor = True

'

'Label3

'

Me.Label3.AutoSize = True

Me.Label3.Location = New System.Drawing.Point(9, 72)

Me.Label3.Name = "Label3"

Me.Label3.Size = New System.Drawing.Size(462, 13)

Me.Label3.TabIndex = 19

Me.Label3.Text = "To do - read back the messagexxx.txt file from ftp, clean up old message.txt file" & _

"s on the ftp server"

'

'Form1

'

Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)

Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

Me.ClientSize = New System.Drawing.Size(478, 295)

Me.Controls.Add(Me.Label3)

Me.Controls.Add(Me.Button1)

Me.Controls.Add(Me.Button3)

Me.Controls.Add(Me.Label2)

Me.Controls.Add(Me.PictureBox2)

Me.Controls.Add(Me.PictureBox1)

Me.Controls.Add(Me.Label1)

Me.Name = "Form1"

Me.Text = "Picaxe Interface"

CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()

CType(Me.PictureBox2, System.ComponentModel.ISupportInitialize).EndInit()

Me.ResumeLayout(False)

Me.PerformLayout()

End Sub

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox

Friend WithEvents Timer1 As System.Windows.Forms.Timer

Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox

Friend WithEvents Label2 As System.Windows.Forms.Label

Friend WithEvents Button3 As System.Windows.Forms.Button

Friend WithEvents Button1 As System.Windows.Forms.Button

Friend WithEvents Label3 As System.Windows.Forms.Label

End Class

********************* Picaxe code ******************************

' physical leg 2 is grounded
' pin3 (physical leg 4) = the data input
' pin0 (physical leg 7) = the data output
' data protocols
' address of device is 255.255.255.255 eg a.b.c.d where a and b are device numbers, c is the local bus and d is the device number
symbol device0 = 1' device 1.1.1.1
symbol device1 = 1
symbol device2 = 1
symbol device3 = 1


main:serin 3,N2400,("Data"),b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13
    ' b0.b1.b2.b3 is the device this message is for - can add code to ignore other messages if using multiple devices on a bus
    ' b4.b5.b6.b7 is the device the message came from
    ' b8 to b13 are the data bytes
    b0=1' send to device 1.2.1.1
    b1=2
    b2=1
    b3=1
    b4=device0' from me
    b5=device1
    b6=device2
    b7=device3
    readadc 2,b8' read a pot
    serout 0,N2400,("Data", b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13)
goto main