关于VB串口通讯MSCOMM控件的问题!急求程序!!!

使用RS232接口(pc机COM口)PC启动等待不少于10秒POS机发送"Q",PC收到返回“R”POS接收后发送最后一条记录的地址号(序号)PC收到发回传送记录的首地址... 使用RS232接口(pc机COM口)
PC启动等待不少于10秒
POS机发送"Q",PC收到返回“R”
POS接收后发送最后一条记录的地址号(序号)
PC收到发回传送记录的首地址
pos机按接收到的首地址开始发送10条记录
pc每接收完8个字符(8个字节数据)返回一个“C”,10条记录(160个字符)完成后返回“E”
PC收到“C”继续发送,收到“E”结束本次发送
有的话可以发到我的信箱:xuxiaoyu19851121@163.com,急等程序用!谢谢了!
谢谢你的回答!但是我还要发10进制的地址给POS机呢!如果POS机收不到那个地址就不知道从哪里开始传!怎么解决这个问题!
展开
 我来答
zdingyun
推荐于2016-09-13 · 知道合伙人软件行家
zdingyun
知道合伙人软件行家
采纳数:15429 获赞数:48171
1982年上海业余工业大学化工系毕业 现退休

向TA提问 私信TA
展开全部
Option Explicit
Dim strInput As String
Dim strSend As String
Dim sum As Integer

Private Sub Command1_Click()
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
End Sub

Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeText
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
Timer1.Interval = 100 '控制发送延迟时间
Timer1.Enabled = False
End Sub

Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0
strInput = strInput & MSComm1.Input
Text1 = strInput
If strInput = "Q" Then
strSend = "R"
strInput = ""
Timer1.Enabled = True
ElseIf Len(strInput) < 8 And strInput <> "R" Then
strSend = strInput
strInput = ""
'MSComm1.RThreshold = 8
Timer1.Enabled = True
Else ' Then
sum = sum + 8
Text2 = sum / 8
If sum >= 160 Then
strSend = "E"
'MSComm1.PortOpen = False '本句似乎可不用
sum = 0
Else
strSend = "C"
End If
strInput = ""
Timer1.Enabled = True
End If
End Select
End Sub

Private Sub Timer1_Timer()
If MSComm1.PortOpen = True Then
MSComm1.Output = strSend
Timer1.Enabled = False
End If
End Sub

补充:2进制接收
Option Explicit
Dim strInput As String
Dim strSend() As Byte
Dim sum As Integer
Dim strSj As String
Private Sub Command1_Click()
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
End Sub

Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary
'MSComm1.InputMode = comInputModeText
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
Timer1.Interval = 100 '控制发送延迟时间
Timer1.Enabled = False
End Sub

Private Sub MSComm1_OnComm()
Dim buffer() As Byte
Dim i As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0
buffer = MSComm1.Input
For i = 0 To UBound(buffer)
strInput = strInput & Right("0" & Hex(buffer(i)), 2)
Next
If Mid(strInput, 1, 2) = "51" Then
ReDim strSend(0)
strSend(0) = &H52
strInput = ""
Timer1.Enabled = True
ElseIf Len(strInput) < 16 And Mid(strInput, 1, 2) <> "51" Then
ReDim strSend(1 To Len(strInput) / 2)
For i = 1 To Len(strInput) Step 2
strSend((i + 1) / 2) = Val("&H" & Mid(strInput, i, 2))
Next
strInput = ""
Timer1.Enabled = True
Else ' Then
sum = sum + 8
Text2 = sum / 8
strSj = strSj & strInput '& vbCrLf
If sum >= 160 Then
ReDim strSend(0)
Text1 = strSj
'请进入数据处理代码,转换为ASCII字符号。
MSComm1.PortOpen = False '本句似乎可不用
strSend(0) = &H45
sum = 0
Else
ReDim strSend(0)
strSend(0) = &H43
End If
strInput = ""
Timer1.Enabled = True
End If
End Select
End Sub

Private Sub Timer1_Timer()
If MSComm1.PortOpen = True Then
MSComm1.Output = strSend
Timer1.Enabled = False
End If
End Sub
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式