COM port Access

amitguitarplayer

Active member
Joined
Jul 5, 2008
Messages
27
Programming Experience
5-10
Hi Guys,

I have a serious dilemma !! I have a com port COM3 and i USB device attached to it ! now when i use Hyper terminal and i can read and write to it using AT commands. for example when i type in "ATI" in hyper terminal it will return me a bunch of data !

how can i so the same using a VB program ? is there a way to emulate the functionality of Hyperterminal ?

I tried using System.IO.Ports and a SerialPort class but no success.. :(

Please Help !
 
Dim sp As New SerialPort("COM3")

sp.BaudRate = CInt("19200")
sp.DataBits = 8
sp.StopBits = StopBits.One
sp.Parity = Parity.None
sp.Handshake = Handshake.None
sp.RtsEnable = True
sp.Open()

Debug.Print("open")
sp.Write("ati")
sp.Read(buffer, 0, CInt(buffer.Length))
sp.BaseStream.Read(buffer, 0, CInt(buffer.Length))

it will open the port and also execute sp.write("ati")
but in hyperterminal when i execute ati, the com3 device will talk to me and return of info..

when i do sp.read it returns "ati" instead of the 3 lines.

please help !
 
Try this:
VB.NET:
Using sp As New SerialPort("COM3")
            sp.BaudRate = 19200
            sp.Handshake = Handshake.None
            sp.RtsEnable = True

            sp.WriteTimeout = 1500
            sp.ReadTimeout = 1500

            'now this could be the issue...what does your device determine as command end?
            sp.NewLine = vbLf

            Try
                sp.Open()

                sp.WriteLine("ati") 'write a command ending with the NewLine character(s)

                Debug.Print(sp.ReadLine) 'this waits until it receives the NewLine character, or the function times out, whatever comes first

            Catch ex As Exception
                Debug.Print(ex.Message)
            End Try
        End Using 'beyond this line the SerialPort Object will be closed/disposed and the port freed...

The point with the line end is pretty important, if the device does not receive the line end command it will not do anything. The HyperTerminal uses in default LineFeed (vbLf) so check for that (get a hold of the documentation of the device if necessary).

Bobby
 
Back
Top