硬件接口中的SerialPort问题 [英] SerialPort Problem in A hardware interface
问题描述
你好
这是我的PC和微控制器之间的硬件接口(此板在RS232和RS485中传输和接收数据)
Hello
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
http://obrazki.elektroda.pl/2668501600_1355938414.png
我每200毫秒通过MCU将40字节的字符串(例如,"A12132146544654589589546424649120540371964454B" +输入ascii代码)发送到PC,它向我显示了我编写的vb程序中接收到的数据
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
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
它的工作原理,并向我展示了接收到的数据.但是正如我在两台不同的计算机上测试时,有时它们有时会显示给我
"拒绝访问端口"COM1""
并且不起作用.正如您在我的程序中看到的那样,我以关闭subtoutine的方式关闭了串行端口activex,并使用关闭端口"(Button3)来设置我程序的管理特权,但是当它向我显示时,拒绝访问端口"COM1".然后我重置&
关闭PC超过5次可能会正常工作... 8-O
我检查了任务管理器,当我关闭表单时,在进程"选项卡"中看不到我的程序.
我通过Windows 7和Windows XP测试了该程序,并认为这可能是我在编程中的错,但是当我使用超级终端程序并得到相同的结果时,我会得到更多
我再次检查了Windows软件,但找不到任何使用"COM1"的程序.
我在硬件和PC之间购买了一个RS232到USB转换器,以通过Virtual Com Port传输数据,但是我遇到了以上问题..
我怎么了?
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?
推荐答案
我忘了说MCU在每200毫秒内同时发送数据
i forgot to say that the MCU send datas simultaneously in each 200mili second
这篇关于硬件接口中的SerialPort问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!