SerialPort Problem in A hardware interface

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello<br/>
here is my hardware interface between PC and my Microcontroller(MCU) ( this board transfer and recived data in RS232 and RS485)
http://obrazki.elektroda.pl/2668501600_1355938414.png<br/>

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<br/>

<pre class="prettyprint lang-vb 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
[/code]
<br/>
<br/>
<br/>
it works and show me the recived data. but as i test in two differenet computers sometimes they work some times show me
<br/>
" Access to the port COM1 is denied"<br/>
and doesnt 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<br/>
<br/>
i checked task manager and when i close my form i cant see my program in "proccess tab"<br/>
<br/>
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
<br/>
<br/>
again i checked windows software and cant find any program that use "COM1"<br/>
<br/>
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..<br/>
<br/>
what is my problem?<br/>


View the full article
 
Back
Top