串行端口通信程序,用于向DMX 512接收器发送数据 [英] Serial Port communication program of sending data to DMX 512 receiver
问题描述
DMX 512接收器,我需要通过串口通信发送512行数据。当我需要以波特率250000发送数据时出现问题。然后我使用具有Getcomm状态和Set Comm状态的DCB Control块。然后我写writefile但我应该使用comPort.Write(串口comPort =新的串口)发送数据或WriteFile。这是我的下面的程序
我有一个VB .Net程序的串口通信程序将数据发送到DMX 512接收器,我需要转换成c#。但我很困惑,因为他们使用MSCOMM1.OUTPUT发送数据。
非常感谢您的帮助
DMX 512 receiver , i need to send data in 512 lines through serial port communication .The problem arises when i need to send data at a baud rate 250000 . Then i used DCB Control block with Getcomm state and Set Comm state . and then i writefile but should i use comPort.Write (Serial comPort = new Serial Port) to send data or WriteFile .This is my below program
I have a VB.Net program of Serial Port communication program of sending data to DMX 512 receiver which i need to convert into c# . But i am confused as they have used MSCOMM1.OUTPUT to send data .
Your help highly appreciated
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public data_array
Private Sub cmd_Start_Click()
setup_com_port
send_comm_data
End Sub
Private Sub cmd_Stop_Click()
If MSComm1.PortOpen = True Then
''MSComm1.PortOpen = False
End
End If
End Sub
Private Sub Form_Load()
data_array = Array(&H7, &H20, &H7)
Slider_Red = &H7
Slider_Green = &H20
Slider_Blue = &H7
End Sub
Private Sub setup_com_port()
MSComm1.CommPort = 2
MSComm1.Settings = "9600,N,8,2"
'MSComm1.InputLen = 0
' MSComm1.InBufferSize = 1024
' MSComm1.OutBufferSize = 1024
MSComm1.PortOpen = True
SetBaudRate MSComm1, 250000
End Sub
' Set baud rate using Win32 API.
' The PortOpen property should be set to True before calling.
' May raise the following errors:
' comPortNotOpen the PortOpen property has not been set to True
' comDCBError failed to read current state of the port
' comSetCommStateFailed failed to set new baud rate
Sub SetBaudRate(Com As MSComm, baud As Long)
Dim ComDcb As dcb
Dim ret As Long
' Check port is open
If Not Com.PortOpen Then
Err.Raise comPortNotOpen, Com.Name, _
"Operation valid only when the port is open"
Exit Sub
End If
' Get existing Comm state
ret = GetCommState(Com.CommID, ComDcb)
If ret = 0 Then
Err.Raise comDCBError, Com.Name, _
"Could not read current state of the port"
Exit Sub
End If
' Modify state with new baud rate
ComDcb.BaudRate = baud
' Set the new Comm state
ret = SetCommState(Com.CommID, ComDcb)
If ret = 0 Then
Err.Raise comSetCommStateFailed, Com.Name, _
"Could not set port to specified baud rate"
Exit Sub
End If
End Sub
Private Sub send_comm_data()
'com_break (10)
Do
com_break (5)
'DoEvents
Sleep (5)
MSComm1.Output = Chr$(0)
'DoEvents
'Sleep (1)
send_char_0
Sleep (10)
DoEvents
Loop
End Sub
Private Sub com_break(break_in_ms)
' Set the Break condition.
MSComm1.Break = True
' Set duration to 1/10 second - 100ms
'Duration! = Timer + (break_in_ms / 100) '0.1 = 100ms
' Wait for the duration to pass.
'Do Until Timer > Duration!
'' Dummy = DoEvents()
'Loop
' Clear the Break condition.
Sleep (1)
MSComm1.Break = False
End Sub
Private Sub send_char_0()
Dim strData As String
strData = ""
For i = 0 To UBound(data_array)
'' MSComm1.Output = Chr$(data_array(i))
strData = strData & Chr$(data_array(i))
Next
For i = 1 To 509
''MSComm1.Output = Chr$(50)
strData = strData & Chr$(50)
Next
MSComm1.Output = strData
7 DoEvents
End Sub
谢谢advance
Thanks in advance
推荐答案
( 0 )
' DoEvents
' 睡眠( 1)
send_char_0
睡眠( 10 )
DoEvents
循环
结束 Sub
私有 Sub com_break(break_in_ms)
< span class =code -comment>' 设置中断条件。
MSComm1.Break = True
' 将持续时间设置为1/10秒 - 100毫秒
' 持续时间! = Timer +(break_in_ms / 100)'0.1 = 100ms
' 等待持续时间到传递。
' 直到计时器>持续时间!
' 'Dummy = DoEvents()
< span class =code-comment>' 循环
' 清除中断条件。
睡眠( 1 )
MSComm1.Break = 错误
结束 Sub
私有 Sub send_char_0()
Dim strData As String
strData =
对于 i = 0 UBound(data_array)
' 'MSComm1.Output = Chr
(0) 'DoEvents 'Sleep (1) send_char_0 Sleep (10) DoEvents Loop End Sub Private Sub com_break(break_in_ms) ' Set the Break condition. MSComm1.Break = True ' Set duration to 1/10 second - 100ms 'Duration! = Timer + (break_in_ms / 100) '0.1 = 100ms ' Wait for the duration to pass. 'Do Until Timer > Duration! '' Dummy = DoEvents() 'Loop ' Clear the Break condition. Sleep (1) MSComm1.Break = False End Sub Private Sub send_char_0() Dim strData As String strData = "" For i = 0 To UBound(data_array) '' MSComm1.Output = Chr
(data_array(i))
strData = strData& Chr
(data_array(i)) strData = strData & Chr
(data_array(i))
下一步
< span class =code-keyword>对于 i = 1 至 509
' 'MSComm1.Output = Chr
(data_array(i)) Next For i = 1 To 509 ''MSComm1.Output = Chr
这篇关于串行端口通信程序,用于向DMX 512接收器发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!