Sending Commands From Excel to a Device

Note: For a list of DDE commands, check out the support article listing WinWedge DDE Commands.

WinWedge makes it very easy for Excel to communicate with the device. This isn't limited to simply logging data from the device to Excel, but WinWedge also enables Excel to send data directly from VBA using WinWedge's SENDOUT and SEND DDE Commands

Sending a Command to the Device using SENDOUT DDE Command

The following VBA subroutine sends a string of control codes out the serial port by issuing the DDE command “[SendOut()]” to WinWedge. In this example, we're sending four characters: an ASCII 27 (an ESC character), a capital 'P', and a carriage return and line feed characters (ASCII 13 and 10).

 Sub SendEscapeP()
   Dim Chan As Long
   Chan = DDEInitiate("WinWedge", "COM1")
   DDEExecute Chan, "[SENDOUT(27,'P',13,10)]" 
   DDETerminate Chan
End Sub

Sending a String to the Device using SEND DDE Command

The following subroutine shows how to send a text string passed as an argument to the routine.

 Sub SendText(StringToSend$)
   Dim Chan As Long
   Chan = DDEInitiate("WinWedge", "COM2")
   ' The following line concatenates the SEND command with the data
   ' to be sent
   DDEExecute Chan, "[SEND(" & StringToSend$ &")]"
   ' The & operator is the Excel string concatenation operator thus we
   ' are concatenating the three strings "[SEND(" , the data in the
   ' string variable StringToSend$ and ")]". Thus if StringToSend$ 
   ' ="ABC" then the command that is sent to WinWedge would
   ' be: [SEND(ABC)]"
   DDETerminate Chan
End Sub

The following subroutine sends a column of data out the serial port starting in the currently active cell. After each cell's data is sent it reads the cell directly below the current cell. If the cell contains no data then it stops otherwise it continues sending until it hits an empty cell.

 Sub SendCells() 
   ' open a link to winwedge on com1
   chan = DDEInitiate("WinWedge", "COM1")
   ' starting offset from current cell is 0:
   MyPointer = 0
   ' put string from current cell in variable x$
   x$ = ActiveCell.Offset(rowOffset:=MyPointer, columnOffset:=0).Value	

   ' while the length of the string is not 0
   While Len(x$) 
      ' send the string out the port followed by a carriage return
      ' DDEExecute chan, "[SENDOUT(' " & x$ & " ',13)]"
      ' point to the next cell down
      MyPointer = MyPointer + 1
      ' get next cell
      x$ = ActiveCell.Offset(rowOffset:=MyPointer, _
      columnOffset:=0).Value
      ' loop until we reach an empty cell
   Wend

   'Close DDE Channel
   DDETerminate chan 
End Sub

The following subroutine demonstrates how to send the contents of any string variable out the serial port – even strings that contain control characters or non printable ASCII characters. The string to be sent out the serial port is passed as an argument to the SendString subroutine.

 Sub SendString(StringToSend$)
   ' The following loop reads the StringToSend$ variable and generates
   ' a string containing the ASCII values for each character in the
   ' string separated by commas. This resulting string is then used as
   ' the argument to the SENDOUT command below. Ex: if the variable
   ' StringToSend$ = "ABC" then the variable Arg$ will be "65,66,67"
   ' See the syntax of the SENDOUT command in the Wedge users manual
   ' for details.

   For x = 1 To Len(StringToSend$)
      Arg$ = Arg$ + LTrim$(Str$(Asc(Mid$(StringToSend$, x, 1))))
      If x <> Len(StringToSend$) Then Arg$ = Arg$ + ","
   Next

   channelNumber = Application.DDEInitiate("WinWedge", "COM2")
   ' The following line concatenates the SENDOUT command with the data
   ' to be sent
   Application.DDEExecute channelNumber, "[SENDOUT(" & Arg$ &")]"
   ' The & operator is the Excel string concatenation operator thus we
   ' are concatenating the three strings "[SENDOUT(" , the data in the
   ' string variable Arg$ and ")]". Thus if StringToSend$="ABC" then
   ' the command that is sent to WinWedge would be:[SENDOUT(65,66,67)]"
   ' - (the ASCII values for chars A, B and C are 65, 66 and 67)
   Application.DDETerminate channelNumber
End Sub

The following subroutine demonstrates how you would call the above routine passing it the contents of a spreadsheet cell thus sending the cell contents out the serial port.

 Sub TestSendString()
   X$ = Sheets("Sheet1").Cells(1, 1).Value
   SendString(X$)
End Sub

The following Excel VBA subroutine demonstrates how to transmit data out the serial port by “DDEPoking” to the WinWedge OutputBuffer DDE item.

 Sub PokeData()
   ' open a DDE channel
   chan = DDEInitiate("WinWedge", "COM2")
   ' poke the word "Hello" followed by a carriage return
   '(ASCII 13) to the outputbuffer
   DDEPoke chan, "OutputBuffer", "Hello" + Chr$(13)
   ' close the channel
   DDETerminate chan
End Sub

Contact Us