Serial data to Visual Basic, Visual C++, Java

Hello all,

I'm interested in sending serial data from an arduino to my PC and have it displayed on either VB,VC++ or Java program. I'm not sure which one would be easiest to do. I would like to be able to make a simple program rather quickly ... nothing too fancy.
I just need to display a reading from one sensor.

thanks,
Phil

You might try Processing at http://processing.org/. It uses much the same IDE as the Arduino and is similar in many other respects. There are many examples on the Forum and elsewhere to provide the capability you are seeking.

If you use Visual Basic, communicating with the Arduino is pretty simple stuff.

  1. Add the object Microsoft Comm Control 6.0 to your project, and put it on the form.

  2. Set the correct properties for MSComm1 (baud-rate, com-port, etc.) This depends on your Arduino-code and your computer setup.

  3. Add the following code to the Form_Load() event (double click the form):

Private Sub Form_Load()
MSComm1.PortOpen = True
End Sub
  1. Add a Timer to your form (the small clock-symbol-thingie) and set it's interval to something like 100ms (Interval: 100)

  2. Add the following code to the timer (double click the timer-object on the form)

Private Sub Timer1_Timer()
If MSComm1.InBufferCount > 0 Then
    Dim buf as string
    buf = MSComm1.Input
End If
End Sub
  1. To send data back to the Arduino, you place the following code in your program (in a button_Click() event or something like that)
MSComm1.Output "whatever you want to send"

Thanks guys for the help. I think for now I'm going to start with Processing. It seems to be a lot easier to work with. I would like to play around with some VB & C++ , but I don't think I'm ready for it at the moment.

Thanks for the code Plastbox. That was very helpful. The only thing is ... I don't have VB 6.0 .. I have the free VB 2008 Express. I don't think it has MS Comm. It does have SerialPort.

thanks,
Phil

Aaah, ok. I just assumed VB6 as it is by far the most easy windows environment and language to get started with. Also, people whine constantly about the performance and such of programs made with VB6. Up yours, people! When one is talking about small utility-applications and stuff like that, performance doesn't even come into play. :wink:

Yeah ... I don't know why people complain about VB. I don't care about performance differences. It's not like I'm programming a 3D shooter or anything. Besides... I was reading that most programmers aren't even good enough to code in C++ to make it any faster than if it were in VB. I definitely know that I'm not good enough.

I'm more of a hardware guy than a programmer. I would just like to make some quick and easy windows apps that are functional.

Phil

The newer versions of VB ( the .net versions) are not quite as easy to program to use the serial port. But it's possible.

I have some kode that recieves data from Arduino if anybody is interested.

It's a very simple app.

And by the way, in the new versions there is no speed penalty for using VB, it's compiled to the same intermediary language as the other .net languages.

Hello MikMo,

Please do post the code. I would like to see it.

thanks,
Phil

I just realized the the VB program is on a computer that i stuffed under my bed to make room for a major party.

I will have it back in place again in a couple of days. My code is based on a small VB sample app. i found somewhere on the internet. It was something like a small two way chat program using a serial line to ceooent two PC's.

Cool

thanks,
Phil

Hey I think I found the source code for the chat VB application.

Pasting it:

Visual Basic 2005 band up – Chat over serial port

Imports System
Imports System.IO.Ports
Imports System.Threading

Public Class PortChat
Shared _continue As Boolean
Shared _serialPort As SerialPort

Public Shared Sub Main()
Dim name As String
Dim message As String
Dim sComparer As StringComparer = StringComparer.OrdinalIgnoreCase
Dim readThread As Thread = New Thread(AddressOf Read)

' Create a new SerialPort object with default settings.
_serialPort = New SerialPort()

' Allow the user to set the appropriate properties.
_serialPort.PortName = SetPortName(_serialPort.PortName)
_serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate)
_serialPort.Parity = SetPortParity(_serialPort.Parity)
_serialPort.DataBits = SetPortDataBits(_serialPort.DataBits)
_serialPort.StopBits = SetPortStopBits(_serialPort.StopBits)
_serialPort.Handshake = SetPortHandshake(_serialPort.Handshake)

' Set the read/write timeouts
_serialPort.ReadTimeout = 500
_serialPort.WriteTimeout = 500

_serialPort.Open()
_continue = True
readThread.Start()

Console.Write("Name: ")
name = Console.ReadLine()

Console.WriteLine("Type QUIT to exit")

While (_continue)
message = Console.ReadLine()

If sComparer.Equals("quit", message) Then
_continue = False
Else
_serialPort.WriteLine( _
String.Format("<{0}>: {1}", name, message))
End If
end while

readThread.Join()
_serialPort.Close()
End Sub

Public Shared Sub Read()
While (_continue)
Try
Dim message As String = _serialPort.ReadLine()
Console.WriteLine(message)
Catch ex As TimeoutException
' Do nothing
End Try
End While
End Sub

Public Shared Function SetPortName(ByVal defaultPortName As String) As String
Dim newPortName As String

Console.WriteLine("Available Ports:")
Dim s As String
For Each s In SerialPort.GetPortNames()
Console.WriteLine(" {0}", s)
Next s

Console.Write("COM port({0}): ", defaultPortName)
newPortName = Console.ReadLine()

If newPortName = "" Then
newPortName = defaultPortName
End If
Return newPortName
End Function

Public Shared Function SetPortBaudRate(ByVal defaultPortBaudRate As Integer) As Integer
Dim newBaudRate As String

Console.Write("Baud Rate({0}): ", defaultPortBaudRate)
newBaudRate = Console.ReadLine()

If newBaudRate = "" Then
newBaudRate = defaultPortBaudRate.ToString()
End If

Return Integer.Parse(newBaudRate)
End Function

Public Shared Function SetPortParity(ByVal defaultPortParity As Parity) As Parity
Dim newParity As String

Console.WriteLine("Available Parity options:")
Dim s As String
For Each s In [Enum].GetNames(GetType(Parity))
Console.WriteLine(" {0}", s)
Next s

Console.Write("Parity({0}):", defaultPortParity.ToString())
newparity = Console.ReadLine()

If newparity = "" Then
newparity = defaultPortParity.ToString()
End If

Return CType([Enum].Parse(GetType(Parity), newParity), Parity)
End Function

Public Shared Function SetPortDataBits(ByVal defaultPortDataBits As Integer) As Integer
Dim newDataBits As String

Console.Write("Data Bits({0}): ", defaultPortDataBits)
newDataBits = Console.ReadLine()

If newDataBits = "" Then
newDataBits = defaultPortDataBits.ToString()
End If

Return Integer.Parse(newDataBits)
End Function

Public Shared Function SetPortStopBits(ByVal defaultPortStopBits As StopBits) As StopBits
Dim newStopBits As String

Console.WriteLine("Available Stop Bits options:")
Dim s As String
For Each s In [Enum].GetNames(GetType(StopBits))
Console.WriteLine(" {0}", s)
Next s

Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString())
newStopBits = Console.ReadLine()

If newStopBits = "" Then
newStopBits = defaultPortStopBits.ToString()
End If

Return CType([Enum].Parse(GetType(StopBits), newStopBits), StopBits)
End Function

Public Shared Function SetPortHandshake(ByVal defaultPortHandshake As Handshake) As Handshake
Dim newHandshake As String

Console.WriteLine("Available Handshake options:")
Dim s As String
For Each s In [Enum].GetNames(GetType(Handshake))
Console.WriteLine(" {0}", s)
Next s

Console.Write("Stop Bits({0}):", defaultPortHandshake.ToString())
newHandshake = Console.ReadLine()

If newHandshake = "" Then
newHandshake = defaultPortHandshake.ToString()
End If

Return CType([Enum].Parse(GetType(Handshake), newHandshake), Handshake)
End Function
End Class

Thanks auerlg.

I'm messing around with Processing right now ... not as easy as I initially thought it would be. I figured out last night how to print my data to a window using text(); ... still trying to figure out how to refresh it without overlapping the previous value.

I guess I should still look into using VB. Is that code for the 2 way chat program? If so ... how easy would it be for me to use this for one-way communication (Arduino to VB) ?

thanks,
Phil

Hello All.

Are there anybody that has a VB 2008 Express program running to control the Arduino.
maby someone can make a toturical on switching a led on and off from VB,
and reading a bottom press in VB from Arduino input.

Just to get us beginners started.

Regards
Brian

I don't know if you are interested, but I adapted some serial C++ class for its use under linux.

The code is posted in this website Code, along with some other useful codes.

Let me know if it helped!!!

Dolphin

sorry for the delay. i just got sidetracked by work and new grilfriend :slight_smile:

Here's a link to my VB projekt. It's only one way Arduino -> VB, but i should be a piece of cake to also send the other way.

If anbody modify it please post here so we can share our VB <-> Arduino experiences

EDIT :

Wouldn't it be nice to make some kind of "generalised" VB class or component to interface with Arduino, mabe even some kind of dataexchange protocol on top of it ?

May be any of you can help me.

Sometimes, when speaking to arduino from linux, i just read crap from arduino, and after some refreshes, i read good values.
Both are configured at 9600, no parity, 8bits, 1 stop bit.

Do you know why is it happening?

Hi Mikmo!
can you put the link? I´m trying to develop some interface betwen Arduino and a VB6 app. I was wondering to write a protocol using the interrupt signals an serial event for read an write de analogs an digital pins from a VB6 environment.
I´ll be pleased to send the results, but at this moment is only a paper. I´m a newbie in C programming.

Sorry to resurrect a (relatively) old thread, but I found something that may be helpful to those who want to use VBScript to communicate to the Arduino. It's a free OCX available at:
http://home.comcast.net/~hardandsoftware/NETCommOCX.htm

The NETCommOCX is an ActiveX control that wraps the functionality of MSComm32.ocx.

Using this, it was easy for me to write a script that sends the date and time to my sketch with no special programming environment installed.

John

Sorry !

I just realized that i goofed up and forgot to actually post the promised link.

Here it is.

http://mikmo.dk/misc/vbarduino.zip

I'm currently working on a much more advanced project involving Arduino and VB.net. When i get a little further more source code will be made available.

Hi,
This may have been posted before, but "repetition is the mother of learning".
It is possible to send data on serial port in windows just by using the windows-installed components. A simple vb script that reads a text file and sends it to serial port is shown below:

Const ForReading = 1
Const ForWriting = 2

'-------------------------------
' open USB serial port (COMx);
'
' If the serial monitor in Arduino IDE is open, you will get an "access denied" error.
' Just make sure that the serial monitor is closed (so bytes are not sent by the arduino board).
'-------------------------------
Set fso = CreateObject("Scripting.FileSystemObject")
Set com = fso.OpenTextFile("COM4:9600,N,8,1", ForWriting)

'---------------------------------------------
' read content of text file line by line;
' write line to COMx;
'---------------------------------------------

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\docs\quotes.txt", ForReading)

MsgBox("Ready to write file content to COM")

   Do While objFile.AtEndOfStream <> True
      '--------------------------------------------------
      ' read a few (10) characters at a time; serial buffer is limited to 32 characters;
      ' writing a character to eeprom takes about 11 ms (assuming that there is no serial.prints in the loop);
      ' therefore, after each batch of 10 chars sent to COM, we should wait no less than 110 ms;
      ' we use 200 to have a margin of safety;
      '--------------------------------------------------

      strChars = objFile.Read(10)
      com.Write(strChars)
      WScript.Sleep(200)
   Loop


objFile.Close
com.Close()

MsgBox("Finished writing to COM")

I used this script to load an 25LC256 eprom with quotes from a text file, part of my implementation of the "Life clock" project initiated by Mr. BroHogan (see http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1214682544).
According to the datasheet, the eprom requires a 5 ms delay after each byte written (it takes its time to carve the data onto the silicon). In my writeByte function, I used a delay of 10 ms.

A 32K text file takes about 10 minutes to write to the eprom using this script.