Sign in to follow this  
Followers 0
chas183

VB Script and DDE How do I write a value?

9 posts in this topic

I am currently trying to read data from several PLC'S at the same time. I would like to write a one to B141:0/4 then a 0. Basicly similate a push button when running the script. It would be used to select what shifts data would be collected. (1st 2nd and 3rd- yesterday 1st, 2nd and, 3rd) Currently the sheet that is attached is working for retrieval of data. Some how I broke the post clock and machine name function this morning. The sheet is an excell sheet that Bob Lightfoot made up to read and write to plc clocks. I have tried to alter the sheet for my own use. I know nothing about Visual Basic. Just using the example to collect data. Could someone have a look at what I might be doing wrong? I am sure the code is sloppy at best. Kindest regards, Chas Prouduction Counts.zip

Share this post


Link to post
Share on other sites
writing a value using VB to a specific plc is relatively simple. However, you'll need RSLinx Professional to support setting up DDE to an application that no AB. Last I checked its about $800. You say you are reading values already. What application are you using to do this? bob

Share this post


Link to post
Share on other sites
I am currently using RSLinx classic installed. I have the DDETopic already configured.

Share this post


Link to post
Share on other sites
Forgot to mention that the Excel sheet I attached is using VB script to get the data. If you open the sheet and click the VB editor you can see the code.

Share this post


Link to post
Share on other sites
So it works for read but not write. Ok do you have a topic called "write shift" in rslinx. I'd also be leary of the blank space in your topic name {ie. "write_shift" or "WriteShift" make better topic names than "write shift"}

Share this post


Link to post
Share on other sites
This is what I have for the write, The topic is the same for both read and write. Sub Forge_PLC_Clock_Write() 'Dimension Variables for DDE Command Parameters Dim RSIChan Dim DataFile, DataElement, DDETarget 'Dimension Variables for General Use Dim ProcessorNum Dim DisplayData Dim HourData, MinData, SecData, MonthData, DayData, YearData Dim DateData, TimeData Dim ScratchPad 'Establish a maximum number of Processors to Talk to Const ProcessorNumMax = 4 'Dimension Arrays for Access to Processors Dim DDETopic(ProcessorNumMax) Dim TopicDesc(ProcessorNumMax) Dim ProcType(ProcessorNumMax) 'Temporary Variable for Shell Command Return Code Dim ShellResponse 'Clear ScratchPad Sheet6.Select Cells.Select Selection.ClearContents Range("A1").Select 'Establish Initial Values 'Processor 1 Declaration DDETopic(0) = "191b" 'Replace with an actual Processor Name from Your Network TopicDesc(0) = "write shift" 'Replace with an Actual Meaningful Name from Your System ProcType(0) = "SLC" 'Enter the Actual Processor Type SLC / PLC / CLX 'Processor 2 Declaration DDETopic(1) = "" 'Replace with an actual Processor Name from Your Network TopicDesc(1) = "" 'Replace with an Actual Meaningful Name from Your System ProcType(1) = "PLC" 'Enter the Actual Processor Type SLC / PLC /CLX 'Processor 3 Declaration DDETopic(2) = "" 'Replace with an actual Processor Name from Your Network TopicDesc(2) = "" 'Replace with an Actual Meaningful Name from Your System ProcType(2) = "CLX" 'Enter the Actual Processor Type SLC / PLC /CLX 'Processor 4 Declaration DDETopic(3) = "" 'Can be left blank if not used TopicDesc(3) = "" 'Can be left blank if not used ProcType(3) = "" 'Can be left blank if not used 'More Processors Can be Declared Here if ProcessorNumMax Constant is Changed 'Uncomment following command to Get Time From Network if Available 'ShellResponse = Shell("Net Time \\IntranetTimeServerName /set /yes", vbMaximizedFocus) 'Write Time to Each Processor For ProcessorNum = 0 To ProcessorNumMax TimeData = Time DateData = Date 'open dde link If DDETopic(ProcessorNum) <> "" Then RSIChan = DDEInitiate("RSLinx", DDETopic(ProcessorNum)) Else GoTo Bypass End If 'Write Data to SLC DataFile = "N" DataElement = 7 DDETarget = DataFile & DataElement & ":60" 'LINE 200 Currently I can write the hour of the day to address of N7:60. I want to write a number 1 - 6 with a button or value entered. 'I also need to wait for 1 second for the data to update on the PLC side before stepping agian. ScratchPad = "[Prouduction Counts.XLSM]" & "Scratch!" & "A1" Range(ScratchPad).Value = Hour(TimeData) 'Range(ScratchPad).Value = Hour(TimeData) DDEPoke RSIChan, DDETarget, Range(ScratchPad) 'Post Minutes 'Select Case ProcType(ProcessorNum) 'Case "SLC" ' DataElement = 41 ' DDETarget = DataFile & ":" & DataElement 'Case "PLC" ' DataElement = 22 ' DDETarget = DataFile & ":" & DataElement ' Case "CLX" ' DataElement = 4 'This asssumes a variable Clock(6) of type Dint exists ' ' and are set by SOR XIC CLOCK_SET_FLAG SSV WALLCLOCKTIME ? DateTime CLOCK_SETTINGS[0] EOR in Logic ' DDETarget = DataFile & "[" & DataElement & "]" ' Case Else ' GoTo Bypass ' End Select 'ScratchPad = "[Prouduction Counts.XLSM]" & "Scratch!" & "B1" 'Range(ScratchPad).Value = Minute(TimeData) ' DDEPoke RSIChan, DDETarget, Range(ScratchPad) 'Post Seconds 'Select Case ProcType(ProcessorNum) ' Case "SLC" ' DataElement = 42 ' DDETarget = DataFile & ":" & DataElement ' Case "PLC" ' DataElement = 23 ' DDETarget = DataFile & ":" & DataElement ' Case "CLX" ' DataElement = 5 'This asssumes a variable Clock(6) of type Dint exists ' and are set by SOR XIC CLOCK_SET_FLAG SSV WALLCLOCKTIME ? DateTime CLOCK_SETTINGS[0] EOR in Logic ' DDETarget = DataFile & "[" & DataElement & "]" 'Case Else ' GoTo Bypass 'End Select 'Close the DDE Communications Channel for This Processor DDETerminate (RSIChan) Bypass: 'Repeat for Next Processor in List Next ProcessorNum 'Move to Home Position Sheet3.Select Range("B2").Select End Sub

Share this post


Link to post
Share on other sites
After a pretty quick read of your code, I recommend looking closely at the topic names, and use RSLinx DDE/OPC Active Topics/Items to monitor what addresses are being requested by the topic. Also, the data type of you worksheet cells comes into play. I am pretty sure the default data type will not be a bit in VBA, and I have never tried to write to a single bit, only to words, so others would have to comment on that procedure. I have had problems when trying to use a variable in certain parts of the DDEPoke command. Sometimes, it is useful to flush out the RSLinx Harmony files and recreate the topic manually to ensure typing accuracies, and Bobs comments about spaces in topic names is also a factor, but I believe there is a workaround. THe downloads section of the forum has some Excel examples you may want ot compare notes with.

Share this post


Link to post
Share on other sites
ShiftNumber = ShiftNumber + 1 ShiftNumber = Chr$(ShiftNumber) If ShiftNumber > 54 Then MsgBox "Value entered to high. Please enter a value from 1 to 6" & vbCrLf & "Please adjust Before Proceeding", vbOKOnly, "Warning" LoopTest = True GoTo LoopExit The code above is a start to enter a value in ASCII to put into an integer word.

Share this post


Link to post
Share on other sites
A little more awake now and re-read your posts and comments of others. here is what I gleaned , please correct my assumtions if theya re wrong. 1. Reading from PLC to Excel is working fine. 2. Writing from Excel to N7:60 is working fine. 3. Writing from Excel to B141:0/4 is miserable fail. If the above are correct then your problem is most likely not the code, but the fact that this type of DDE Write will balk at writing a single bit of a register. You may have to write 1,2,4,6,16,32... to manipulates bits in B141:0/4. This will mean -- Read B141:0 , add 16 and write it back to turn on bit /4 or read B141:0 if greater than or equal to 16 subtract 16 and write it back to turn off bit /4. Hope this makes sense. As was suggested I'd also check out some of the other samples. P.S. -- DDE is soon to be deprecated by Miscrosoft so plan to read up on OPC and learn it to get data between excel and plcs.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0