简体   繁体   English

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

[英]Creating a Serial Port in code in VB.net

I am trying to create a serial port in VB.net using code only. 我试图只使用代码在VB.net中创建一个串口。 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. 我试过实例化一个新的SeialPort()对象,但这似乎还不够。 I'm sure there is something simple I am missing and any help would be greatly appreciated! 我确信有一些简单的我想念,任何帮助将不胜感激! Thanks! 谢谢!

PS I should add that the problem I am having at this time is getting the code to handle the datareceived event. PS我应该补充一点,我此时遇到的问题是获取处理datareceived事件的代码。 Other than that it might be working, but I can't tell because of that problem. 除此之外它可能正在起作用,但由于这个问题我无法分辨。

If you want to use the events make sure you declare your serialPort object using the 'withevents'. 如果要使用事件,请确保使用'withevents'声明serialPort对象。 The below example will allow you to connect to a serial port, and will raise an event with the received string. 下面的示例将允许您连接到串行端口,并将使用接收的字符串引发事件。

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

I found this article to 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. 我遇到的唯一问题是如果端口在打开时被丢弃,应用程序似乎在退出时失败。

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

I have used the SerialPort .Net class in a past project and I worked fine. 我在过去的项目中使用过SerialPort .Net类,我工作得很好。 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. 检查控制面板中的硬件设置,确保使用相同的参数实例化类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM