在 VB.net 中用代码创建串行端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/329865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:56:39  来源:igfitidea点击:

Creating a Serial Port in code in VB.net

vb.netvisual-studioserial-port

提问by Kyle Gibbons

I am trying to create a serial port in VB.net using code only. Because I am creating a class library I cannot use the built-in component. I have tried instantiating a new SeialPort() object, but that does not seem to be enough. I'm sure there is something simple I am missing and any help would be greatly appreciated! Thanks!

我正在尝试仅使用代码在 VB.net 中创建串行端口。因为我正在创建一个类库,所以无法使用内置组件。我尝试实例化一个新的 SeialPort() 对象,但这似乎还不够。我确定我缺少一些简单的东西,任何帮助将不胜感激!谢谢!

P.S. I should add that the problem I am having at this time is getting the code to handle the datareceived event. Other than that it might be working, but I can't tell because of that problem.

PS我应该补充一点,我此时遇到的问题是获取处理数据接收事件的代码。除此之外,它可能正在工作,但由于这个问题,我无法判断。

回答by Dilbert789

If you want to use the events make sure you declare your serialPort object using the 'withevents'. The below example will allow you to connect to a serial port, and will raise an event with the received string.

如果您想使用这些事件,请确保使用“withevents”声明您的 serialPort 对象。下面的示例将允许您连接到串行端口,并将使用接收到的字符串引发事件。

Imports System.Threading

Imports System.IO

Imports System.Text

Imports System.IO.Ports


Public Class clsBarcodeScanner

Public Event ScanDataRecieved(ByVal data As String)
WithEvents comPort As SerialPort

Public Sub Connect()
    Try
        comPort = My.Computer.Ports.OpenSerialPort("COM5", 9600)
    Catch
    End Try
End Sub

Public Sub Disconnect()

    If comPort IsNot Nothing AndAlso comPort.IsOpen Then
        comPort.Close()
    End If

End Sub

Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles comPort.DataReceived
    Dim str As String = ""
    If e.EventType = SerialData.Chars Then
        Do
            Dim bytecount As Integer = comPort.BytesToRead

            If bytecount = 0 Then
                Exit Do
            End If
            Dim byteBuffer(bytecount) As Byte


            comPort.Read(byteBuffer, 0, bytecount)
            str = str & System.Text.Encoding.ASCII.GetString(byteBuffer, 0, 1)

        Loop
    End If

    RaiseEvent ScanDataRecieved(str)

End Sub
End Class

回答by BCS

I found this articleto be quite good.

我发现这篇文章非常好。

The code i wrote from it is:

我从它写的代码是:

port = new System.IO.Ports.SerialPort(name, 4800, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
port.Open();

void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    buffer = port.ReadLine();
    // process line
}

Sorry it's C# but...

对不起,它是 C# 但是...

The only issue I have with it is if the port is dropped while it's open, the app seems to fail on exit.

我遇到的唯一问题是如果端口在打开时被丢弃,应用程序似乎在退出时失败。

回答by Kyle Gibbons

Thank you all for your help, especially the answer about instantiating a class using the WithEvents keyword.

感谢大家的帮助,尤其是关于使用 WithEvents 关键字实例化类的答案。

I found a really great article that explains how to create a manager class for the serial port. It also discusses sending Binary as well as Hex data to the serial port. It was quite helpful.

我找到了一篇非常棒的文章,它解释了如何为串行端口创建管理器类。它还讨论了向串行端口发送二进制和十六进制数据。这很有帮助。

http://www.dreamincode.net/forums/showtopic37361.htm

http://www.dreamincode.net/forums/showtopic37361.htm

回答by Hapkido

I have used the SerialPort .Net class in a past project and I worked fine. You really don't need anything else. Check the hardware setting in the control panel and make sure you instantiate the class with the same parameters.

我在过去的项目中使用了 SerialPort .Net 类,并且工作正常。你真的不需要其他任何东西。检查控制面板中的硬件设置,并确保使用相同的参数实例化类。