实现一个基于串口的手机AT信令模拟器

因项目需要,经常需要调试手机AT信令,觉得老是要插手机觉得很不方便,我自己还要用那:) 。于是便想到写一个手机串口的模拟器。

思路很简单。模拟串口通信,要么找两台机器用一根串口线连接,要么把机器自己的两个串口对接,要么用软件模拟器。

软件模拟器的原理也相当于两个串口对接:例如COM4和COM5,我往COM4发数据。COM5就能自动接收。

下面是具体的步骤:

1 安装免费的模拟软件com0com-1.4.0.0-386-chk.zip。以下是自带的说明文档

1. Start the "Add/Remove Hardware" wizard in Control Panel.
  2. Click "Add/Troubleshoot a Device".
  3. Select "Add a new device" and then click Next.
  4. Select "No, I Want to Select the Hardware from a list".
  5. For the first time (if the driver is not installed yet):
       1. Select "Other Devices" and then click Next.
       2. Click "Have Disk".
       3. Enter path to the directory with com0com.inf and com0com.sys
          files and then click OK.
     For the next time (adding one more port pair) select
     "com0com - serial port emulators" and then click Next.
  6. Select "com0com - bus for serial port pair emulator" and then
     click Next.

 另外安装好之后 change the names CNCA0 and CNCB0 to COM2 and COM3。这个一定要哦,否则Mscomm不能找到端口
[HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/com0com/Parameters/CNCA0]
"PortName"="COM4"
[HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/com0com/Parameters/CNCB0]
"PortName"="COM5"

然后你可以用超级终端连接两个端口,看看是不是能互相通信

2 接下来用VB写AT信令的模拟器。我举一个最简单的一个例子,列出所有短信

Private Sub MSComm1_OnComm() 'OnComm事件处理新消息提示
Dim buffer As Variant
Dim Enter As String
Dim data As String
Dim iii As String
Enter = Chr$(13) + Chr$(10)
Select Case MSComm1.CommEvent
  Case comEvReceive
  'MSComm1.InputLen = 0
  MSComm1.InputMode = comInputModeText '接收内容的格式
  'Text1.text = MSComm1.InBufferCount & "|"
  buffer = MSComm1.Input
  Call delay(1)
  Text1.text = Text1.text & buffer
 
    If InStr(buffer, "AT+CMGF=0" & vbCr) Then   ' AT信令 模拟返回两条短信1 2
       data = "+CMGL: 1,0,,22" & vbCrLf
       data = indata & "0891683108100005F0040D91683125503956F90008404060313530230660A8597DFF01" & vbCrLf
       data = indata & "+CMGL: 2,0,,22" & vbCrLf
       data = indata & "0891683108100005F0040D91683125503956F90008404060313530230660A8597DFF01" & vbCrLf
       data = indata & "OK" & vbCrLf
       MSComm1.Output = data
    Else
    End If
   
   
  Case Else
End Select
MSComm1.InBufferCount = 0 '清空接收缓冲区
End Sub


3 新建一个发送按钮

Private Sub Command1_Click()
  MSComm1.Output = "AT+CMGF=0" + vbCr
End Sub

你可能感兴趣的:(J2EE)