Menu Horisontal

Sabtu, 16 Maret 2013

Cara Membaca & Menulis INI File Vb6


Sumber : http://arifsoftgames.blogspot.com/2013/02/cara-membaca-menulis-ini-file-vb6.html

Pasti ada orang bertanya-tanya bagaimana menggunakan file INI untuk VB6???, nah Pada kesempatan ini saya akan memberikan cara untuk membaca dan menulis INI File pada VB6. Semoga membantu :)





Bahan - Bahan :
1. Visual Basic 6, kalo belom punya pake yang portable gak apa-apa. Download disini | Password : arifsoftgames
2. Kesabaran

Caranya :
1. Tambahkan 3 TextBox, 1 Timer dan 1 Command Button
Saya Menyusun Seperti ini.
2. Buat 2 Module
Module 1 kasih nama : Read
Module 2 kasih nama : Write
Klik kanan, pilih Add lalu klik Module
Sekarang Tahap Codingnya :
Klik 2x pada daerah form 1 dan masukkan code ini :
'Coding Form
Private Sub Command1_Click()
End
End Sub

Private Sub Form_Load()
Text1.Text = ReadIniValue(App.Path & "\INFO.ini", "Default", "Text1")
    Text2.Text = ReadIniValue(App.Path & "\INFO.ini", "Default", "Text2")
    Text3.Text = ReadIniValue(App.Path & "\INFO.ini", "Default", "Text3")
    Timer1.Enabled = True
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = 0 Then Cancel = 1
End Sub

Private Sub Timer1_Timer()
WriteIniValue App.Path & "\INFO.ini", "Default", "Text1", Text1.Text
    WriteIniValue App.Path & "\INFO.ini", "Default", "Text2", Text2.Text
    WriteIniValue App.Path & "\INFO.ini", "Default", "Text3", Text3.Text
End Sub

Pada Module 1 / Read masukkan Code ini
'Fungsi Module Read
Option Explicit

Public Function ReadIniValue(INIpath As String, KEY As String, Variable As String) As String
Dim NF As Integer
Dim Temp As String
Dim LcaseTemp As String
Dim ReadyToRead As Boolean
    
AssignVariables:
        NF = FreeFile
        ReadIniValue = ""
        KEY = "[" & LCase$(KEY) & "]"
        Variable = LCase$(Variable)
    
EnsureFileExists:
    Open INIpath For Binary As NF
    Close NF
    SetAttr INIpath, vbArchive
    
LoadFile:
    Open INIpath For Input As NF
    While Not EOF(NF)
    Line Input #NF, Temp
    LcaseTemp = LCase$(Temp)
    If InStr(LcaseTemp, "[") <> 0 Then ReadyToRead = False
    If LcaseTemp = KEY Then ReadyToRead = True
    If InStr(LcaseTemp, "[") = 0 And ReadyToRead = True Then
        If InStr(LcaseTemp, Variable & "=") = 1 Then
            ReadIniValue = Mid$(Temp, 1 + Len(Variable & "="))
            Close NF: Exit Function
            End If
        End If
    Wend
    Close NF
End Function

Pada Module 2 / Write masukkan Code ini :
'Fungsi Module Write
Option Explicit

Public Function WriteIniValue(INIpath As String, PutKey As String, PutVariable As String, PutValue As String)
Dim Temp As String
Dim LcaseTemp As String
Dim ReadKey As String
Dim ReadVariable As String
Dim LOKEY As Integer
Dim HIKEY As Integer
Dim KEYLEN As Integer
Dim VAR As Integer
Dim VARENDOFLINE As Integer
Dim NF As Integer
Dim X As Integer

AssignVariables:
    NF = FreeFile
    ReadKey = vbCrLf & "[" & LCase$(PutKey) & "]" & Chr$(13)
    KEYLEN = Len(ReadKey)
    ReadVariable = Chr$(10) & LCase$(PutVariable) & "="
        
EnsureFileExists:
    Open INIpath For Binary As NF
    Close NF
    SetAttr INIpath, vbArchive
    
LoadFile:
    Open INIpath For Input As NF
    Temp = Input$(LOF(NF), NF)
    Temp = vbCrLf & Temp & "[]"
    Close NF
    LcaseTemp = LCase$(Temp)
    
LogicMenu:
    LOKEY = InStr(LcaseTemp, ReadKey)
    If LOKEY = 0 Then GoTo AddKey:
    HIKEY = InStr(LOKEY + KEYLEN, LcaseTemp, "[")
    VAR = InStr(LOKEY, LcaseTemp, ReadVariable)
    If VAR > HIKEY Or VAR < LOKEY Then GoTo AddVariable:
    GoTo RenewVariable:
    
AddKey:
        Temp = Left$(Temp, Len(Temp) - 2)
        Temp = Temp & vbCrLf & vbCrLf & "[" & PutKey & "]" & vbCrLf & PutVariable & "=" & PutValue
        GoTo TrimFinalString:
        
AddVariable:
        Temp = Left$(Temp, Len(Temp) - 2)
        Temp = Left$(Temp, LOKEY + KEYLEN) & PutVariable & "=" & PutValue & vbCrLf & Mid$(Temp, LOKEY + KEYLEN + 1)
        GoTo TrimFinalString:
        
RenewVariable:
        Temp = Left$(Temp, Len(Temp) - 2)
        VARENDOFLINE = InStr(VAR, Temp, Chr$(13))
        Temp = Left$(Temp, VAR) & PutVariable & "=" & PutValue & Mid$(Temp, VARENDOFLINE)
        GoTo TrimFinalString:

TrimFinalString:
        Temp = Mid$(Temp, 2)
        Do Until InStr(Temp, vbCrLf & vbCrLf & vbCrLf) = 0
        Temp = Replace(Temp, vbCrLf & vbCrLf & vbCrLf, vbCrLf & vbCrLf)
        Loop
    
        Do Until Right$(Temp, 1) > Chr$(13)
        Temp = Left$(Temp, Len(Temp) - 1)
        Loop
    
        Do Until Left$(Temp, 1) > Chr$(13)
        Temp = Mid$(Temp, 2)
        Loop
    
OutputAmendedINIFile:
        Open INIpath For Output As NF
        Print #NF, Temp
        Close NF
    
End Function
' Selesai

Nah sekarang kita tinggal compile exe aja caranya :
Masuk menu file > Make EXE file
Make EXE file
Sekedar info aja, nanti file *.ini bernama INFO.ini

Membaca File Text VB6

Cara membaca file Text adalah dengan menggunakan fungsi berikut :

Versi 1
Private Function OpenTextFile() As String 
    Dim nFileNum As Integer, sText As String 
    Dim sNextLine As String, lLineCount As Long 
    nFileNum = FreeFile 
    Open "C:\daftar_driver.txt" For Input As nFileNum 
        lLineCount = 1 
        Do While Not EOF(nFileNum) 
            Line Input #nFileNum, sNextLine 
            MsgBox sNextLine 'ini akan membaca file text baris per baris 
            sNextLine = sNextLine & vbCrLf 
            sText = sText & sNextLine 
        Loop 
        OpenTextFile = sText 
    Close nFileNum 
End Function 


Versi 2
1. Menulis Ke File Text  
  Dim LocTextFile As String     LocTextFile = “C:\windows\SvAdd.txt”         Open LocTextFile For Output As #1   ‘Buka file text        Print #1, “Server_Name”    ‘Modify Baris Pertama”        Print #1, “Database_Name”  ‘Modify Baris Kedua        Print #1, “User”           ‘Modify Baris Ketiga        Print #1, “Pwd”            ‘Modify Baris Keempat     Close #1
2. Membaca dari File Text
    Dim LocTextFile As String     Dim ServerAddress  As String     Dim DatabaseName As String     Dim UserID As String     Dim PwdSql As String     LocTextFile = “C:\windows\SvAdd.txt”     Open LocTextFile For Input As #1    ‘Buka file text        Line Input #1, ServerAddress      ‘Baca Baris Pertama        Line Input #1, DatabaseName       ‘Baca Baris Kedua        Line Input #1, UserID             ‘Baca Baris Ketiga        Line Input #1, PwdSql             ‘Baca Baris Keempat     Close #1 ‘Tutup File file text
Versi 3
Ini Fungsinya
Function ReadTextFileContents(filename As String) As String
    Dim fnum As Integer, isOpen As Boolean
    On Error GoTo Error_Handler
    fnum = FreeFile()
    Open filename For Input As #fnum
    isOpen = True
    ReadTextFileContents = Input(LOF(fnum), fnum)
    
Error_Handler:
    If isOpen Then Close #fnum
    If Err Then Err.Raise Err.Number, , Err.Description
End Function

Ini Cara Pakainya
TxtPesan.Text = ReadTextFileContents(App.Path & "\service.log")



Kamis, 07 Maret 2013

How to Communicate to the Arduino in Visual Basic .NET

Sumber : http://social.technet.microsoft.com/wiki/contents/articles/14194.how-to-communicate-to-the-arduino-in-visual-basic-net.aspx

Communicating to the Arduino in the .NET platform is pretty much straightforward: The Arduino uses a virtual serial port to allow programs to be written onto it, but we can also use this port to get and send data to and from the Arduino.
Materials:

  • Arduino or Arduino Compatible Board (UNO, Mega, Etc.)
  • Compatible USB Cable to Connect the Arduino to the Host Computer
  • The Arduino Software (Available Here )
  • VB.NET 2010 or 2012 (Available Here )
I am assuming that you already have a simple knowledge of the .NET and Arduino (software & hardware) platforms. If not, there are many tutorials on the internet to help get you started. Let's begin by putting the program on the Arduino.


Step 1: Adding a Program to Send Data From the Arduino
Let's start by opening up the Arduino software, and copying/pasting in the following code:

void setup() {              
    Serial.begin(9600); 
}
 
//Sends the Number 1234 Over the Serial Port Once Every Second
void loop() {
Serial.write(1234)
delay(1000)
}

This program opens up a serial port, and sends the number string, 1234, over the port once every second in an infinite loop. The number '1234' has no special meaning, you could place any number there in place of it. Then plug in your Arduino board and upload the code to it. Leave it plugged in.

Step 2: Creating the .NET Program
Creating the .NET code in VB.NET is actually quite simple and easy to understand. Open a new VB.NET console program, clear all of the code, and paste in the following:

Module Module1
    Sub Main()
        Dim userRespond
        Dim fetchedData
        Dim writeString
        Console.WriteLine("Type R to Read Serial Port.")
        userRespond = Console.ReadLine
        If userRespond = "R" Or userRespond = "r" Then
            Dim port As New System.IO.Ports.SerialPort
            port.PortName = "COM4"
            port.Open()
            port.BaudRate = 9600
            fetchedData = port.ReadByte
            Console.WriteLine(fetchedData)
            Console.ReadLine()
  Else
            Console.WriteLine("Not a Recognized Command.")
        End If
    End Sub
End Module

This code should be fairly simple. It opens a new console window, then it asks the user if they want to get the data from the port, next (if they said yes), it opens up a new COM port and gets the data, finally it writes it to the console. You may need to change COM4 to the correct port for your Arduino. It lists what port Arduino is on in the Arduino software bottom right corner. You can not write a new program to the Arduino when this software is running, for it gives an error that says that the COM port is busy.

Step 3: Testing
To use this program, be sure your Arduino is plugged in, and it is running the code from step 1. Now, run the program from above and type in R. It may take a second (the Arduino only sends it once per second), but the number 1234 will come up! If you change the number 1234 in the Arduino code to "hello" and upload it and run the .NET program, it will only give you the decimal value for a lower-case h. This is because this program can only read one byte at a time. See if you can change it up to read the whole word and convert it to unicode! Post your inventions!