05. marts 2010 - 20:58Der 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.
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?
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. )
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.
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.
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.
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
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.
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:
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?
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?
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.
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
"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
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.
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.
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.
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"
"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.
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.
Synes godt om
Ny brugerNybegynder
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.