Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
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

VB
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



Thanks in advance
Posted
Updated 10-Apr-17 4:24am
v2
Comments
[no name] 6-Aug-15 14:05pm    
And your question is what? Why "they" used "MSCOMM1.OUTPUT to send data"? Probably because whomever ported this from VB6 was lazy and did not use the serial port class available in .NET like they should have.
[no name] 6-Aug-15 15:26pm    
I need/do to agree. V5.
[no name] 6-Aug-15 21:47pm    
As Wes Aday has implied a nasty example of code conversion. You can write this from scratch in C# in a few lines. The best learning exercise possible.

1 solution

do something like this:

SerialPort mySerialPort = new SerialPort("COM1");

mySerialPort.BaudRate = 250000;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.Two;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
mySerialPort.Open();

//do transfer struff here

mySerialPort.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900