Shop OBEX P1 Docs P2 Docs Learn Events
Serial communication problem with BS2 and VB.NET — Parallax Forums

Serial communication problem with BS2 and VB.NET

millitantmillitant Posts: 15
edited 2010-10-26 07:55 in BASIC Stamp
Hey,

Im new to the forum, and havent had the time to skim through all the posts, perhaps this problem has already been solved, but i havent found anything that can help.

this is the problem, i managed to get my bs2 to communicate with my pc, it sends and receives perfectly fine. but all of a sudden last night all my work has come crashing. my app just keeps hanging when i try to connect to the COM port and closing or opening the port doesnt work. so i went back to basics.

this is my bs2 code:
' {$STAMP BS2}
' {$PBASIC 2.5}
Trigger CON 1

rawDist VAR Word

Ping PIN 15

DO
  Ping = 0
  PULSOUT Ping, Trigger
  PULSIN Ping, 1, rawDist
  rawDist = rawDist * 2
  rawDist = rawDist / 2
  rawDist = rawDist / 30
  SEROUT 16, 16468, 100, [DEC rawDist, LF] ' baudmode: 9600,8,N,1
  PAUSE 100
LOOP

basically a small parallax ping is attached and measured the distance to my ceiling. nothing big.

anyway here is my .net code:
Public Class Form1
    Dim WithEvents serialPort As New IO.Ports.SerialPort

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
        serialPort.Close()
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            With serialPort
                .PortName = "COM3"
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .Handshake = IO.Ports.Handshake.None
                .ReadTimeout = 250
                .WriteTimeout = 250
            End With
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim myMessage As String
            myMessage = serialPort.ReadLine
            txtData.Text = myMessage
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub


    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            serialPort.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            serialPort.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

it worked before, i just cant seem to figure out why it wont work any more.
the design is simple just three buttons and 1 textblok. the serout code should be displayed in the textblok, every time i press button 1 it should read the line on the serial port again and post that in the textblok.

this is as soon as i press button1, it just hangs. i put a readtimeout to try to stop the crash but no such luck.

anyone know of any code i can try that does work with this set-up?

thanks.

Comments

  • YoshtiYoshti Posts: 108
    edited 2010-10-25 07:05
    ~S!~,
    Hi,
    It happened to me once. And don't know why, but my system decided to change the port # on me.
    So I changed it accordingly and everything was tickeedeeboo!

    I used Hyperterminal to finally find out that the port was no longer the same!

    Might give it a try...

    Cheers
  • millitantmillitant Posts: 15
    edited 2010-10-25 08:13
    hey Yoshti,

    Thanks for the reply, i checked to see what port im using with Hyperterminal, its strange, but it says im using COM port 5, but when i use BASIC stamp editor, it says im using COM port 3.

    i found out the hard way that you cant allow two programs to use a single COM port, just got me a blue screen of death error.

    told me that some midi device was causing a lot of problems. it seems that vs 2010 and Basic stamp editor can not use the same port at the same time. i didnt know that. causes my computer to flip.

    i changed my code to COM 5, and close BASIC stamp editor, no luck still nothing coming through. out of pure frustration i reset everything, closed Basic stamp editor and tried just receiving a string from my BS2, and what do you know it works again on COM port 3. my original COM port.

    its a little weird, im going to have to add some code to check which COM port is active.

    sorry if this is not really a Basic stamp problem, i dont know why my computer is giving me a hard time.

    im just going to try some of the examples in one of the other posts. see if i can get some results.
  • YoshtiYoshti Posts: 108
    edited 2010-10-25 11:39
    ~S!~
    Hi,
    Forgot about that one. When I installed the Basic editor, If I remember well, I had to de-install my PALM stuff (or at least to disable it) so both software are not trying to communicate on the same port.

    Cheers
  • millitantmillitant Posts: 15
    edited 2010-10-26 07:02
    Hey,

    im having problems with a little vb code while communicating with my basic stamp. im trying to reset my basic stamp, and it does work but the second it reset my vb code gives me an exception.
    If serialPort.IsOpen Then
                    Do
                        in_data = serialPort.ReadLine()
                    Loop Until in_data = "RDY"
                Else
                    Thread.Sleep(50)
                End If
                If serialPort.IsOpen Then
                    serialPort.WriteLine("!" & vbLf & Servo1 & vbLf & Servo2)
                Else
                    Thread.Sleep(50)
                End If
    

    i cant figure out what the problem is. it looks like my vb code is trying to keep the connection open, but my reset code shouldnt close the COM port.

    anyone have any ideas?

    this is my reset code:
    Private Sub Reset()
            Try
                If serialPort.IsOpen Then
                    serialPort.DtrEnable = True
                    Thread.Sleep(2)
                    serialPort.DtrEnable = False
                    serialPort.BreakState = True
                    Thread.Sleep(50)
                    serialPort.BreakState = False
                    Thread.Sleep(5)
                    serialPort.DiscardInBuffer()
                    Thread.Sleep(5)
                    serialPort.Dispose()
                    Thread.Sleep(150)
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                If (serialPort IsNot Nothing) Then serialPort.Dispose()
            End Try
        End Sub
    

    the error that springs up:

    "The I/O operation has been aborted because of either a thread exit or an application request."

    this bit of code come from one of the template examples on the forum. so probably someone already figured it out and perhaps can give me a hand.

    thanks.
  • millitantmillitant Posts: 15
    edited 2010-10-26 07:55
    sorry guys for the question already solved it. thanks for any future help i will most probably need.
Sign In or Register to comment.