Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi to all !
I wrote a code in VB2008/NET2.0,after a problem with my operating system i re-wrote this code.I'm using this code in order to communicate my Atmel board with my PC and display some graphic data via NI Measurement Studio.

The problem is,that now i can't make it communicate with Atmel,can someone of you tell me where is the error ?

The program is running but does not reads data from RS232,on the other hand i use Hyper Terminal to test my connection and it works perfect...
I have also try to different version of .NET (2.0 & 3.0).
Another question is that after some executions of this code my O.S. crashes (WinXP Home).
Is there any solution for these two problems?
Thank's a lot for your time! :-D :-D
-----------------CODE-----------------------------
VB
Imports System.IO.Ports.SerialPort
Imports System.Windows.Forms.Control
Imports System.Windows.Forms.Form

Public Class Form1

    Public Delegate Sub myDelegate()

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

        If SerialPort1.IsOpen Then
            SerialPort1.Close()
        End If

        Try
            With SerialPort1
                .PortName = "Com1"
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .ReceivedBytesThreshold = 1
            End With
            SerialPort1.Open()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Dispose()
        Try
            SerialPort1.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)

        TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
    End Sub

    Public Sub updateTextBox()
        Dim input As String
        input = SerialPort1.ReadExisting
        If input = "A" Then alive()
        If input = "T" Then tank()
        If input = "G" Then gauge()
        If input = "O" Then outemp()
        If input = "B" Then boiler()
        If input = "W" Then water()

        'Try

        'Catch ex As Exception

        'End Try
        With TextBox1
            .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
            .AppendText(SerialPort1.ReadLine)
            .ScrollToCaret()
        End With
    End Sub

    Sub tank()
        Dim tankval As Integer
        tankval = Integer.Parse(SerialPort1.ReadLine.Trim())
        Tank1.Value = tankval
        If tankval < 200 Then
            Beep()
            Led3.Value = True
            Led2.Value = False
            Led1.Value = False
        ElseIf tankval > 200 And tankval < 750 Then
            Led3.Value = False
            Led2.Value = True
            Led1.Value = False
        ElseIf tankval > 750 Then
            Led3.Value = False
            Led2.Value = False
            Led1.Value = True
        End If

    End Sub
    Sub water()
        Dim tankval1 As Integer
        tankval1 = Integer.Parse(SerialPort1.ReadLine.Trim())

        Tank2.Value = tankval1
        If tankval1 < 300 Then
            Beep()
            Led6.Value = True
            Led5.Value = False
            Led4.Value = False
        ElseIf tankval1 > 300 And tankval1 < 4500 Then
            Led6.Value = False
            Led5.Value = True
            Led4.Value = False
        ElseIf tankval1 > 4500 Then
            Led6.Value = False
            Led5.Value = False
            Led4.Value = True
        End If

    End Sub

    Sub gauge()
        Dim gauge As Double
        gauge = Integer.Parse(SerialPort1.ReadLine.Trim())
        If gauge < 0 Then gauge = 0
        gauge = gauge / 0.434 / 100
        Gauge1.Value = gauge
    End Sub
    Sub outemp()
        Dim temper As Integer
        temper = Integer.Parse(SerialPort1.ReadLine.Trim())

        Thermometer1.Value = temper
    End Sub

    Sub boiler()
        Dim boiler As Integer
        boiler = Integer.Parse(SerialPort1.ReadLine.Trim())

        Thermometer2.Value = boiler
    End Sub

    Sub alive()
        SerialPort1.Write("L")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SerialPort1.Close()
        Me.Close()
    End Sub
End Class

---------------------------------------------
Posted
Updated 18-May-10 6:39am
v2
Comments
TheyCallMeMrJames 18-May-10 15:02pm    
Can you post sample output from your Hyper Terminal session?

The pluses:

    • It looks like you're trapping any shutdown intent and closing (and thus disposing) the COM port

    • You are properly pushing back the changes to the UI to the correct thread



A couple of potential problems:

    • ReadLine is blocking...if there is no NewLine character coming across the COM port you're going to just be hanging the thread

    • You're using input = ReadExisting...then comparing single characters. Are you sure there is only one char in the buffer when you read?

    • Maybe also try using .ToUpper() before the compare...not sure what your data looks like.



Throw a couple of Debug.WriteLine methods in there and make sure you're not hanging that thread up or checking against strings that aren't the same length.
 
Share this answer
 
VB
T0
W0.0
G317
O502
B1023
T0
W0.0
G131
O328
B1023
T0

this is a view of hyper terminal.
Note that the code above was working perfect until i install again WinXp,might be a problem of .NET or of Service Pack 2 ?
i can't remember what i had installed before i format my PC.
The bytes from μC followed always by a CR byte.
I'm not an expert on VB.
You can see another post of my,i have used this code as a "base",there are some suggestions-corrections from other users.
 
Share this answer
 
Comment out these lines and you should see something in your textbox:

Dim input As String
input = SerialPort1.ReadExisting
If input = "A" Then alive()
If input = "T" Then tank()
If input = "G" Then gauge()
If input = "O" Then outemp()
If input = "B" Then boiler()
If input = "W" Then water()


Also...take out the .Font = New Font("Garamond", 12.0!, FontStyle.Bold) line as you shouldn't be instantiating a new font every time a byte hits your COM port.

In the above code you are reading all data on the serial port into a string. So when you get T0 and then you test it against a single character you will get false every time.

After eating all the data you attempt to place the result of a ReadLine into the textbox. I can't see anything ever appearing there.
 
Share this answer
 
I comment these lines but nothing in the textbox.
I use characters,such as "T" in order to jump my code in a specific SUB,
after the AVR transmit a value as INTEGER,for example Print "T" ; Litres ; Chr(10).
Is the initialization of COM port right for you,or i must set more parameters,like ReadTimeOut etc. ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900