Read data from MySQL to PLC

dracekvo

Member
Join Date
Apr 2011
Location
home
Posts
17
Hi, I have problem

I tried load data from MySQL to PLC via VBscript

This is my code
Code:
im db_server
Dim db_port
Dim db_user
Dim db_pass
Dim db_name
Dim db_tabelle
Dim Sql
Dim Conn
Dim strConnectString
Dim rs
Dim vysledek


db_server= "127.0.0.1"
db_port= "3306"
db_user= "root"
db_pass= "root"
db_name= "archiv"
 
If SmartTags("data.b00") = True Then
    
    Sql = "Select * from test limit 1"
 
    Set Conn=CreateObject("ADODB.Connection")
               strConnectString = "DRIVER={MySQL ODBC 5.1 Driver};" _
               & "SERVER=" & db_server & ";" _
               & " DATABASE=" & db_name & ";" _
               & "UID=" & db_user & ";PWD=" & db_pass & "; OPTION=3"
    Conn.Open strConnectString                                    
    Set rs=CreateObject("ADODB.recordset")    
    rs.Open Sql, Conn          
    
    
    
    rs.MoveFirst
    Do While Not rs.EOF
        
        SmartTags("Pole.Carovy_kod") = rs("slovo")
        
    rs.MoveNext
    Loop
    
    Conn.Close  

    
    SmartTags("data.b00") = False
     
End If

I don't know how move data from rs("slovo") in tag.
SmartTags("Pole.Carovy_kod") is STRING[22]

SmartTags("Pole.Carovy_kod") = rs("slovo") tell me syntax error
 
Where is the SmartTags function defined? Can you verify that something simple like below this writes to your PLC?

SmartTags("Pole.Carovy_kod") = 1

As for your problems accessing the ADODB record set, this page describes the available methods. Maybe something like: rs.Fields.Item("slovo").

This type of functionality is simple to drag and drop with the Ignition SQL Bridge module without any painful code.
 
Last edited:
By the "smarttags" we can see that dracekvo is talking about Siemens WinCC Flexible RT.

I have a little bit of experience with reading ODBC/SQL in WinCC Flexible VBS scripts.
I read from SQL tables that had a fixed number of fields. I read all the fields in one go, and subsequently passed the values from the fields into the SmartTags. Something like this:

Code:
SQL_Table = "SELECT * FROM " & SmartTags("ODBC\strTableName_readDB")
'Reads the data records of the SQL table
 If SmartTags("ODBC\debug_message_on") Then ShowSystemAlarm SQL_Table
Set rst = conn.Execute(SQL_Table)
 
'Error routine
If Err.Number <> 0 Then
 ShowSystemAlarm "Error #" & Err.Number & " " & Err.Description
 Err.Clear 
 Exit Sub
End If
If SmartTags("ODBC\debug_message_on") Then ShowSystemAlarm SQL_Table
 
If Not (rst.EOF And rst.BOF) Then 
 'Compare if "End of File" or "Begin of File" exists, if not the pointer will be reset to the first entry
 
 rst.MoveFirst 'reset to 1st entry 
 
 'read from DB values into PLC tags
 SmartTags("ODBC\FromDB\cdr_type") = rst.Fields(0).Value
 SmartTags("ODBC\FromDB\shift_date") = rst.Fields(1).Value
' etc. etc.
 
 rst.close 
Else
 ShowSystemAlarm "Dat_No. is not available"
End If
It worked, but not 100% reliably !
It was reading cyclically, and if there was a problem (network problems for example), the code would not recover automatically from the error.
Because of this I ended up by using Inductive Automations FactorySQL (now called Ignition SQL bridge as Nathan mentions).
 
Jesper - thanks for the backup! I have some VBA programming experience, but not under WinCC.
 

Similar Topics

Hi Iam using monitouch hmi(V9 soft) with omron plc cj2m (CX programmer). In this I want to read a data from hmi to plc. The data was like...
Replies
0
Views
94
Thank you for any and all responses/help. I have an RSLogix 5000 v20 and a Cognex In-Sight v5.9 spreadsheet (8502P). I can not figure out how to...
Replies
0
Views
127
Dear Experts, Please, kindly guide me through how to read data from ABB PLC to S7-1200 Siemens PLC. I want to use Siemens S7-1200 PLC to read data...
Replies
1
Views
445
Hello, I want to send the data from Rslogix500 to Excel. I created a connection between RSlinx and Excel. The values in Excel automatically...
Replies
5
Views
1,178
Dear colleagues, I am reaching out for assistance with an issue I am having. I have a code that can successfully insert data from FactoryTalk...
Replies
6
Views
1,049
Back
Top Bottom