serial port help

bisk_25

New member
Joined
Nov 12, 2007
Messages
1
Programming Experience
Beginner
Hello, Can someone please help me out with this....I ahve an assignment for school that I am working on in vb.net 2005. It consists of two programs on two different computers. Computer 1 contains two tank simulators (liquid) and the tank levels are sent out through rs232 to computer2. Computer 2 decodes this info and sends out control commands to computer 1, controlling the liquid and the process of the tanks. I will post the code below that I have written so far, my problem is when I debug the programs they both crash. If I run computer 1 program alone then, in telnet (comp2) I am able to view the data that is being sent. Can someone go over what I have so far and maybey point me in the right direction on how to correct this?? Please any help would be greatly appreciated.

Code On Computer 1:

VB.NET:
Public Class Form1



    Dim working As Boolean
    Dim ReceivedMessage As String

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick


        Dim Tank1_Level As Byte
        Dim Tank2_level As Byte

        Tank1_Level = Me.TankSystemSim1.GPIO1
        Tank2_level = Me.TankSystemSim2.GPIO1

        Dim message As String


        message = "$Tank_Level," & Tank1_Level.ToString() & "," & Tank2_level.ToString() & Chr(13)
        Me.SerialPort1.Write(message)





    End Sub



    Private Sub TankSystemSim2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub TankSystemSim2_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TankSystemSim2.Load

    End Sub

    

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.SerialPort1.Open()


    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Me.SerialPort1.Close()


    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
        Dim ReceivedLocker() As String
        working = True
        ReceivedMessage = Me.SerialPort1.ReadLine()
        If (ReceivedMessage <> Nothing And ReceivedMessage.Length > 0) Then
            ReceivedLocker = ReceivedMessage.Split(New [Char]() {","c})
            Me.TankSystemSim1.GPIO2 = ReceivedLocker(1)
            Me.TankSystemSim2.GPIO2 = ReceivedLocker(2)
        End If

    End Sub
End Class
Code on Computer 2

VB.NET:
Public Class Form1
    Dim ReceivedMessage As String
    Dim ReceivedLocker() As String
    Dim ReceivedInstruction1 As Byte
    Dim ReceivedInstruction2 As Byte
    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
        ReceivedMessage = Me.SerialPort1.ReadLine()
        If (ReceivedMessage <> Nothing And ReceivedMessage.Length > 0) Then
            ReceivedLocker = ReceivedMessage.Split(New [Char]() {","c})
            ReceivedInstruction1 = ReceivedLocker(1)
            ReceivedInstruction2 = ReceivedLocker(2)
        End If
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim InstructionTank1 As Byte
        Dim InstructionTank2 As Byte
        Dim SentMessage As String


        Select Case ReceivedInstruction1
            Case 40  'hex 40
                InstructionTank1 = 41
            Case 41, &H61  'hex 41,61
                InstructionTank1 = 41
            Case 22, &H23  'hex 22,23
                InstructionTank1 = 41
            Case 24, &H25 'hex 24, 25
                InstructionTank1 = 41
        End Select
        Select Case ReceivedInstruction2
            Case 40
                InstructionTank2 = 41
            Case 41, &H61  'hex 41,61
                InstructionTank1 = 41
            Case 22, &H23  'hex 22,23
                InstructionTank1 = 41
            Case 24, &H25 'hex 24, 25
                InstructionTank1 = 41
        End Select
        SentMessage = "$Tank_Instruction," & InstructionTank1 & "," & InstructionTank2 & Chr(13)
        Me.SerialPort1.Write(SentMessage)
        Me.Label1.Text = ReceivedMessage
    End Sub
 
Last edited by a moderator:
You haven't specified what you mean by "crash" exactly, there usually is information about exception types and messages and the call that throws them.

Are you sure about Chr(13)? At my system the serialport.NewLine is 10/vbLf, but why not use WriteLine instead of Write, I see you use ReadLine to read at both ends. Set NewLine property if you need to, but I don't think so here.

Me.TankSystemSim1.GPIO2 is not explained. Beware that you can't access UI controls directly from DataReceived since it is raised from a different thread.
 
Back
Top