Avatar billede op1 Nybegynder
05. marts 2010 - 20:58 Der er 29 kommentarer og
1 løsning

Oppdatering av Private Sub

Hvordan oppdaterer man Private Sub i VB i Access uten å måtte bruke hendelseprosedyrer på objekter i Forms?
I mitt tilfelle dreier det som å benytte bits over Modbus TCP. Disse har jeg i VB men jeg får dem ikke til å utføre Ptivte Subs i VB.
Avatar billede terry Ekspert
06. marts 2010 - 09:53 #1
Looking at your last question http://www.eksperten.dk/spm/900560

your expecting something which isnt possible
Avatar billede terry Ekspert
06. marts 2010 - 10:08 #2
Access uses a timer event to trigger code at specific intervals and that the answer which you were previoulsy given.
Without knowing anything about Modbus etc its not easy to make suggestions  on how to solve your problem.

Can you give a short description of what your trying to do and a little about Modbus please?
Avatar billede terry Ekspert
06. marts 2010 - 13:44 #3
what hardware are receiving data from?

More info please...
Avatar billede op1 Nybegynder
06. marts 2010 - 16:30 #4
Hei Terry
Tidligere spørsmål handlet om tidstyret kontroll av Private Subs.
Det jeg ønsker her er hendels-styrt kontroll av Private Subs utenom den kontroll man har ved å knytte Private Subs til objekter i Forms.

  Modbus er en industriell kommunikasjonsprotokoll som tidligere kun ble utført over seriell-port på PC/PLC. Da Ethernet kom utviklet man Modbus slik at den kan kommunisere over vanlig "kontornettverk". Derav navnet Modbus TCP. Der etter ble det utviklet Active-X som gjør at man benytte Modbus-TCP direkte inn i applikasjoner som støtter bruk av Active-X. Access støtter bruk av Active-X. Derfor kan man skrive kode i VB som gjør at man har dynamisk og kontrollerbar data-flyt mellom en controller-device som støtter Modbus-TCP og Access. Modbus TCP visker ut skillet mellom prosessdata og øvrig data-bruk.

  Nedenfor vises et eksempel på Private Sub som kontrolleres med Button i et skjema, men dette skulle gjerne vært utført med et bit som kommer fra min kontroll-device. ( Koyo05 PLC. )


Private Sub LoggVektverdi_Click()
   
If KvVeiingAvsl = 1 And VarekodeVeiedata > 0 Then

Me.DatoVeiedata = Date
Me.TidVeiedata = Time()
Me.VektverdiVeiedata = (V1510 * 100) + V1512

  End If


End Sub
Avatar billede terry Ekspert
06. marts 2010 - 18:06 #5
Hi op1

I understand that its a comms. protocol.

I must admit I dont quite understand the language your writing. Is it Norwegian?

Are you trying to execute the sub ( LoggVektverdi_Click) without having to click on the object LoggVektverdi?

You can execute by writing

LoggVektverdi_Click in code and as it is Private then it has to be within the forms module.

Do you want to execute the sub when a data bit is received by the ActiveX control?

If the ActiveX control is on the form then just writing

LoggVektverdi_Click  in one of the control events should work.
Avatar billede op1 Nybegynder
06. marts 2010 - 19:40 #6
Hi
Yes, I write in Norwegian.
"Are you trying to execute the sub ( LoggVektverdi_Click) without having to click on the object LoggVektverdi?"  Yes

"Do you want to execute the sub when a data bit is received by the ActiveX control?"  Yes, this is exactly what I mean.

You suggest: LoggVektverdi_Click  in one of the control events should work.
I do not understand. In this statement there is no connection between my Modbustag and the sub to be executed. LoggVektverdi is name of a button in my form.
I hoped I could replace LoggVektverdi with  the name of one of my Modbus-tags, but I cant make it working. May be it is outside the rules of VB? It is no problem to use the Modbustags to show the value, change colour etc.
Avatar billede terry Ekspert
06. marts 2010 - 20:55 #7
OK I think I understand what your after


Are you using the Winsock control?


I'll get back to you tomorrow, going to see some TV
Avatar billede op1 Nybegynder
06. marts 2010 - 21:13 #8
Hi
Winsoc controll? I dont know what it is. If it can help: The connection between my Modbus-device ( PLC ) is done by a Active-X, innstalled in my Access-application. This Active-X makes i possible to write certain code i VB to achive data to/from my PLC. Modbus operates with singel bits or 16 bits signed integers. In this case PLC operates as a server and Access as a client. Each time the Sub is executed data will be read/written from/to my PLC. Each Modbus-tag is named exclusively in VB-code. In the code-example above V1510 og V1512 are names on two 16 bits Modbustags.
Avatar billede terry Ekspert
07. marts 2010 - 09:19 #9
Winsock is also a protocol, I was thinking that ModBus maybe used it.

http://en.wikipedia.org/wiki/Winsock

Anyway if I have undertstood your problem your trying to run the code in a sub which is on a form.

As I mentioned previously Private functions and Subs can only be used in the module (form/code) in which it exits.

But you can make a Public function and call this from any module.

I assume that the ActiveX control has a code module wher ethere is a function which receives the data,is that correct?
I would think that is necessary otherwise I cant see how you would receive data.


Try this

Move the code from LoggVektverdi_Click into a new function named MyFunction.


Private Sub LoggVektverdi_Click()

  MyFunction

End Sub


Public function MyFunction()

     
If KvVeiingAvsl = 1 And VarekodeVeiedata > 0 Then

  Me.DatoVeiedata = Date
  Me.TidVeiedata = Time()
  Me.VektverdiVeiedata = (V1510 * 100) + V1512

End If


End Function

Remember to save the forms and modules you alter.

Now you can call MyFunction from other forms modules like this

forms!NamOfFormGoesHere.MyFunction
Avatar billede op1 Nybegynder
07. marts 2010 - 21:29 #10
Hi Terry

Thank you for suggestion, but I cant see your eksample makes differense. I made it working but still the function is not triggered by one of my Modbustags. Below I show you an eksample on how to get 8 bits from my Modbus-device. The names on the left side of = are the names beeing used in Access. Names on right side of = are names bound to the VB-syntax for my Active-X. What I want is one or more of theese to execute a Private Sub or a Function when they change their value from 0 to 1.

To call for data from Modbus-device the code goes like this:

Private Sub Form_Timer()

        Dim nRegData(6) As Integer    ' register data buffer (size as necessary)
        Dim nCoilData(7) As Integer  ' coildatabuffer
        Dim e As Integer              ' return error
        Dim S As String              ' string variable used for formatting reg. data
 
   
    e = SMTX1.MbReadCoilStatus(1, 3072, 8, nCoilData(0)) 'LeserC0-C7
    Tara = nCoilData(0)
    OpSpjOvBeh = nCoilData(1)
    VeiingPagar = nCoilData(2)
    LuSpjOvBeh = nCoilData(3)
    Brutto = nCoilData(4)
    OpSpjUndBeh = nCoilData(7)
Avatar billede terry Ekspert
08. marts 2010 - 09:50 #11
You have to appreciate that I know NOTHING other than what I see here about what you are doing so I am trying to get a picture of how your Access database and ModBus work together.


" I made it working but still the function is not triggered by one of my Modbustags"

What is a Modbustags? Where is that in your code?

If the Form_Timer() event and  LoggVektverdi_Click() event are on the same form then you dont need to make a public function. Are they on the same form?

If they are then you just need to add a line of code to call the sub
So after this line

OpSpjUndBeh = nCoilData(7)

add

LoggVektverdi_Click


Your original question was
"Hvordan oppdaterer man Private Sub i VB i Access uten å måtte bruke hendelseprosedyrer på objekter i Forms?"

and thats what I'm trying to asnwer not>

" What I want is one or more of theese to execute a Private Sub or a Function when they change their value from 0 to 1."

Once we can call a sub then I'm sure you can test if the data has changed from 0 to 1.
Avatar billede op1 Nybegynder
08. marts 2010 - 10:24 #12
Hi

Sorry for my inacurate question, but so far as I can see your answer still needs a action to be taken in one or another form-objekt, or am I wrong?

If the Form_Timer() event and  LoggVektverdi_Click() event are on the same form then you dont need to make a public function. Are they on the same form? 
Yes, they are.

This is an eksample on Modbustag: Brutto = nCoilData(4)


When I tried to solve the problem first, I wrote thise code:

Private Sub Brutto = 1()

  Me.DatoVeiedata = Date
  Me.TidVeiedata = Time()
  Me.VektverdiVeiedata = (V1510 * 100) + V1512

End Sub

Why does it not work? I can use the Modbustag Brutto to change colour, show the value, boolean math, but not to perform Privete Subs.  What am I doing wrong?
Avatar billede terry Ekspert
08. marts 2010 - 10:33 #13
"What am I doing wrong?"

Your not showing/telling me the whole picture :o)

Can you send me your dB? I know I cant test it but maybe seeing your code etc will help.

ekspertenATsanthell.dk

AT = @
Avatar billede op1 Nybegynder
09. marts 2010 - 08:05 #14
Hi Terry
Thank you for involving your self in my question. I can send you my DB but not as it looks like now. I have to clean it up a little bit. Too much experimental code. There is not much more picture to show, but the main thing I want to discuss is my very first question. According to this question we can imagine theese situations:
Is it possible to execute private Subs on different timeintervalls within the same form?
Is it possible to execute private Subs according to spesfic result from a calculation?
Is it possible to execute private Subs according to logic in boolean calculations?
Is it possible to execute private Subs on bits/bytes-values from other applications like PLC or other connected devices?

May be this is out of range for VB?
Avatar billede terry Ekspert
09. marts 2010 - 08:55 #15
Is it possible to execute private Subs on different timeintervalls within the same form?
A form has only one timer event so if you need more no.

"different timeintervalls"?
Well as I stated there is only one timer event but I would think it possible to do differnt things depending on an inteval. Now I havent tried anything like this but I cantsee why it shouldnt work.
Lets say the timer event fiores every 0,5 seconds. In a sub/function you can have what is called a static variable which you could use to increment by (for example) 1 each time the timer event is fired. So if you wanted to do something every 2 seconds you can test what the value is and if its 3 (test before incrementing) then we know that 2 seconds have gone. You can the rest to 0.
As I said thats just an idea which I havent tried.


Is it possible to execute private Subs according to spesfic result from a calculation?
Yes

Is it possible to execute private Subs according to logic in boolean calculations?
I dont see why not, yes

Is it possible to execute private Subs on bits/bytes-values from other applications like PLC or other connected devices?
???
Not sure how to answer this.
Does the ModBus ActiveX control have evenys which fire when it receives data from the PLC? If it does then YES.


If your software has to read data comming from the PLC then again YES.
So the answer seems to be YES.
Avatar billede terry Ekspert
09. marts 2010 - 09:01 #16
I have a brother who is kind of an expert with PLC things, he made this sofware www.easydnc.com this sofwatre uses seriel comms. but he has also made things which use the internet (network) using the Winsock control I mentioned.

It has an event which fires when it receives data, so it isnt necessary to use timer events.

This is whathe says>

You have the control on your form.

You have an event called "DataArrival" so if your winsock control was called ws1 your event would look like this:-

Private Sub ws1_DataArrival(HowManyBytes as long)

as soon as any other device sends information to your ip address/port number the code in the DataArrival  event will start executing. You don't need to use a timer, just put your code in that event and it'll start executing as soon as a message arrives.
The first thing you might want to do is read the value of HowManyBytes and that will tell you how long the message from the PLC is. Is the length what you expect ? If it is then just read the message into a string like this

MyString = ws1.GetData


End Sub
Avatar billede terry Ekspert
09. marts 2010 - 09:02 #17
I'm going out shortly, back around 12:00
Avatar billede op1 Nybegynder
09. marts 2010 - 12:04 #18
Hello Terry

Seems to be on the track now.


"Is it possible to execute private Subs according to logic in boolean calculations? I dont see why not, yes"
Could you please give me a short eksample, may be I can do it this way. I have not problems using Modbus-tags in simple boolean calculations.


" Does the ModBus ActiveX control have events which fire when it receives data from the PLC? If it does then YES "
Have sendt a mail to the company developed the Active-X to ask.
www.sapia-inc.com
Avatar billede terry Ekspert
09. marts 2010 - 12:19 #19
Give me a boolean calculation


I've just looked at the link www.sapia-inc.com


"Download a demo today and get on with your development.  Do what you do best and leave the Modbus to us."


There must be some example code in these demo modules, havent you looked at them?

Any that work in VB6 should work in Access.
Avatar billede op1 Nybegynder
09. marts 2010 - 12:53 #20
Hi

Eksample using Modbustag in boolean logic:

Private Sub LoggVektverdi_Click()
   
If KvVeiingAvsl = 1 And VarekodeVeiedata > 0 Then 
  Me.DatoVeiedata = Date
  Me.TidVeiedata = Time()
  Me.VektverdiVeiedata = (V1510 * 100) + V1512
End If
End Sub
'KvVeingAvsl = Modbusvalue ( One bit.)
'LoggVektverdi = Button in my form.
'VarekodeVeiedata = Value to be logged to my table with the other data beeing "Me". Make sure they are all inncluded.


Yes there are lots eksamples to get bits/bytes to/from the Modbus-device.
Avatar billede terry Ekspert
09. marts 2010 - 13:07 #21
Now I'm lost again.

You say
"Could you please give me a short eksample, may be I can do it this way. I have not problems using Modbus-tags in simple boolean calculations."

So I thought you wanted an example and I asked for "Give me a boolean calculation" so I could givg eyou an example.

Do you have wa´nt you want now?
Avatar billede op1 Nybegynder
09. marts 2010 - 15:17 #22
What I meant was en eksample from you how to write at boolean calculation that ends up i execution an Private Sub.

What you got from me was an eksample how I made a boolean tag from Modbus working in a boolean logic in a sub that ends up in a Me-command.

As you still see my private sub must be activated from a button on my form (LoggVektverdi)
You wrote above that Private Subs can be activated ( executed ) as a result from a boolean calculation. Executing a Sub is one thing  Result of the code written within a Sub i another.
Avatar billede terry Ekspert
09. marts 2010 - 15:50 #23
op1
I'm going to pass on this quesion shortly as I dont seem to be getting anywhere.

I cant teach you to code, sorry.

I have given an example of how to call a sub/function from your timer event, if you cant manage to put this into practice then I'm afraid its going to take far to long.

To call the sub  LoggVektverdi_Click
you wriet in your code (EXACTLY AS IT IS HERE)

LoggVektverdi_Click


You should try the examples you say are in the demo download

"Yes there are lots eksamples to get bits/bytes to/from the Modbus-device"
Avatar billede op1 Nybegynder
09. marts 2010 - 16:10 #24
Hi Terry

"You should try the examples you say are in the demo download"
I have no problem to make the demo-code working. I have the all the modbus-data I want innside my Access-application.
The challenge is how to use this data to perform VB-code automatically. As you understand I want to avoid manually actions so much as possible of security-reasons. The hole idea is to use modbus-data to execute Public/private Subs, not buttons in my forms.

  But thank you for involving your self. I will come back soon as I have found a solution.
The future is Ethernet as fieldbus.
Avatar billede op1 Nybegynder
09. marts 2010 - 18:34 #25
Hi Terry

Have found a solution. What do you think?

If If KvVeiingAvsl = 1 Then
LoggVektverdi_Click  ' Executes this sub.
End if

Has to be placed under: Private Sub Form_Timer()
Avatar billede terry Ekspert
09. marts 2010 - 19:28 #26
Nice :o)

Actualy close to what I've been trying to get to :o)
Avatar billede op1 Nybegynder
09. marts 2010 - 20:21 #27
Closing Who deserves the points?
Avatar billede op1 Nybegynder
09. marts 2010 - 20:25 #28
Terry should have because of his helpfulness.
Oh. I did something wrong here. I gave the points to my self.
Avatar billede terry Ekspert
09. marts 2010 - 20:48 #29
No problem, I have lots already. So many I dont know what to do with them. What can you do with them?? :o)
Avatar billede op1 Nybegynder
10. marts 2010 - 08:31 #30
It is probably a kind of high status having many points.
I'm not in the kategory beeing able to get many points.
Guess I'm better to ask than to answer. Have a nice day.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester