Writing the full article with all the photos will take a few more days but in the meantime, here is a picaxe program and a vb.net program which are working fine. The vb.net program has one rich text box, one text box, one picture and 4 labels on the form. I do have all this working but I need the results of my own experiments before I post the full article (my experiment is to work out the capacity of a rechargeable battery, as I have a sneaking suspicion based on two tests so far that the ones made in china marked 2600mAH are actually 450mAH batteries.) I know what Dippy is going to say to that...
main:serin 3,N2400,("Data"),b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13
readadc10 1,w0' read the analog value on pin1 (physical leg 6)
readadc10 2,w1
serout 0,N2400,("Data", b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13)
goto main
and .net
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 ModifyFlag As Boolean
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 ' every 10 seconds
PictureBox1.BackColor = Color.Gray ' start with the comms boxes gray
ModifyFlag = False ' if modify a value manually then skip download
RichTextBox1.Multiline = True ' so can display more than one line
Label1.Text = "Picaxe communications; red=not working, green=ok"
Label2.Text = "Location of data file"
Label3.Text = "Date and time"
Label4.Text = "Picaxe registers"
TextBox3.Text = "c:\Datafile.csv" 'name and location of data file
Call DisplayPicaxeRegisters() ' display the 14 registers
End Sub
Sub SerialTxRx()
Dim DataPacket(0 To 17) As Byte ' entire data packet "Data"+14 bytes
Dim i As Integer ' i is always useful for loops etc
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
PicaxeRegisters(i - 4) = DataPacket(i) ' move the new data packet into the register array
Next
PictureBox1.BackColor = Color.GreenYellow ' working
Catch ex As Exception
PictureBox1.BackColor = Color.Red ' not working
End Try
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Call SerialTxRx() ' send to the picaxe and read it back
System.Windows.Forms.Application.DoEvents() ' so windows doesn't hang
Call DisplayPicaxeRegisters() ' refresh registers on the screen
Call SaveRegistersToLocalFile() ' save numbers to file
Label3.Text = Now ' refresh date and time on screen
End Sub
Sub DisplayPicaxeRegisters()
Dim i As Integer
Dim registernumber As String
RichTextBox1.Multiline = True ' so can display more than one line in the text box
RichTextBox1.Clear() ' clear the text box
For i = 0 To 13
registernumber = Trim(Str(i)) ' trim off leading spaces
If i < 10 Then
registernumber = "0" + registernumber ' add 0 to numbers under 10
End If
RichTextBox1.AppendText(registernumber + " = " + Str(PicaxeRegisters(i)) + Chr(13))
Next ' chr(13) is carriage return so new line
End Sub
Sub SaveRegistersToLocalFile() ' save register array in a local text file
' use a file with extension .csv for excel (comma seperated)
Dim i As Integer
Dim DateTime As String
Dim LineOfText As String
FileOpen(1, TextBox3.Text, OpenMode.Append) ' open the text file named in the text box
For i = 0 To 13
LineOfText = LineOfText + Str(PicaxeRegisters(i)) + ","
Next
DateTime = Now
LineOfText = LineOfText + DateTime ' add the date
PrintLine(1, LineOfText)
FileClose(1) ' close the file
End Sub
End Class