USB/RS232 emulator on PIC only returning 1st char w/ VB.NET

Status
Not open for further replies.

pratiken

Newbie level 5
Joined
Mar 13, 2014
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
85
Hi guys
I have a device using a PIC18F2550 and runs a USB/RS232 emulator.
I can connect to the device AND control it perfectly fine with Putty or Hyperterminal.

Now I'm trying to build a VB.NET app that also connects to the device and sends it data.
However, I've built multiple VB apps based on different Serial Com tutorials and they all react the same way. The device only reads back the first character of the command I send it.
For example:
if I type
"Command 1"
The read back is
"C"

I have a feeling this is an issue with the PIC as different VB.NET Serial programs give the same results.

Is there something specific I should look for in the PIC's firmware to solve this?

Thanks for the help!
 
Last edited:

if the PIC18 works Ok with terminal emulaltors such as PUtty it should work with VB

have you tried using the SerialPort component from VBs toolbox?

I find that works OK talking to my PIC microcontrollers
 

Hi Horace,
Yes I did.
I followed a Youtube video tutorial and came up with this:

Code:
Imports System
Imports System.Text
Imports System.Drawing
Imports System.IO.Ports
Imports System.Windows.Forms

Public Class Form1

    Dim WithEvents COMPort As New SerialPort
    Dim myPort As Array
    Delegate Sub SetTextCallBack(ByVal [text] As String)


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        myPort = IO.Ports.SerialPort.GetPortNames()
        ports.Items.AddRange(myPort)
        Send.Enabled = False
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        SerialPort1.PortName = ports.Text
        SerialPort1.BaudRate = "9600"
        SerialPort1.Open()
        Send.Enabled = True
        Button2.Enabled = False
    End Sub


    Private Sub Send_Click(sender As Object, e As EventArgs) Handles Send.Click
        SerialPort1.Write(Input.Text + vbCr)
    End Sub


    Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Me.ReceiveText(SerialPort1.ReadExisting())
    End Sub


    Public Sub ReceiveText(ByVal [text] As String)
        If Me.Console.InvokeRequired Then
            Dim x As New SetTextCallBack(AddressOf ReceiveText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.Console.AppendText([text])

        End If
    End Sub

End Class

As well,
Electronics-diy.com has a very simple VB.NET program to connect and send commands to a serial port
https://electronics-diy.com/USB_IO_Board.php

Both programs yield the same thing, first character returned and nothing more.

What could this be??


Something to note: I can't open two serial connections simultaneously. If I open and connect with a second PuTTY window, it says that it's unable to connect to the device regardless of the fact that a window is already open and connected to it.
Could this indicate a problem?
 
Last edited:

no idea why you code does not work - it looks OK

once you have a port open, e.g. by your VB program, you cannot open it with another program such as PUtty

my VB terminal code is
Code:
Public Class Form1

    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Console.WriteLine("key hit" + e.KeyChar)
        SerialPort1.Write(e.KeyChar)
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        'Console.WriteLine("serial port receive" + SerialPort1.ReadExisting())
        'Console.WriteLine(SerialPort1.ReadExisting())
        TextBox1.AppendText(SerialPort1.ReadExisting())
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim port As Integer = 0
        Dim COMmenuitem As ToolStripMenuItem = MenuStrip1.Items.Item(1)
        ' go thru COM ports adding them to the list of available ports
        For port = 0 To 20
            'Console.WriteLine("form1 load")
            SerialPort1.PortName = "COM" + port.ToString()
            Try
                SerialPort1.Open()                                                          ' attempt to open serial port
                TextBox1.AppendText("Found " + SerialPort1.PortName + vbNewLine)
                Console.WriteLine("serial port open OK {0}", SerialPort1.PortName)
                SerialPort1.Close()                                                         ' open OK, close it
                Dim item As ToolStripMenuItem = New ToolStripMenuItem(SerialPort1.PortName) ' create a new menu item
                AddHandler item.Click, AddressOf Me.COMToolStripMenuItem_Click              ' add event handler
                COMmenuitem.DropDownItems.Add(item)                                         ' add to the list of COM ports
            Catch ex As Exception
                Console.WriteLine("serial port open fail {0}", SerialPort1.PortName)
            End Try
        Next

    End Sub

    Private Sub COMToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Console.WriteLine("item selected {0} {1}", e.ToString(), sender.ToString())
        SerialPort1.Close()
        SerialPort1.PortName = sender.ToString()
        SerialPort1.Open()
        TextBox1.AppendText("Opened Port " + sender.ToString() + " baud rate " + SerialPort1.BaudRate.ToString() + vbNewLine)
        Text = "Port " + sender.ToString() + " baud rate " + SerialPort1.BaudRate.ToString()
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click, ToolStripMenuItem9.Click, ToolStripMenuItem8.Click, ToolStripMenuItem7.Click, ToolStripMenuItem6.Click, ToolStripMenuItem5.Click, ToolStripMenuItem4.Click, ToolStripMenuItem3.Click
        Console.WriteLine("item selected {0} {1}", e.ToString(), sender.ToString())
        SerialPort1.BaudRate = sender.ToString()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Close()
    End Sub
End Class
on Form Load it scans for COM port and put them in a MenuItem for selection, e.g. the Console output looks like


and the VB form looks like


it is talking to a Bytronic PIC24 trainer
**broken link removed**
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…