Final year degree project for engineers Final year projects based on microcontrollers
Microcontroller based different projects abstract and source code
AT89s51 microcontroller projects Motion control
at89c2051 motor controller and other light projects
Tutorials of microcontroller 8051, A step by step easy to use self learning rapidex

Wednesday, March 3, 2010

VB6 RS-232 serial communication example program

Many student asked for VB6 program for serial communication.
This is a short lesson or tutorial on the development of your own application program for computer to send and recive data from microcontroller (any, the microcontroller side program is not discussed here).
What you need is written in steps;
Step # 1:
You need a computer with visual basic 6 plat form installed on it.
Open the VB6 application from programs to write a code.
You will notice following message on screen, choose Standard exe and press OK,( This is default)
STEP#2:
Here a form is open, you will need to add a component required for serial communication. Normally it is not visible in tools and we have to bring it in tools from components. This VB6 library built in.
1. right click on the bar of tools available for you to use on left side of the application and press add components. OR
2. Press control+T to open the component window OR
3. Go to project menu and click on components
By using either way , you will open the component window as shown below.
Now Find "Microsoft comm control 6" and check the option and press "OK"

STEP#3
Place the texts boxes and command buttons by drag and drop method from tool bar. Resize them. and make the form as shown in the figure below.

STEP#4
copy the following code and paste it in the code section and compile , run it.
You can receive the byte stream of any length. the incoming string will be shown in text1. Then i have use Mid function to split the string to acquire one byte and then shown in the other text boxes.
In this program i have send only one byte , but you can send as many as you wish.
The com setting is 9600,N,8,1 mean baud rate is 9600, the byte will of 8 bits and one stopping bit and now parity bit is used ion this communication. Make you micro controller set accordingly.
Enjoy it, if you want to ask any questions , please write in comments. I will check and answer them.
Option Explicit
Dim strInBuff as String ' The incoming data from the serial port will be stored in this string
Dim temp1 as Integer ' The only one parameter in this program you will send out from PC
Private Sub Form_Load()
With MSComm1
.CommPort = 1
.RThreshold = 1
.RTSEnable = True
.Settings = "9600,N,8,1"
.InputLen = 127
.SThreshold = 1
' .PortOpen = True
End With
temp1 = 100 ' You can declar and use as many varaible whose value is required to send from ' 'PC but here as only one is required , so i have declared only one.
End Sub
'When some data is recived in the serial port of the PC (computer) the following function will be excecuted
Private Sub MSComm1_OnComm()
' Different events are occured , when data come , these includes some errors also (if they occur in communication, but normally RS-232 have not problems)
Select Case MSComm1.CommEvent

' Errors
Case comEventBreak
Case comEventCDTO
Case comEventCTSTO
Case comEventDSRTO
Case comEventFrame
Case comEventOverrun
Case comEventRxOver
Case comEventRxParity
Case comEventTxFull
Case comEventDCB

' Events
Case comEvCD
Case comEvCTS
Case comEvDSR
Case comEvRing
Case comEvReceive
' The data (incoming from outside world to PC through RS-232 port is stored in
''MSComm1.Input , which is a string, each time we read from this
strInBuff = MSComm1.Input
' after reading it requires free from any data, otherwise all the data will be gathhered and it will
'overflow, result in runtime error
MSComm1.Input = ""
Text1.text = strInBuff ' what ever we get is displayed in a text
Text2.text = Asc(Mid(strInBuff , 1, 1)) ' getting one byte and speratly displaying in other
'text for other possible use, for this MID function is used
Text3.text = Asc(Mid(strInBuff , 2, 1))
Case comEvSend
Case comEvEOF

End Select

End Sub
' if you wish to send a byte to microcontroller from pc, then use following code, and in the same
'way using loop or other methods you can send more bytes, if u need??
Private Sub Command3_Click()
MSComm1.OutBufferCount = 0
MSComm1.Output = Chr$(temp1)
End Sub
' opening the com port for communication with microcontroller or other world
Private Sub Command1_Click()
If (MSComm1.PortOpen = False) Then
MSComm1.PortOpen = True
End If
End Sub
' closing the com port if its no need for further communication
Private Sub Command2_Click()
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
End Sub
' at the form closing or exit or unloading, again port is rechecked, if it is open then it will close 'now, for the use of any other program
' This important, and many user or programmer forget this point. otherwise you will need to 'restart the computer for again use of this serial port even in this program or any other program.
Private Sub Form_Unload(Cancel As Integer)
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
End Sub

other post on the topic of serial communication are given below.Students can look on them for further informaion.

1. Programmable Frequency meter
This project is for measuring frequency of a digital signal. The data is transferred to PC. C51 Code for serial communication is given in this post. 
2. Serial communication with PC Using HyperTerminal
Serial communication of micro-controller with PC Using Hyper Terminal.
In Windows-95, win-98, window-Me, win-NT, and windows 2K & XP have Hyper-Terminal built in program. The prime object of HyperTerminal is have serial communication either through RS-232 port or modem.
3. Display and Serial Interfacing of automatic car parking project 
The RS232 circuit diagram is provided.
4. Serial Interfacing of Microcontroller 8051 in Automatic Car Parking Project
The micro-controller is interfaced to the PC by serial port through ICL232 logic level converter. An IBM compatible computer and 89C51 microcontroller interface is used in RS-232 serial communication.
5. Introduction of serial Port RS-232--part1-4
Software writting procedures for serial port communication This is consisting of four post about the RS232 serial communication.
6. Serial Communication between microcontroller and PC
 Serial CommunicationThe Serial Port is harder to interface than the Parallel Port. In most cases, any device you connect to the serial port will need the serial transmission converted back to parallel so that it can be used. This can be done using a UART. On the software side of things, there are many more registers that you have to attend to than on a Standard Parallel Port. (SPP) .So what are the advantages of using serial data transfer rather than parallel?
7. DB9 pin connector for RS232 serial Communication
All communication we have dealt with up to now has been parallel. Data being transferred between one location and another (R0 to the accumulator, for example) travel along the 8-bit data bus. Because of this data bus, data bytes can be moved about the microcontroller at high speed.
8. MAX 232 Interfacing with Microcontroller 8052
MAX232 is used to interface the microcontroller to standard RS-232 port of personal computer. It is a signal level converter necessary for conversion between TTL and RS-232 standards.

 Tags:-example code of sending data from at89s51 to serial port,the data transfer must be by a specific serial channel 8051 Project,Send data to COM port and analyze reaction of the serial device 8051 projects,Serial Port Monitor Software -uart at89c51 Serial Monitor - AT89C51 serial to communication a bar code RS232 Port ADC0804,12 MHz crystal with AT89C51 and want to send and receive data from serial port,The Micro Stop - RS232 and microcontroller schematics and C code ,microcontroller projects, at89c51 projects, 8051 serial port communication,How to send number through rs232 to AT89C51,serial port programming in c,Interfacing Serial Port (RS232) with 8051 microcontroller AT89C51.,Project also describe serial port interfacing circuit and code in C.AT89C51 microcontroller can be set to transfer and receive serial data,serial communication in AT89c51,serial port is ready to transmit a new character,send data correctly into SBUF ,Send data on a serial port ,code to read data from Serial Port of Com port,Sending ASCII Code For At89c51 ,Serial Port Communication With PIC microcontroller,Communicate Through The Serial Port(com1) Communicate VB With External Buttons Using Serial Port Using Existing C Source Code To Communicate Via Serial Port Send Text File To Serial Port To Communicate With A 8051 Embeded Device. Transmit From Serial Comm To Microcontroller Connecting a microcontroller to RS-232 ports and PS/2 devices Serial Reception Of Data From Modem With Atmel89C51 Microcontroller How To Communicate With USB Port.How To Communicate With COM Port? How Do You Communicate Through The IrDA Port? Communicate Directly With PS/2 Or Other Port Communicate With Hradware Port Communicate Directly With PS/2 Or Other Port communicate With Printer Through COM Port How To Make VB Communicate With A Com Port Of Ur Pc Program To Communicate With USB Port Communicate Through Parallel Port How Do I Communicate With A Modem Or With A Com Port? How To Communicate With Parallel Port Using VB.NET? Problem With Reading Data From Com Port(serial Port) Read Data From Serial Port Or COM Port Serial Port Emulator / Virtual Com Port How To Get Data From Serial Port /parallel Port Get Info From Serial Port (mouse Port) Is There Any Controls Of USB Port? Like MSComm To Serial Port. RS232 & Microcontroller Communication 8051 interfacing tutorials with RS232 logic devices with circuit Interfacing Serial Port RS232 with 8051 microcontroller AT89C51,Microcontroller with RS232 and GSM,Basic USB-RS232 Communication with PIC Microcontrollers,Interfacing The Serial / RS-232 Port,RS232 and Microcontroller 80s52, Interfacing a Microcontroller With a PC Using RS232 ,USB-to-RS232 Hurdle Race ,Serial Port PIC Microcontroller Programmer

13 comments:

  1. Graphic LCD interface module with microcontroller 8031 or 89s51 or at89c2051 is required. Please give me c-code for graphic lcd. Sample program in c-language using keil c51 will be helping for us to understand.
    please give some details of .
    How to use USB with microcontroller 8051 family?
    Thanks a lot.

    ReplyDelete
  2. can help me on the project of control of boiler turbine unit by using fuzzy logic.....it is an embedded project in this pic microcontroller is used
    Details

    ReplyDelete
  3. Hello ,
    I have a project going on in college as part of which I have to take an output of adc to the hyperterminal .I went through all the resources on your blog ,but then also when i was unable to get the desired result I am mailing you. I have coded the 8051 to 0804 interfacing programme in keil .I have also included the code for hex to ascii conversion of the 8 bit data taken from adc to 8051 port, to be displayed on hyperterminal using serial communication through rs232. I downloaded the programme to Flash RAM . Then executed it , but getting no display on hyperterminal. Can you please take the pain of testing the code yourself ?
    I would be happy to revert back the favour to best of my ability .Its really urgent as the deadline is 20th march and i am still in this stage.Next I have to send the data to database.
    I desire the data on hyperterminal to be in format " 11110000 11001100 10101010 byte4 byte 5 byte6......." whichcan be straight away sent to consecutive columns in MSaccess database .
    Please find the attached code and circuit diagram for interfacing and help me with this .I would be really grateful to you for the same.

    Thanks,
    Shobhit

    ReplyDelete
  4. See this page online terminal RS-232
    TerminalWeb

    ReplyDelete
  5. hello...actually, i want to collect data from rs232 (RFID) to my VB6..
    when i', touch the rfid card on the reader, it seem like VB can read from comm port but still error, please help me..



    Dim sbj_id() As Integer

    Private Sub cmdexit_Click()
    Unload Me
    End Sub

    Private Sub cmdlogin_Click()
    Dim xRS As New ADODB.Recordset
    Dim jam As String
    Dim minit As Integer
    Dim hari As Variant
    Dim JM As Variant
    Dim MM As Variant
    Dim JK As Variant
    Dim MK As Variant
    Dim X As Boolean
    Dim sqlCmd As String


    jam = Format(Now, "hh")
    minit = Format(Now, "nn")
    hari = WeekdayName(Weekday(Now))


    RS.Open "Select * From Student Where Student.Tagid='" & txtidtag.Text & "'", CONN
    If Not RS.EOF Then
    txtname.Text = RS!Studentname
    Else
    MsgBox "Student not found", vbInformation
    Exit Sub
    End If
    RS.Close


    'to get hari,jam,minit

    X = False

    sqlCmd = "SELECT Student.Tagid, Subject.JM, Subject.Subjectid FROM (Studreg INNER JOIN Student ON Studreg.Tagid=Student.Tagid) "
    sqlCmd = sqlCmd & "INNER JOIN Subject ON Studreg.Subjectid=Subject.Subjectid "
    sqlCmd = sqlCmd & "WHERE ((Subject.JM)<" & jam & ") AND ((Subject.JK)>" & jam & ") AND ((Subject.Day)='" & hari & "') "
    sqlCmd = sqlCmd & "OR (((Subject.JM) = " & jam & ") AND ((Subject.MM) <= " & minit & ")) "
    sqlCmd = sqlCmd & "OR (((Subject.JK) = " & jam & ") AND ((Subject.MK) >= " & minit & ")) "
    RS.Open sqlCmd, CONN

    Do While Not RS.EOF
    If txtidtag.Text = RS!Tagid Then
    c.Text = RS!Subjectid
    Form4.Show vbModal
    X = True
    Exit Do


    End If

    RS.MoveNext
    Loop
    RS.Close



    'Set xRS = CONN.Execute(sqlCmd)

    If X = True Then
    'Save Studreg
    xRS.Open "Select * From Studreport", CONN
    If X = True Then
    sqlCmd = "INSERT INTO Studreport(Tagid,Subjectid,Masa,Tarikh) VALUES('" & txtidtag.Text & "','" & c.Text & "','" & lbltime.Caption _
    & "','" & lbldate.Caption & "')"
    CONN.Execute (sqlCmd)


    Else
    'Access Denied
    End If
    xRS.Close

    Else
    Form5.Show vbModal
    End If


    End Sub

    Private Sub MSComm1_OnComm()
    Private Sub Form_Load()
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.CommPort = 1
    MSComm1.InputLen = 0
    MSComm1.PortOpen = True
    MSComm1.RThreshold = 1
    End Sub


    Private Sub Timer1_Timer()
    lbltime.Caption = Format(Time, "hh:mm:ss")
    lbldate.Caption = Date
    Timer1.Enabled = False

    End Sub

    Private Sub txtidtag_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
    cmdlogin_Click
    End If

    End Sub

    ReplyDelete
  6. Private Sub MSComm1_OnComm()
    Private Sub Form_Load()
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.CommPort = 1
    MSComm1.InputLen = 0
    MSComm1.PortOpen = True
    MSComm1.RThreshold = 1
    End Sub


    Private Sub Timer1_Timer()
    lbltime.Caption = Format(Time, "hh:mm:ss")
    lbldate.Caption = Date
    Timer1.Enabled = False

    End Sub

    Private Sub txtidtag_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
    cmdlogin_Click
    End If

    End Sub

    ReplyDelete
  7. Hi - I am definitely happy to find this. great job!

    ReplyDelete
  8. HELLO.i am final year engg student.plz help me to connect the my hardware to my software interface in VB6. PLZ,GIVE ME A PROGRAM AND OBJECT PROPERTIES SETTING . SEND ME ADESCRIPATION HOW TO BUILT IN VB6
    MY EMAIL- virendra_vgec@yahoo.in

    ReplyDelete
  9. Dear Sir

    I am final year engg student. i have decide the my final year project is Temperature Data equisition system. in this project some problem to interfacing between my hardware and software.i want to made software in vb6 so please give me a program of vb6 and how many object require and what is a properties setting?

    please give me a report of Temperature Data equisiton System.



    regards

    Virendra.p.patel

    ReplyDelete
  10. @Virendra:-
    You asked some question about how to write a program in VB6 for serial communication between computer and some hardware based DAQ say based on microcontroller at89s51.
    from your question it is seemed that you are new in programming, any ways, this project is not so much difficult. you can make it with some basic information.
    for these is suggest you, plz copy the program and do the coding work in vb6 as meantioned in the post. you will learn some important things. if you still need some help from me, i have added you on my yahoo for live chatting to save your time. my id is rghkk1@yahoo.com
    i would be availble in evening times from 6pm(PST, GMT+5) to onward.

    ReplyDelete
  11. Hi in my project i want to use serial port for communication f 2 devices(PC and GSM) ...how can i do t ,bcoz in 8051 oly one serial port s avalible... pls help me ...

    ReplyDelete
  12. HI sir in my project i want to communicate with two serial devices how can i accomplish t using oly one 8051...(to PC and GSM)

    ReplyDelete
  13. kindly provide me the coding for controlling 24 relays in every revolution. my mailid:babulalsha@rediff.com

    ReplyDelete

Please note that my helps are not for free.
So, send me your problems and pay money for solution.ok
This is www.microcontroller51.blogspot.com comments section. Here you are requested to write your questions and problems about the microcontroller and electronics projects. Write the questions or comments in such a way that, it have all necessary information, so that i can understand and reply.If you want to send pictures and codes then e-mail me (rghkk@hotmail.com).Thanks.
All comments will be highly appriciated.

Blogroll

  • 8051 Projects
  • Microcontroller based different projects abstract and source code
    AT89s51 microcontroller projects Motion control
    at89c2051 motor controller and other light projects
    Tutorials of microcontroller 8051, A step by step easy to use self learning rapidex

    Popular Posts

    Microcontroller Forum