Public Class Form1
Public DoMeasure = 0
Public ReRead = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With SerialPort1
.PortName = "COM6"
.BaudRate = 9600
.DataBits = 8
.Parity = IO.Ports.Parity.None
.StopBits = IO.Ports.StopBits.One
.DtrEnable = False
.Encoding
= System.Text.Encoding.GetEncoding("ASCII")
End With
With Timer1
.Interval
= 60000 '1000 = 1sec 60000=1min
.Enabled = True
End With
With Timer2
.Interval = 200
.Enabled = True
End With
If SerialPort1.IsOpen Then
Label5.Text = "接続中"
Else
Label5.Text = "切断中"
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.PortName = Label2.Text
Try
SerialPort1.Open()
Catch ex As Exception
MessageBox.Show(ex.Message,
"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
If SerialPort1.IsOpen Then
Label5.Text = "接続中"
Else
Label5.Text = "切断中"
End If
End Sub
Delegate Sub DataDelegate(ByVal Sdata As String)
Private Sub PrintData(ByVal Sdata As String)
TextBox4.Text = Sdata
End Sub
Private
Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles
SerialPort1.DataReceived
Dim i As Integer
Dim FilePath As String
Dim WriteData As String
Dim TextFile As System.IO.StreamWriter
FilePath = "C:\data\new.txt"
TextFile = My.Computer.FileSystem.OpenTextFileWriter(FilePath, True)
Dim ReadData As String
Dim LengthofReadData As Integer
Dim ReadInt(6) As Integer
Dim DelegatetedData As New DataDelegate(AddressOf PrintData)
Try
ReadData = SerialPort1.ReadLine
Catch ex As Exception
ReadData = "9999,8888,7777,6666,5555,4444,"
End Try
LengthofReadData = Len(ReadData)
If LengthofReadData < 30 Then
TextFile.Close()
ReRead = 1
Else
For i = 1 To 6
If
IsNumeric(Mid(ReadData, (i - 1) * 5 + 1, 4)) Then
ReadInt(i)
= CInt(Mid(ReadData, (i - 1) * 5 + 1, 4))
Else
ReadInt(i)
= 9999 + i
End If
Next i
If
ReadInt(1) <> ReadInt(4) Or ReadInt(2) <>
ReadInt(5) Or ReadInt(3) <> ReadInt(6) Then
TextFile.Close()
ReRead
= 1
Else
WriteData
= Date.Now.ToString() & "," & ReadInt(1) & ","
& ReadInt(2) & "," & ReadInt(3)
Invoke(DelegatetedData,
WriteData)
TextFile.WriteLine(WriteData)
TextFile.Close()
End If
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
If SerialPort1.IsOpen And DoMeasure = 1 Then
SerialPort1.Write("S")
End If
Catch ex As Exception
MessageBox.Show(ex.Message,
"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Try
If ReRead = 1 Then
ReRead
= 0
SerialPort1.Write("r")
End If
Catch ex As Exception
MessageBox.Show(ex.Message,
"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
DoMeasure = 1
Label7.Text = "測定中"
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
DoMeasure = 0
Label7.Text = "停止中"
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If SerialPort1.IsOpen Then
MessageBox.Show("
設定は接続前に行ってください", "注意", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
SerialPort1.PortName = TextBox1.Text
Timer1.Interval = Integer.Parse(TextBox3.Text)
Label2.Text = TextBox1.Text
Label6.Text = TextBox3.Text
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Application.Exit()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
SerialPort1.Close()
Label5.Text = "切断中"
End Sub
End Class
PICのソースコードです。
/*
* File: newmain.c
* Author:
*
* Created on 2014/07/27, 19:56
*/
#include <stdio.h>
#include <stdlib.h>
#include <pic.h> //delayを使うのに必要
#include <pic16lf1827.h> //レジスタの定義に必要
#include <usart_pic16.h> //USARTを使うのに必要 ※USARTinitをクロックに合わせて変更すること
#define _XTAL_FREQ 16000000 // delay用に必要(クロック16MHzを指定)
int main(int argc, char** argv) {
int voltH = 0;
int voltL = 0;
int volt = 0;
int register1 = 0;
int register2 = 0;
int voltMAX = 0;
int i = 0;
unsigned char RS232Standby;
unsigned char RCVdata;
unsigned char strout[29];
OSCCON = 0b01111011; // 16MHz Internal Clock
PORTA = 0x00; // All PORT Clear
ANSELA = 0b00010110; // AN1,AN2,AN4
ANSELB = 0x00;
TRISA = 0b00010110; // AN1,AN2,AN4
ADCS2=1;ADCS1=0;ADCS0=1; //ADC Clock Source Fosc/16 => 1usec at 16MHz
//
11.5usec + acq4.5usec = 16usec
ADFM = 1; //Result Format : Right Justified
FVREN = 1;ADFVR1 = 1;ADFVR0 = 1; // FVR enable FVR=4.096V
ADPREF1 = 1;ADPREF0 = 1; // use FVR
ADON = 1; // ADC ON
USARTInit(9600);
while(1){
RS232Standby = USARTDataAvailable();
if( RS232Standby != 0)
{
RCVdata
= USARTReadData();
if(
RCVdata == 'S' )
{
//
//
Read Voltage
//
voltMAX
= 0;
for(i
= 1;i <= 1250;i++) // 16usec x 1250 = 0.02sec = 1/50
{
//
select AN2
CHS4
= 0;CHS3 = 0;CHS2 = 0;CHS1 = 1;CHS0 = 0;
__delay_us(5);
// Acquisition Time
GO
= 1;
while(GO){}
voltH
= ADRESH;
voltL
= ADRESL;
volt
= voltH * 256 + voltL;
if(voltMAX
< volt){
voltMAX
= volt;
}
}
//
//
Read Register 1
//
//
select AN1
CHS4
= 0;CHS3 = 0;CHS2 = 0;CHS1 = 0;CHS0 = 1;
__delay_us(5);
// Acquisition Time
GO
= 1;
while(GO){}
voltH
= ADRESH;
voltL
= ADRESL;
register1
= voltH * 256 + voltL;
//
//
Read Register 2
//
//
select AN4
CHS4
= 0;CHS3 = 0;CHS2 = 1;CHS1 = 0;CHS0 = 0;
__delay_us(5);
// Acquisition TIme
GO
= 1;
while(GO){}
voltH
= ADRESH;
voltL
= ADRESL;
register2
= voltH * 256 + voltL;
//
//
make strout[]
//
strout[0]
= voltMAX / 1000 + 48;
strout[1]
= (voltMAX % 1000) / 100 + 48;
strout[2]
= (voltMAX % 100) / 10 + 48;
strout[3]
= voltMAX % 10 + 48;
strout[4]
= ',';
strout[5]
= register1 / 1000 + 48;
strout[6]
= (register1 % 1000) / 100 + 48;
strout[7]
= (register1 % 100) / 10 + 48;
strout[8]
= register1 % 10 + 48;
strout[9]
= ',';
strout[10]
= register2 / 1000 + 48;
strout[11]
= (register2 % 1000) / 100 + 48;
strout[12]
= (register2 % 100) / 10 + 48;
strout[13]
= register2 % 10 + 48;
strout[14]
= ',';
for(i=1;i<16;i++)
{
strout[14+i]=strout[i-1]
;
}
//
//
write to serial IO
//
USARTWriteLine(strout);
}else{
USARTWriteLine(strout);
}
}
}
return (EXIT_SUCCESS);
}