Home Parallel Port Serial Port Serial Info Parallel Info USB .NET Webboard I/O Today API Project Directory

 Hardware Interfacing and TCP/IP interfacing in VB programming.

Programming in Visual Basic to allow data transfer via a serial port
Thai Page Click Here
  In this section, we will learn about programming in Visual Basic to allow data transfer via a serial port, by transfer via Com Port.
Visual Basic(VB) Programming in RS-232,Serial Port,Com Port Interfacing for HardwareS systems. 
First of all, we will make understanding about approaching data in Visual Basic, approaching data file in the three ways;
        

  1. Sequential File
  2. File in which the records are arranged in order of a key field (First In First Out), providing a straightforward way to read and write files, although there are the different lengths of data. For disadvantage of this, you must access one line at a time, starting with the first line, so it isnít suitable for lot of data.

  3. Random File
  4. Random files are a better solution to database problems than sequential files , access faster and donít be arranged in order of a key field, consisting of records that can be accessed in any sequence, but Random files is more complex than sequential File.

  5. Binary File
  6. It can be transferred completely because Binary files typically contain bytes that are intended to be interpreted as something other than text characters.

             The writer will explain Binary File as the main content of this article because we will transfer data through serial port, also can also contain images, sounds, compressed versions of other files, etc.

  Start by open file
Open pathname For mode [Access access] [lock] As [#]filenumber [Len=reclength]

Pathname is just a filename used.

Mode is a reserved word for choosing the files, Append, Binary, Input, Output, or Random. If it isnít be set, its default will be set for Random mode.

Access is a reserved word for getting data, Read, Write, or Read Write.
Lock is a reserved word that is used for set dataís qualification, Shared, Lock Read, Lock Write, and Lock Read Write.

Filenumber is the number of file that is opened (between 1-511), also, we can find free file number by FreeFile

Reclength is the length of recording data, will be set to 32,767 bytes

Example;
Open "TESTFILE" For Input As #1
Open "TESTFILE" For Binary Access Write As #1
Open "TESTFILE" For Output Shared As #1

Usually, operated by open, if you donít read and write, you must close the data file by using Close #filenumber Close #1
 


 Reading and writing data file.  


Get statement is used for reading data from file opened in order to keep in variable,

Get [#]filenumber, [recnumber], varname

Put statement is used for reading from variable to file opened,

Put [#]filenumber, [recnumber], varname

Filenumber is number of file that is currently opened in program.

Recnumber is the number of the record at which to start reading in Random mode or the byte number (Binary mode files) at which reading and writing begins.

Varname is variable for recording data read from files and verify status of the files.

Seek statement set the position in a file for the next input or output operation, it will recovery the current position in reading and writing the files opened from 1 to 2,147,483,647, in the following;

Seek(filenumber)

LOF statement is used for verifying data length in the files opened in the following;
LOF(filenumber) is used for verifying the status of data that is end-of-file. Returns a Boolean value True when the end of a file opened, but returns False when it isnít the end of a file like in the following;

EOF(filenumber)

Filenumber is number of file that is currently opened in program.

 

 

   Programming through port for transferring data   

   Transferring data file  
    1. Set variable
    2. Find the free file number to work by using FreeFile statement and keep in noSend variable.
    3. Open binary file in order to read data.
    4. Read data from file opened with the amount of data is equal to OutBufferSize in MSComm1 that is Buffer for transferring through port.
        ē When Block has been already read, it will send by statement MSComm1.Output
        ē Wait until all data is completely send from Buffer by verifying by MSComm1.OutBufferCount
    5. Open data file.

The example of program

Dim numSend, buffSize, chkLF&
numSend = FreeFile
Open ďc:\ test.datĒ For Binary Access Read As numSend
If Err Then
        MsgBox Error$, 48
        Else
        buffSize = MSComm1.OutBufferSize
        chkLF& = LOF(hSend)
        Do Until EOF(numSend) Or CancelSend
        If chkLF& - Loc(numSend) <= buffSize Then
                buffSize = chkLF& - Loc(munSend) + 1
        End If
        Temp = Space$(BSize)
        Get numSend, , Temp
        MSComm1.Output = Temp
        If Err Then
                MsgBox Error$, 48
        Exit Do
        End If
        Do
                DoEvents()
                Loop Until MSComm1.OutBufferCount = 0 Or CancelSend
        Loop
End If
Close numSend

     Getting data file   

1. Find the file that has space and open that we want to storage data that is got.
2. Verify error while opening.
3. Use seek statement to find the current data in order to add data of file opened to get.

This statement is in procedure like Menu or Command Button.
The example of program
using variable numRev at head of program to keep the value of FreeFile

Private Sub Command1_Click()
numRev = FreeFile
Open ďD:\ test.datĒ For Binary Access Write As numRev
   If Err Then
       MsgBox Error$, 48
       Close numRev
       numRev = 0
       Exit Sub
       Else
       Seek numRev, LOF(numRev) + 1
   End If
End Sub

For getting data file sent, we must write program in event of MSComm when get data that use event, named comEvReceive in procedure.
Sub MSComm1_OnComm () we will use Select Case to verify when Events of getting data is occurred. This statement is used in procedure MSComm1_OnComm()

Private Static Sub MSComm1_OnComm()
    Select Case MSComm1.CommEvent
    Case comEvReceive
    Dim buffRev As Variant
    buffRev = MSComm1.Input
        If numRev Then
            i = 3
            Do
                 Err = 0
                

                 Put numRev, , bffRev
                 If Err Then
                         i = MsgBox(Error$, 21)
                         If i = 1 Then
                                Close numRev

                         End If
                 End If
            Loop While i <> 3
        End If
End Select

  Learning outcome of this article

1. To know how to program by using Visual Basic to control working of I/O File
2. To know how to apply File to help you for programming due to you canít directly program to control Visual Basic.
3. To know how to program by using API from using DllPort.Dll file.
4. To know how to use Comm Port except connecting with other computer.



Copyright (c) 2000-2008 thaiio.com all rights reserved.  Contact webmaster :webmaster@thaiio.com