SerialPort Problem in A hardware interface

Status
Not open for further replies.

baby_1

Advanced Member level 1
Joined
Dec 3, 2010
Messages
415
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Visit site
Activity points
4,277
Hello
here is my hardware interface between PC and my Microcontroller(MCU) ( this board transfer and recived data in RS232 and RS485)

i send a 40bytes string (for example "A12132146544654589546424649120540371964454B" + enter ascii code) to PC via MCU in each 200mili second and it shows me the recived data in my vb program that i wrote

Code:
Public Class Form1
    Dim msg As String

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        With SerialPort1
            If .IsOpen = True Then .Close()
        End With
    End Sub

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

    End Sub
    Private Sub search()
        Dim s As String = ""
        Dim ma As Byte = My.Computer.Ports.SerialPortNames.Count - 1
        ComboBox1.Items.Clear()
        For i As Byte = 0 To ma
            s = My.Computer.Ports.SerialPortNames(i).ToString
            If InStr(s, "i") = 0 Then
                ComboBox1.Items.Add(s)
            End If
        Next
        ma = ComboBox1.Items.Count
        If ma > 0 Then
            ComboBox1.SelectedIndex = ma - 1
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        search()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ComboBox1.Text <> "" Or InStr(ComboBox1.Text, "m") = 0 Then
            With SerialPort1
                Try
                    If .IsOpen = True Then .Close()
                Catch ex As Exception
                    MsgBox("Error 1", MsgBoxStyle.Information, "Error")
                    Exit Sub
                End Try
                .DataBits = 8
                .BaudRate = 4800
                .StopBits = IO.Ports.StopBits.One
                .Parity = IO.Ports.Parity.None
                .Handshake = IO.Ports.Handshake.None
                .PortName = ComboBox1.Text
                AddHandler SerialPort1.DataReceived, AddressOf SerialPort1_DataReceived
                Try

                    .Open()
                Catch ex As Exception
                    MsgBox(Err.Description, MsgBoxStyle.Information, "Error")
                    Me.Show()
                    Exit Sub
                End Try
                MsgBox("Connected")
            End With
        End If
    End Sub
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Try
            msg = msg & SerialPort1.ReadExisting

            Me.Invoke(New EventHandler(AddressOf baby))
        Catch ex As Exception

        End Try
    End Sub
    Private Sub baby()
        If InStr(msg, "OK") > 0 Then
            ListBox1.Items.Add(msg)
            msg = ""
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        With SerialPort1
            Try
                If .IsOpen = True Then .Close()
            Catch ex As Exception
                MsgBox("Error 2", MsgBoxStyle.Information, "Error")
                Exit Sub
            End Try
        End With
    End Sub
End Class

it works and show me the recived data. but as i test in two differenet computers sometimes they work some times show me
" Access to the port 'COM1' is denied"
and doesn't work.as you see in my program i close the serialport activex in formclosing subtoutine and with a "Close Port "(Button3) and set administrative privileges to my program but when it shows me " Access to the port 'COM1' is denied" and i reset & shutdown PCs more than 5 time it may work fine...8-O

i checked task manager and when i close my form i can't see my program in "proccess tab"

i test this program via windows 7 and windows xp and think it maybe my fault in programming but when i use hyperterminal program and get same result i getting more

again i checked windows software and can't find any program that use "COM1"

i bought a RS232 to USB converter between my hardware and PC to transfer data via Virtual Com Port but i see the above problem..

what is my problem?
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…