COM to PS2(将串口读到的信息直接转换成键盘的输入信息)

1、使用MSComm读串口信息

Private Sub Command1_Click()

    If Me.MSComm1.PortOpen = True Then Me.MSComm1.PortOpen = False
   
    Me.MSComm1.CommPort = Me.cbCom.ListIndex + 1
    Me.MSComm1.Settings = Me.txtSettings.Text
    Me.MSComm1.InputLen = 0
    Me.MSComm1.PortOpen = True

End Sub

Private Sub MSComm1_OnComm()
Dim readline As String
    Select Case MSComm1.CommEvent
        Case comEvReceive
            '开始接收
            readline = MSComm1.Input
              
            formatData readline
        Case Else
    End Select
End Sub

2、将读到的信息做些处理

Private Sub formatData(ByVal str As String)
    '判断字符串是否以回车换行结束,如果是去掉
    If Asc(Mid(str, Len(str), 1)) = 10 And Asc(Mid(str, Len(str) - 1, 1)) = 13 Then
        str = Mid(str, 1, Len(str) - 2)
    End If
    '添加回车换行功能
    If selectEnter Then str = str + Chr(13) + Chr(10)
    '添加Tab功能
    If selectTab Then str = str + Chr(9)
    sendstring (str)
End Sub

3、出发键盘信息

 

Private Sub sendstring(ByVal str As String)
    SendKeys str
End Sub

 

c#

 private void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string data = "";
            data = sp.ReadLine();

            sendData(data);
        }

        private void sendData(string data)
        {
            SendKeys.SendWait(data);
            if (this.checkBox1.Checked)
            {
                SendKeys.SendWait("{enter}");
            }
            if (this.checkBox2.Checked)
            {
                SendKeys.SendWait("{tab}");
            }
        }

 

SendKeys.Send(data)不能用。

posted @ 2009-06-15 17:41  TNTZWC  阅读(721)  评论(0编辑  收藏  举报