| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 594 人关注过本帖
标题:有没做RS232通信的高手进!!
只看楼主 加入收藏
zidian
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-10-26
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
有没做RS232通信的高手进!!
两台电脑两台显示器,电脑通过RS232连接,RS232 Pin2,Pin3交叉连接,Pin5 为GND,编写程序使一个小球在一台电脑全屏幕运动,碰到边界反弹,碰到主机右边界穿过,在副机左边出现并按照远路径运动,碰到边界反弹,碰到左边界穿过,继续回到主机运动,如此反复:以下为我编写的两个程序,一个放主机,一个在副机,运行后只能来回穿屏幕一次,希望高手看看代码有何错误。。。
主机程序代码:
Dim x As Integer
Dim y As Integer
Dim m As Integer
Dim sk As String
Private Sub Command1_Click()
Randomize
Shape1.Left = Int(((Screen.Width - Shape1.Width + 1) - Form1.Left) * Rnd + Form1.Left) '自動生成小球位置
Shape1.Top = Int(((Screen.Height - Shape1.Height + 1) - Form1.Top) * Rnd + Form1.Top)  '自動生成小球位置
x = Int((200 - 150 + 1) * Rnd + 150)                                             '隨機生成小球X方向位移
y = Int((200 - 150 + 1) * Rnd + 150)                                              '隨機生成小球Y方向位移
m = Int((4 - 1 + 1) * Rnd + 1)                                                      '隨機生成運動方向
Text1.Text = x
Text2.Text = y
MSComm1.Output = CStr(x) & CStr(y)                                      '同時向副機端傳送X Y位移量
Shape1.Visible = True
Timer1.Enabled = True                                                   '打開timer1,小球在主機器端運動
End Sub
Private Sub Form_Load()
MSComm1.PortOpen = True
MSComm1.OutBufferCount = 0
MSComm1.InBufferCount = 0
Form1.Width = Screen.Width
Form1.Height = Screen.Height
Command1.Left = Screen.Width / 2 - Command1.Width / 2
Command1.Top = Screen.Height / 2 - Command1.Height / 2
Shape1.Visible = False
Timer2.Enabled = False
End Sub

Private Sub Timer1_Timer()
Form1.Refresh
If m = 1 Then
Shape1.Left = Shape1.Left + x
Shape1.Top = Shape1.Top - y
If Shape1.Left > Screen.Width - Shape1.Width And Shape1.Left < Screen.Width - Shape1.Width + x Then            '小球左邊到達 屏幕寬度-小球寬度 和屏幕寬度-小球寬度+X之間時
MSComm1.Output = CStr(1) & CStr(Shape1.Top)                     '傳送小球方向及垂直坐標給副機端
ElseIf Shape1.Left > Screen.Width Then                        '小球完全過邊界時
Timer2.Enabled = True                   '打開timer2,準被偵測接受副機器端效果過左邊界的運動方向和坐標
Timer1.Enabled = False                                                    '本主機器端小球運動暫停
ElseIf Shape1.Top < Form1.Top Then
m = 4
End If
ElseIf m = 2 Then
Shape1.Left = Shape1.Left - x
Shape1.Top = Shape1.Top - y
If Shape1.Left < Form1.Left Then
m = 1
ElseIf Shape1.Top < Form1.Top Then
m = 3
End If
ElseIf m = 3 Then
Shape1.Left = Shape1.Left - x
Shape1.Top = Shape1.Top + y
If Shape1.Left < Form1.Left Then
m = 4
ElseIf Shape1.Top > Screen.Height - Shape1.Height Then
m = 2
End If
ElseIf m = 4 Then
Shape1.Left = Shape1.Left + x
Shape1.Top = Shape1.Top + y
If Shape1.Left > Screen.Width - Shape1.Width And Shape1.Left < Screen.Width - Shape1.Width + x Then
MSComm1.Output = CStr(4) & CStr(Shape1.Top)
MSComm1.PortOpen = False
ElseIf Shape1.Left > Screen.Width Then
MSComm1.PortOpen = True
Timer2.Enabled = True
Timer1.Enabled = False

ElseIf Shape1.Top > Screen.Height - Shape1.Height Then
m = 1
End If
End If
End Sub

Private Sub Timer2_Timer()
If MSComm1.InBufferCount > 0 Then                                   'Comm1端口有數據即接受
sk = MSComm1.Input
m = Val(Mid(sk, 1, 1))                                              '將副機端運動方向讀出并保存留用
Shape1.Top = Val(Right(sk, Len(sk) - 1))                            '重新定位小球垂直坐標
Shape1.Left = Screen.Width - Shape1.Width
Shape1.Visible = True
Timer1.Enabled = True                                               '打開timer1小球開始運動
Timer2.Enabled = False
End If
End Sub

副机程序代码:
Dim x As Integer
Dim y As Integer
Dim m As Integer
Dim st As String
Dim sk As String
Private Sub Form_Load()
MSComm1.PortOpen = True
MSComm1.OutBufferCount = 0
MSComm1.InBufferCount = 0
Form1.Width = Screen.Width
Form1.Height = Screen.Height
Shape1.Visible = False
Timer1.Enabled = False
Timer2.Enabled = True                '打開timer2偵測接受端數據
Timer3.Enabled = False
End Sub

Private Sub Timer2_Timer()
If MSComm1.InBufferCount > 0 Then    '有數據即讀入
st = MSComm1.Input
x = Val(Mid(st, 1, 3))               '將主窗口隨機生成之X方向位移讀出并賦值給本窗口,留待後用
y = Val(Mid(st, 4, 3))                '將主窗口隨機生成之Y方向位移讀出并賦值給本窗口,留待後用
Text1.Text = CStr(x)
Text2.Text = CStr(y)
Timer3.Enabled = True
Timer2.Enabled = False               'timer2使用一次后關閉
End If
End Sub
Private Sub Timer1_Timer()
Form1.Refresh
If m = 1 Then                                                '運動方向及方式
Shape1.Left = Shape1.Left + x
Shape1.Top = Shape1.Top - y
If Shape1.Left > Screen.Width - Shape1.Width Then
m = 2
ElseIf Shape1.Top < Form1.Top Then
m = 4
End If
ElseIf m = 2 Then
Shape1.Left = Shape1.Left - x
Shape1.Top = Shape1.Top - y
If Shape1.Left > Form1.Left - x And Shape1.Left < Form1.Left Then   '本副機器端小球左邊到達過 左邊
                                                                     界 和 左邊界-X 之間時
MSComm1.Output = CStr(2) & CStr(Shape1.Top)                         '向主機端傳送過左邊界時的狀態及垂直
                                                                      坐標
ElseIf Shape1.Left > Form1.Width Then                                '本副機器端小球完全通過左邊界時
Timer3.Enabled = True                                               '打開timer3準備接受主機小球穿過主機
                                                                     右邊界時的方向和坐標
Timer1.Enabled = False
ElseIf Shape1.Top < Form1.Top Then
m = 3
End If
ElseIf m = 3 Then
Shape1.Left = Shape1.Left - x
Shape1.Top = Shape1.Top + y
If Shape1.Left > Form1.Left - x And Shape1.Left < Form1.Left Then    '本副機器端小球左邊到達過 左邊
                                                                     界 和 左邊界-X 之間時
MSComm1.Output = CStr(3) & CStr(Shape1.Top)                          '向主機端傳送過左邊界時的狀態及垂直
                                                                        坐標
ElseIf Shape1.Left > Form1.Width Then                                 '本副機器端小球完全通過左邊界時
Timer3.Enabled = True                                                 '打開timer3準備接受主機小球穿過主
                                                                       機右邊界時的方向和坐標
Timer1.Enabled = False
ElseIf Shape1.Top > Screen.Height - Shape1.Height Then
m = 2
End If
ElseIf m = 4 Then
Shape1.Left = Shape1.Left + x
Shape1.Top = Shape1.Top + y
If Shape1.Left > Screen.Width - Shape1.Width Then
m = 3
ElseIf Shape1.Top > Screen.Height - Shape1.Height Then
m = 1
End If
End If
End Sub

Private Sub Timer3_Timer()
If MSComm1.InBufferCount > 0 Then
sk = MSComm1.Input
m = Val(Mid(sk, 1, 1))                             '將主機端小球到達右邊界的運動方向賦值給本副機端
Shape1.Top = Val(Right(sk, Len(sk) - 1))           '將主機端小球到達右邊界的垂直坐標賦值給本副機端
Shape1.Left = Form1.Left - Shape1.Width            '副機端小球水平位置重新賦值
Text8.Text = Text8.Text & sk
Shape1.Visible = True
Timer1.Enabled = True                              '打開timer1,使小球按照接受到的方向和最新初始坐標
                                                  按照之前接受的X,Y位移運動
Timer3.Enabled = False                             '暫時關閉timer3
End If
End Sub
搜索更多相关主题的帖子: 通信 
2009-11-02 22:39
msgj
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:4
帖 子:190
专家分:914
注 册:2009-10-3
收藏
得分:20 
需要先将两个COM口握手
2009-11-06 16:27
msgj
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:4
帖 子:190
专家分:914
注 册:2009-10-3
收藏
得分:0 
= 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
2009-11-06 16:30
twh000
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-11-8
收藏
得分:0 
正是自己想学的东东,好!
2011-11-08 22:44
擦身而过
Rank: 1
等 级:新手上路
帖 子:3
专家分:3
注 册:2008-5-17
收藏
得分:0 
我也想学习这方面。用在PLC上
2011-11-22 16:28
快速回复:有没做RS232通信的高手进!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017767 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved