2

Author Topic: Serial Communication Using VB 2005  (Read 2552 times)

0 Members and 1 Guest are viewing this topic.

Offline hendrachubbyTopic starter

  • Full Member
  • ***
  • Posts: 49
  • Helpful? 0
Serial Communication Using VB 2005
« on: February 21, 2009, 02:41:12 AM »
How to connect serial port using VB and send ascii code to microcontroller ? I can do it with hyperterminal but i need to make a user interface using VB.
Thx

Offline daved

  • Beginner
  • *
  • Posts: 5
  • Helpful? 1
Re: Serial Communication Using VB 2005
« Reply #1 on: February 21, 2009, 01:37:31 PM »
Not using VB but am using C# to prototype a communications program to the Axon. Simply use a Serial Object and set all the params for the serial connection. Here is a listing for C#, it's very basic and should be able to be ported to VB with very little issues:

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading;

namespace SerialControl
{
    class Program
    {
        static SerialPort sp;
        static byte[] reply = new byte[16];

        static void Main(string[] args)
        {
            // initialize the serial port
            initSP();
            bool cont = true;

            try
            {
                sp.Open();
                Trace.WriteLine("Opening the COM port");
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.InnerException);
            }

            // Just keep sending the same message over and over again
            // infinite loop
            while (cont)
            {
                if (sp.IsOpen)
                {
                    sp.Write("Hello Axon!");

                    Thread.Sleep(100);

                    sp.Read(reply, 0, 16);

                    string s = System.Text.ASCIIEncoding.ASCII.GetString(reply);

                    Trace.WriteLine(s);
                }
                else
                {
                    cont = false;
                }
            }

            sp.Close();
           
        }

        static bool initSP()
        {
            bool Success = false;

            try
            {
                sp = new SerialPort();
                sp.PortName = "COM3";
                sp.BaudRate = 115200;
                sp.DataBits = 8;
                sp.Parity = Parity.None;
                sp.StopBits = StopBits.One;
               
                Success = true;
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.InnerException);
            }

            return Success;

        }// initSP
    }
}

Keep in mind I did this in about 2 minutes so there may be bugs, however it works.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,666
  • Helpful? 169
    • Society of Robots
Re: Serial Communication Using VB 2005
« Reply #2 on: February 25, 2009, 10:25:00 AM »
We've been looking into this. Have a look at this thread:
http://www.societyofrobots.com/robotforum/index.php?topic=6711.new;topicseen#new

 


Get Your Ad Here

data_list