Read And Write Values from/to Arduino with Visual Basic 6.0

Hello Friends,
I want to read the values from LM35 connected to pin 0 on arduino in VB 6.0. I have searched for the programs and similar problems but found none as suitable for my concern.
I am able to read the values from Serial Port in Arduino Serial window. ANd thus want to read the values in VB and with a click of “CommandButton” (In VB) is wish to turn on the Led on the pin no 13.
Will you please guide me how to do so?
MY VB code:

some googling gave me

might help ...

There is an ocx control for vb 6 i think is called mscom, i used that a long time ago.

VB 6 is very very old, If you download the free express from MS it's all build in and much simpler to use, but of course there's a little learning to do in moving to the .net version.

You can communicate with your Arduino (or serial communication in general ) using Windows API functions. This is my preferred method because Windows API work with several languages like .Net, VB6, C#, C++, Visual Fortran just to name a few.

Here is the code in VB6:

'This program is based on the following Arduino's source code:

Private Type DCB
        DCBlength As Long
        BaudRate As Long
        fBitFields As Long 'See Comments in Win32API.Txt
        wReserved As Integer
        XonLim As Integer
        XoffLim As Integer
        ByteSize As Byte
        Parity As Byte
        StopBits As Byte
        XonChar As Byte
        XoffChar As Byte
        ErrorChar As Byte
        EofChar As Byte
        EvtChar As Byte
        wReserved1 As Integer 'Reserved; Do Not Use
End Type

        ReadIntervalTimeout As Long
        ReadTotalTimeoutMultiplier As Long
        ReadTotalTimeoutConstant As Long
        WriteTotalTimeoutMultiplier As Long
        WriteTotalTimeoutConstant As Long
End Type

        Internal As Long
        InternalHigh As Long
        offset As Long
        OffsetHigh As Long
        hEvent As Long
End Type

Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Const OPEN_EXISTING As Long = 3
Private Const INVALID_HANDLE_VALUE As Long = -1
Private Const FORMAT_MESSAGE_FROM_SYSTEM As Long = &H1000
Private Const ONESTOPBIT As Byte = 0
Private Const NOPARITY As Byte = 0
Private Const MAXDWORD As Long = &HFFFF

Dim hPort As Long 'Handle to the port
Dim hPortReady As Boolean 'Is port ready for Rx & Tx?

Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _
                                                                        ByVal dwDesiredAccess As Long, _
                                                                        ByVal dwShareMode As Long, _
                                                                        lpSecurityAttributes As Any, _
                                                                        ByVal dwCreationDisposition As Long, _
                                                                        ByVal dwFlagsAndAttributes As Long, _
                                                                        ByVal hTemplateFile As Long) As Long

Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" ( _
                                                                               ByVal dwFlags As Long, _
                                                                               lpSource As Any, _
                                                                               ByVal dwMessageId As Long, _
                                                                               ByVal dwLanguageId As Long, _
                                                                               ByVal lpBuffer As String, _
                                                                               ByVal nSize As Long, _
                                                                               Arguments As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long
Private Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As OVERLAPPED) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Function Write_Port(ThisByte As Byte) As Long
    Dim rtLng As Long, BytesWritten As Long, dOverlapped As OVERLAPPED
    With dOverlapped 'Send it as null
        .Internal = 0
        .InternalHigh = 0
        .offset = 0
        .OffsetHigh = 0
        .hEvent = 0
    End With

    Write_Port = WriteFile(hPort, ThisByte, 1, BytesWritten, dOverlapped)
    If BytesWritten <> 1 Then Write_Port = -1
End Function
Private Function Prepare_Port(wMsg As Boolean) As Boolean
    Dim PortNumber As String, rtLng As Long, PortDCB As DCB
    Dim cTimeOuts As COMMTIMEOUTS
    Prepare_Port = False
    '1) open port
    PortNumber = "COM" & Trim(Text1.Text) & ":"
    hPort = CreateFile(PortNumber, GENERIC_READ Or GENERIC_WRITE, _
                       ByVal 0&, ByVal 0&, _
                       OPEN_EXISTING, ByVal 0&, ByVal 0&)
        If wMsg Then Display_Error 'Display the error message
        rtLng = Close_Port
        Prepare_Port = False
        Exit Function
    End If
    '2) Configure port
    With PortDCB
        .DCBlength = Len(DCB) 'Initialize DCB Length member
        .BaudRate = CLng(Val(Trim(Text2.Text))) 'Current Baud rate
        .ByteSize = 8 'Number of bits
        .StopBits = ONESTOPBIT
        .Parity = NOPARITY
    End With
    rtLng = SetCommState(hPort, PortDCB)
    If rtLng = 0 Then 'Failure
        If wMsg Then Display_Error
        Prepare_Port = False
        Exit Function
    End If
    '3) Set the timeout properties
    With cTimeOuts
        .ReadIntervalTimeout = MAXDWORD  'How long to wait between receiving chars before timing out
        If wMsg Then .ReadTotalTimeoutMultiplier = 1000  'How long to wait before returning
        If Not wMsg Then .ReadTotalTimeoutMultiplier = 0 'How long to wait before returning
        .ReadTotalTimeoutConstant = 0  'How much additional time to wait before returning for each byte that was requested in the read operation
        .WriteTotalTimeoutMultiplier = 0
        .WriteTotalTimeoutConstant = 0
    End With
    rtLng = SetCommTimeouts(hPort, cTimeOuts)
    If rtLng = 0 Then 'Failure
        If wMsg Then Display_Error
        Prepare_Port = False
        Exit Function
    End If
    Prepare_Port = True
End Function

Private Function Close_Port() As Long
    Close_Port = CloseHandle(hPort)
End Function
Private Sub Display_Error()
    Dim LastError As Long, msgbuf As String, i As Long
    LastError = Err.LastDllError
    msgbuf = Space(256)
    i = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, LastError, 0, msgbuf, 256, 0)
    MsgBox "Error in API: " & Left(msgbuf, i)
End Sub

Private Sub Command1_Click()
    Dim rtrLng As Long
    rtLng = Write_Port(1)
End Sub

Private Sub Command2_Click()
    Dim rtrLng As Long
    rtLng = Write_Port(0)
End Sub

Private Sub Command3_Click()
End Sub

Private Sub Form_Load()
    hPortReady = False
    If Not hPortReady Then
        hPortReady = Prepare_Port(True)
        If hPortReady Then
            Command1.Enabled = True
            Command2.Enabled = True
        End If
    End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
End Sub

Private Sub Timer1_Timer()
    'When the user enables the timer, the LED turns itself on & off every second
    Write_Port (1) 'On
    Sleep 1000 'Wait a second
    Write_Port (0) 'Off
End Sub

The program sends a 1 to turn the LED on and a 0 to turn it off.

Here’s the code to program pin 13 in the Arduino to turn it off & on with a 0 and 1 respectively:

/*The following program turns an LED on & off from the computer*/

int ledPin = 13;
void setup()
  Serial.begin(9600); //Set the Baud rate
  pinMode(ledPin, OUTPUT); //Initialize the pin for output 

void loop()
  if(Serial.available() > 0) //If data is received
    int inByte =; //Store data received
    if(inByte == 1) {digitalWrite(ledPin, HIGH);} //Turn it on
    if(inByte == 0) {digitalWrite(ledPin, LOW);} //Turn it off


Here’s another example using VB6 to communicate with the Arduino via serial communication. In this example, VB tracks the state of an LED. See attachment for VB code. Here is the sketch for the Arduino:

/*The following sketch turns an LED on & off using a 
  physical button. The program takes in consideration
  the debouncing of the switch. It also writes a 0 when  
  the LED is off and 1 when is on to the port, so that 
  the state of the LED can be monitored using serial
  communication via the USB port */

int SwitchPin = 8; //The pin the button is connected to
int ledPin = 13; //The pin the LED is connected to
int ledByte = 0; //The byte that tracks the state of the LED
boolean lastButton = LOW; //Tracks the state of the last push
boolean ledOn = false; //Send an on or off signal to the controller
boolean currentButton = LOW; //Current state of the button

void setup()
  //Set the variables
  pinMode(SwitchPin, INPUT);
  pinMode(ledPin, OUTPUT); 

int GetButtonState(boolean bStatus)
/* This function determines the state of the LED. If
   it is on, returns 1; otherwise 0 */
  if(bStatus == true) {return 1;}
  return 0;  

boolean debounce(boolean last)
/*This function takes care of the debouncing noise
 by comparing the last and current state of the switch.
 it updates its value after a 5 milliseconds delay. */
  boolean current = digitalRead(SwitchPin);
  if(last != current)
    current = digitalRead(SwitchPin); //Steady state value
  return current;

void loop()
  currentButton = debounce(lastButton); //Get button state
  if(lastButton == LOW && currentButton == HIGH) //If button was pressed
    ledOn = !ledOn; //Change state
  lastButton = currentButton; //update lastButton state
  digitalWrite(ledPin, ledOn); //Turn LED On or Off based on LedOn value
  ledByte = GetButtonState(ledOn); //Determine the byte value to write to the port
  Serial.write(ledByte); //Send the data to the port
  delay(100); //Wait 100 milliseconds
  Serial.flush(); //Flush the serial port


VBCode.txt (10.4 KB)


hello there. I am developing a system for my final project that involves the bill acceptor and visual basic 6.0 as the main user interface. I am right now confused on how to start coding the connection of the two through the serial ports…do i even make sense? i am really sorry. i am way too much of a beginner and i badly need help on how to code. i am not really a good programmer and i am just trying to develop this system to be able to get through my final thesis project?

…anyone cares to help me start this? :frowning: i would really appreciate it.

since the bill acceptor is pulse interfaced, i got the pulses and was able to display it on the serial monitor. so now i do not know how to begin with the visual basic part of my system.

thank you!

do i even make sense?


since the bill acceptor is pulse interfaced, i got the pulses and was able to display it on the serial monitor. so now i do not know how to begin with the visual basic part of my system.

So, you are able to send data out the serial port. You are half way there. Now, you just need to write the PC app to read the data.

Personally, I'd move out of the stone ages and use use C#. Much nicer looking user interfaces. Much faster. And, reading and writing serial data is trivial.

Unlike some other languages with B in their name...

i really see your sarcasm and that you would rather die than using the ‘B’. hahaha But unfortunately i have to use it mainly because i don’t think i have enough time to change to other software for now.

It would really save my life if you could just help me a bit. :frowning: i just do not know how to start this. please? :frowning: maybe give me or teach me how to code on connecting the serial port to visual basic first? like how am i going to use the MSCOMM? i dont even get what people say in the internet about it right now. :frowning:

I don't understand how you think you have time to learn Visual Basic but not Visual C#. Especially since C# has built in support for reading from and writing to the serial port, while the different versions of VB may or may not.

Every time it has been suggested that I need to learn to write programs using VB, I simply say sure, whatever. And then I build the app using C# and no one can tell that it is not using VB, except that it is faster and it works.

It's because my project is due next week, and I am done doing most of what i need in VB6. the last thing i need to do is to connect the bill acceptor (or the payment receiver part of my system). If I have to learn another language, then I would be cramming myself and might end up not finishing my project. As I have said, I do not have time for now. i could teach myself that if only my time wasn't tied up to present my project.

...if you can't help me then what is this forum for?

i am just trying to learn and pass my final project here and so i am asking for help. otherwise i wouldn't.

Almost a decade old post, but nice one! Thanks for sharing! 8)