none
通过 SerialPort 类操作串口,怎么接收不到数据呢? RRS feed

  • 问题

  • 我的软件环境是vs2012 RC,Win8 RP,硬件环境是一根usb转串口现(驱动正常),串口转ps/2接口(功能是接收串口发送过来的数据,发送到另外一个键盘的输入端)

    我是通过SerialPort类对串口进行的写入操作,write 或 writeline 两个方法都试了,可那边ps/2接口那端接收不到数据是怎么回事呢?是我操作的问题,还是COM转ps/2端对输入的数据有格式要求?请高手们帮忙看看,先感谢了。

    UI:

    CODE:

    Imports System.IO.Ports
    
    Public Class Window3
    
        Private sp As SerialPort
    
        Private Sub Window3_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
            Dim sp_portname As String = "COM3", sp_baudrate As Integer = 9600
            sp = New SerialPort(sp_portname, sp_baudrate, Parity.None, 8)
            sp.ReceivedBytesThreshold = 5
            sp.Handshake = Handshake.RequestToSend
            sp.ReadTimeout = 200
            sp.WriteTimeout = 200
            AddHandler sp.DataReceived, AddressOf sp_DataReceived
            AddHandler sp.ErrorReceived, AddressOf sp_ErrorReceived
            but2.IsEnabled = False
        End Sub
    
        Private Sub Window3_Closed(sender As Object, e As EventArgs) Handles Me.Closed
            If sp IsNot Nothing AndAlso sp.IsOpen Then sp.Close()
        End Sub
    
        Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
            If sp IsNot Nothing AndAlso Not sp.IsOpen Then
                sp.Open()
                but2.IsEnabled = sp.IsOpen
            End If
        End Sub
    
        Private Sub Button_Click_2(sender As Object, e As RoutedEventArgs)
            'sp.Write("abc")
            sp.Write(New Byte() {"1"}, 0, 1)
        End Sub
    
        Private Sub sp_DataReceived(sender As Object, e As SerialDataReceivedEventArgs)
            MsgBox(sp.ReadExisting)
        End Sub
    
        Private Sub sp_ErrorReceived(sender As Object, e As SerialErrorReceivedEventArgs)
            MsgBox(sp.ReadExisting)
        End Sub
    
    End Class


    人要诚实!还要踏实! 我先为人人,人人再为我! 我的Spaces: http://mydodu.spaces.live.com/




    • 已编辑 Dodu.NET 2012年6月18日 0:39
    2012年6月16日 0:24

答案

全部回复