实例介绍
【实例简介】力创IO模块:EDA9061实例!
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | Public Class Frm_Main Private Structure KGLOut_Info ''' <summary> ''' 通道0的输出ON时间周期 S ''' </summary> ''' <remarks></remarks> Public CH0ON_T, CH0OFF_T As Int16 ''' <summary> ''' 通道1的输出时间周期 S ''' </summary> ''' <remarks></remarks> Public CH1ON_T, CH1OFF_T As Int16 ''' <summary> ''' 通道2的输出时间周期 S ''' </summary> ''' <remarks></remarks> Public CH2ON_T, CH2OFF_T As Int16 ''' <summary> ''' 通道3的输出时间周期 S ''' </summary> ''' <remarks></remarks> Public CH3ON_T, CH3OFF_T As Int16 ''' <summary> ''' 通道0的输出的实时时间戳 ''' </summary> ''' <remarks></remarks> Public CH0_CurrentIndex As Int16 ''' <summary> ''' 通道1的输出的实时时间戳 ''' </summary> ''' <remarks></remarks> Public CH1_CurrentIndex As Int16 ''' <summary> ''' 通道2的输出的实时时间戳 ''' </summary> ''' <remarks></remarks> Public CH2_CurrentIndex As Int16 ''' <summary> ''' 通道3的输出的实时时间戳 ''' </summary> ''' <remarks></remarks> Public CH3_CurrentIndex As Int16 ''' <summary> ''' 通道0的当前状态 0/1 ''' </summary> ''' <remarks></remarks> Public CH0_Status, CH1_Status, CH2_Status, CH3_Status As Int16 End Structure Private KGLOutInfo As KGLOut_Info Private Sub btnOpenCom_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles btnOpenCom.Click Try If (btnOpenCom.Text = "打开串口" ) Then If (SerialPort1.IsOpen = True ) Then SerialPort1.Close() SerialPort1.PortName = cmbComPort.Text SerialPort1.BaudRate = 9600 SerialPort1.DataBits = 8 SerialPort1.StopBits = System.IO.Ports.StopBits.One SerialPort1.Parity = System.IO.Ports.Parity.None SerialPort1.DtrEnable = True SerialPort1.RtsEnable = True SerialPort1.Open() btnOpenCom.Text = "关闭串口" GroupBox2.Enabled = True Else If (SerialPort1.IsOpen = True ) Then SerialPort1.Close() btnOpenCom.Text = "打开串口" GroupBox2.Enabled = False End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub SerialPort1_DataReceived( ByVal sender As System. Object , ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived End Sub Private Sub btnReadSet_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles btnReadSet.Click Dim bSend(0 To 5) As Byte bSend(0) = &H4C bSend(1) = &H57 bSend(2) = CInt (txtDAddress.Text) bSend(3) = &H1 bSend(4) = bSend(2) bSend(3) bSend(5) = &HD SerialPort1.Write(bSend, 0, 6) Do Until (SerialPort1.BytesToRead = 9) CommFunc.Delay(50) Loop txtComRece.Text = "" Dim n As Integer = SerialPort1.BytesToRead ''读取当前有多少数据可读。 Dim binary(n) As Byte ''新建内容。 SerialPort1.Read(binary, 0, n) ''读取 For Each b As Byte In binary txtComRece.AppendText(b.ToString( "X2" ) & " " ) Next Select Case binary(3).ToString( "X2" ) Case "03" cmbbtl.Text = "1200" Case "04" cmbbtl.Text = "2400" Case "05" cmbbtl.Text = "4800" Case "06" cmbbtl.Text = "9600" Case "07" cmbbtl.Text = "19200" End Select lblsblx.Text = binary(4).ToString( "X2" ) & binary(5).ToString( "X2" ) End Sub Private Sub btnReadCHState_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles btnReadCHState.Click Dim bSend(0 To 5) As Byte bSend(0) = &H4C bSend(1) = &H57 bSend(2) = CInt (txtDAddress.Text) bSend(3) = &H3 bSend(4) = bSend(2) bSend(3) bSend(5) = &HD SerialPort1.Write(bSend, 0, 6) Do Until (SerialPort1.BytesToRead = 7) CommFunc.Delay(50) Loop txtComRece.Text = "" Dim n As Integer = SerialPort1.BytesToRead ''读取当前有多少数据可读。 Dim binary(n) As Byte ''新建内容。 SerialPort1.Read(binary, 0, n) ''读取 For Each b As Byte In binary txtComRece.AppendText(b.ToString( "X2" ) & " " ) Next lblOCHState.Text = Convert.ToString(binary(3), 2).PadLeft(4, "0" ) lblICHState.Text = Convert.ToString(binary(4), 2).PadLeft(7, "0" ) If (lblOCHState.Text.Substring(0, 1) = "1" ) Then chkOCH3.Checked = True Else chkOCH3.Checked = False If (lblOCHState.Text.Substring(1, 1) = "1" ) Then chkOCH2.Checked = True Else chkOCH2.Checked = False If (lblOCHState.Text.Substring(2, 1) = "1" ) Then chkOCH1.Checked = True Else chkOCH1.Checked = False If (lblOCHState.Text.Substring(3, 1) = "1" ) Then chkOCH0.Checked = True Else chkOCH0.Checked = False If (lblICHState.Text.Substring(0, 1) = "1" ) Then chkDI6.Checked = True Else chkDI6.Checked = False If (lblICHState.Text.Substring(1, 1) = "1" ) Then chkDI5.Checked = True Else chkDI5.Checked = False If (lblICHState.Text.Substring(2, 1) = "1" ) Then chkDI4.Checked = True Else chkDI4.Checked = False If (lblICHState.Text.Substring(3, 1) = "1" ) Then chkDI3.Checked = True Else chkDI3.Checked = False If (lblICHState.Text.Substring(4, 1) = "1" ) Then chkDI2.Checked = True Else chkDI2.Checked = False If (lblICHState.Text.Substring(5, 1) = "1" ) Then chkDI1.Checked = True Else chkDI1.Checked = False If (lblICHState.Text.Substring(6, 1) = "1" ) Then chkDI0.Checked = True Else chkDI0.Checked = False End Sub Private Sub btnSCFSSZ_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles btnSCFSSZ.Click Dim bSend(0 To 7) As Byte txtComRece.Text = "" If (Val(txtmccd.Text) > 200) Then Exit Sub Dim TDSC As Int16 = 0 ''通道输出的状态串 If (chkSCFS3.Checked) Then TDSC = 1 * 2 * 2 * 2 If (chkSCFS2.Checked) Then TDSC = 1 * 2 * 2 If (chkSCFS1.Checked) Then TDSC = 1 * 2 If (chkSCFS0.Checked) Then TDSC = 1 bSend(0) = &H4C bSend(1) = &H57 bSend(2) = CInt (txtDAddress.Text) bSend(3) = &H5 bSend(4) = TDSC bSend(5) = CInt (txtmccd.Text) bSend(6) = bSend(2) bSend(3) bSend(4) bSend(5) bSend(7) = &HD SerialPort1.Write(bSend, 0, 8) Do Until (SerialPort1.BytesToRead = 5) CommFunc.Delay(50) Loop txtComRece.Text = "" Dim n As Integer = SerialPort1.BytesToRead ''读取当前有多少数据可读。 Dim binary(n) As Byte ''新建内容。 SerialPort1.Read(binary, 0, n) ''读取 For Each b As Byte In binary txtComRece.AppendText(b.ToString( "X2" ) & " " ) Next End Sub Private Sub btnReadSCFSSZ_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles btnReadSCFSSZ.Click Dim bSend(0 To 5) As Byte txtComRece.Text = "" bSend(0) = &H4C bSend(1) = &H57 bSend(2) = CInt (txtDAddress.Text) bSend(3) = &H6 bSend(4) = bSend(2) bSend(3) bSend(5) = &HD SerialPort1.Write(bSend, 0, 6) Do Until (SerialPort1.BytesToRead = 7) CommFunc.Delay(50) Loop txtComRece.Text = "" Dim n As Integer = SerialPort1.BytesToRead ''读取当前有多少数据可读。 Dim binary(n) As Byte ''新建内容。 SerialPort1.Read(binary, 0, n) ''读取 For Each b As Byte In binary txtComRece.AppendText(b.ToString( "X2" ) & " " ) Next End Sub Private Sub btnWSet_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles btnWSet.Click Dim bSend(0 To 7) As Byte txtComRece.Text = "" Dim TDH As Byte = 0 ''通道号 Dim TDSC As Int16 = 0 ''通道输出的值 If (rbtnAll.Checked) Then If (chkCHZ3.Checked) Then TDSC = 1 * 2 * 2 * 2 If (chkCHZ2.Checked) Then TDSC = 1 * 2 * 2 If (chkCHZ1.Checked) Then TDSC = 1 * 2 If (chkCHZ0.Checked) Then TDSC = 1 TDH = &HFF End If If (rbtnCH3.Checked) Then If (chkCHZ3.Checked) Then TDSC = 1 TDH = &H3 End If If (rbtnCH2.Checked) Then If (chkCHZ2.Checked) Then TDSC = 1 TDH = &H2 End If If (rbtnCH1.Checked) Then If (chkCHZ1.Checked) Then TDSC = 1 TDH = &H1 End If If (rbtnCH0.Checked) Then If (chkCHZ0.Checked) Then TDSC = 1 TDH = &H0 End If bSend(0) = &H4C bSend(1) = &H57 bSend(2) = CInt (txtDAddress.Text) bSend(3) = &H4 bSend(4) = TDH bSend(5) = TDSC If (rbtnAll.Checked) Then bSend(6) = bSend(2) bSend(3) bSend(5) Else bSend(6) = bSend(2) bSend(3) bSend(4) bSend(5) End If bSend(7) = &HD SerialPort1.Write(bSend, 0, 8) Do Until (SerialPort1.BytesToRead = 5) CommFunc.Delay(50) Loop txtComRece.Text = "" Dim n As Integer = SerialPort1.BytesToRead ''读取当前有多少数据可读。 Dim binary(n) As Byte ''新建内容。 SerialPort1.Read(binary, 0, n) ''读取 For Each b As Byte In binary txtComRece.AppendText(b.ToString( "X2" ) & " " ) Next End Sub Private Sub Frm_Main_Load( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles MyBase .Load cmbComPort.Items.AddRange(CommFunc.GetAvaliableSerialPorts()) End Sub Private Sub btnBeginOut_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles btnBeginOut.Click T_Out.Enabled = Not T_Out.Enabled If (T_Out.Enabled) Then KGLOutInfo.CH0_CurrentIndex = 0 KGLOutInfo.CH0ON_T = txtCHT0ON.Text KGLOutInfo.CH0OFF_T = txtCHT0OFF.Text KGLOutInfo.CH0_Status = 0 KGLOutInfo.CH1_CurrentIndex = 0 KGLOutInfo.CH1ON_T = txtCHT1ON.Text KGLOutInfo.CH1OFF_T = txtCHT1OFF.Text KGLOutInfo.CH1_Status = 0 KGLOutInfo.CH2_CurrentIndex = 0 KGLOutInfo.CH2ON_T = txtCHT2ON.Text KGLOutInfo.CH2OFF_T = txtCHT2OFF.Text KGLOutInfo.CH2_Status = 0 KGLOutInfo.CH3_CurrentIndex = 0 KGLOutInfo.CH3ON_T = txtCHT3ON.Text KGLOutInfo.CH3OFF_T = txtCHT3OFF.Text KGLOutInfo.CH3_Status = 0 btnBeginOut.Text = "启动定时开关量输出(ON)" Else btnBeginOut.Text = "启动定时开关量输出(OFF)" End If End Sub Private Sub T_Out_Tick( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles T_Out.Tick If (chkCHO0.Checked) Then KGLOutInfo.CH0_CurrentIndex = 1 lblCH0.Text = KGLOutInfo.CH0_CurrentIndex If (KGLOutInfo.CH0_CurrentIndex >= KGLOutInfo.CH0ON_T And KGLOutInfo.CH0_Status = 1) Then KGLOutInfo.CH0_CurrentIndex = 0 ''输出 KGLOutInfo.CH0_Status = 0 Call T_KGLOut(KGLOutInfo.CH0_Status, &H0) End If If (KGLOutInfo.CH0_CurrentIndex >= KGLOutInfo.CH0OFF_T And KGLOutInfo.CH0_Status = 0) Then KGLOutInfo.CH0_CurrentIndex = 0 ''输出 KGLOutInfo.CH0_Status = 1 Call T_KGLOut(KGLOutInfo.CH0_Status, &H0) End If End If If (chkCHO1.Checked) Then KGLOutInfo.CH1_CurrentIndex = 1 lblCH1.Text = KGLOutInfo.CH1_CurrentIndex If (KGLOutInfo.CH1_CurrentIndex >= KGLOutInfo.CH1ON_T And KGLOutInfo.CH1_Status = 1) Then KGLOutInfo.CH1_CurrentIndex = 0 ''输出 KGLOutInfo.CH1_Status = 0 Call T_KGLOut(KGLOutInfo.CH1_Status, &H1) End If If (KGLOutInfo.CH1_CurrentIndex >= KGLOutInfo.CH1OFF_T And KGLOutInfo.CH1_Status = 0) Then KGLOutInfo.CH1_CurrentIndex = 0 ''输出 KGLOutInfo.CH1_Status = 1 Call T_KGLOut(KGLOutInfo.CH1_Status, &H1) End If End If If (chkCHO2.Checked) Then KGLOutInfo.CH2_CurrentIndex = 1 lblCH2.Text = KGLOutInfo.CH2_CurrentIndex If (KGLOutInfo.CH2_CurrentIndex >= KGLOutInfo.CH2ON_T And KGLOutInfo.CH2_Status = 1) Then KGLOutInfo.CH2_CurrentIndex = 0 ''输出 KGLOutInfo.CH2_Status = 0 Call T_KGLOut(KGLOutInfo.CH2_Status, &H2) End If If (KGLOutInfo.CH2_CurrentIndex >= KGLOutInfo.CH2OFF_T And KGLOutInfo.CH2_Status = 0) Then KGLOutInfo.CH2_CurrentIndex = 0 ''输出 KGLOutInfo.CH2_Status = 1 Call T_KGLOut(KGLOutInfo.CH2_Status, &H2) End If End If If (chkCHO3.Checked) Then KGLOutInfo.CH3_CurrentIndex = 1 lblCH3.Text = KGLOutInfo.CH3_CurrentIndex If (KGLOutInfo.CH3_CurrentIndex >= KGLOutInfo.CH3ON_T And KGLOutInfo.CH3_Status = 1) Then KGLOutInfo.CH3_CurrentIndex = 0 ''输出 KGLOutInfo.CH3_Status = 0 Call T_KGLOut(KGLOutInfo.CH3_Status, &H3) End If If (KGLOutInfo.CH3_CurrentIndex >= KGLOutInfo.CH3OFF_T And KGLOutInfo.CH3_Status = 0) Then KGLOutInfo.CH3_CurrentIndex = 0 ''输出 KGLOutInfo.CH3_Status = 1 Call T_KGLOut(KGLOutInfo.CH3_Status, &H3) End If End If End Sub Private Sub T_KGLOut( ByVal TDSC As String , ByVal TDH As Byte ) Dim bSend(0 To 7) As Byte txtComRece.Text = "" 'Dim TDH As Byte = 0 ''通道号 'Dim TDSC As Int16 = 0 ''通道输出的值 'If (rbtnAll.Checked) Then ' If (chkCHZ3.Checked) Then TDSC = 1 * 2 * 2 * 2 ' If (chkCHZ2.Checked) Then TDSC = 1 * 2 * 2 ' If (chkCHZ1.Checked) Then TDSC = 1 * 2 ' If (chkCHZ0.Checked) Then TDSC = 1 ' TDH = &HFF 'End If 'If (rbtnCH3.Checked) Then ' If (chkCHZ3.Checked) Then TDSC = 1 ' TDH = &H3 'End If 'If (rbtnCH2.Checked) Then ' If (chkCHZ2.Checked) Then TDSC = 1 ' TDH = &H2 'End If 'If (rbtnCH1.Checked) Then ' If (chkCHZ1.Checked) Then TDSC = 1 ' TDH = &H1 'End If 'If (rbtnCH0.Checked) Then ' If (chkCHZ0.Checked) Then TDSC = 1 ' TDH = &H0 'End If bSend(0) = &H4C bSend(1) = &H57 bSend(2) = CInt (txtDAddress.Text) bSend(3) = &H4 bSend(4) = TDH bSend(5) = TDSC 'If (rbtnAll.Checked) Then ' bSend(6) = bSend(2) bSend(3) bSend(5) 'Else bSend(6) = bSend(2) bSend(3) bSend(4) bSend(5) 'End If bSend(7) = &HD SerialPort1.Write(bSend, 0, 8) Do Until (SerialPort1.BytesToRead = 5) CommFunc.Delay(50) Loop txtComRece.Text = "" Dim n As Integer = SerialPort1.BytesToRead ''读取当前有多少数据可读。 Dim binary(n) As Byte ''新建内容。 SerialPort1.Read(binary, 0, n) ''读取 For Each b As Byte In binary txtComRece.AppendText(b.ToString( "X2" ) & " " ) Next End Sub End Class |
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论