Combine Bits And Send In One Word


Recommended Posts

Hi, Admin, I wrote some code to combine some bits in one word, then send it via COM port. I'm not sure if it's right, because can't test it now. Thanks

 

 

   private bitarray
   
   bitarray[0][16] = V.START[0]
   bitarray[0][15] = V.STOP[0]
   bitarray[0][13] = V.VIPLOT_START[0]
   bitarray[0][11] = V.RESET[0]
   bitarray[0][10] = V.LOCAL_WR[0]
   bitarray[0][9] = V.REMOTE_WR[0]
   bitarray[0][8] = V.CONTINUOUS_WR[0]
   bitarray[0][7] = V.PULSING_WR[0]
   bitarray[0][6] = V.REGUPULSE_WR[0]
   bitarray[0][4] = V.ENERGY1[0]
   bitarray[0][3] = V.ENERGY2[0]
   bitarray[0][2] = V.FORCE[0]
   bitarray[0][1] = V.ABORT[0]   
   
   Device1_40100[0] = From.Bit(bitarray)
 
note: bits are bitarray[][1-16], Device1_40100 is the word
Link to comment
Share on other sites

A few things:

 

1) bits are numbered from 0.  Its a confusing thing that Modbus numbers from 1 in the original specs, but internally it uses 0.  DAQFactory always numbers from 0 like any other programming language, so it should go from 0 - 15, not 1 - 16.

2) you don't need the [0] in the Device1_40100 line

3) read my post on common mistakes made by new users: 

 

http://www.azeotech.com/board/index.php?/topic/5249-some-basic-pointers/

Link to comment
Share on other sites

Hi, Admin

 

I didn't make it work today, my code is below,

 

 

   private bitarray
   VIPLOT_START[0] = 1
   bitarray[0][12] = VIPLOT_START[0]
   C_40500[0] = From.Bit(bitarray)
 
VIPLOT_START is a test device, D# 0, D to A, a unique chn#, timing 0, offset 0
C_40500 is has correct device and device number, I/O set register S16(6), correct channel address
 
My purpose, is to set the bit 12 of 40500 address inside external device. I checked, while I execute this code, VIPLOT_START[0] is 1, but value of C_40500[0] is 0 with no value change. Is there mistake inside? And I'm worrying one point, that if I set bit 12 in this word, which other bits inside also being changed, ie., bit 13, bit 14.
 
Many thanks
Link to comment
Share on other sites

I'm sorry. I don't think you understand what I meant.  So I created a little sample.  It handles 4 bits, but you can easily extend it to 16.  Also, you can rename the channels.  I just used bit0, bit1, etc. for clarity.  Here's what it does:

 

1) there are four test D/A channels for the bits.  Each has a unique channel #.  In the event for each of them, the UpdateWordOut() sequence is called.

2) I created a 5th test D/A channel just to represent the actual Modbus output.  In your case, this would be C_40500, but I made it a test channel because this is a sample.

3) In the startup sequence, I set a flag to tell the UpdateWordOut() function not to update, and then initialize the 4 bit channels.  Then I clear the flag and call UpdateWordOut().  This is to keep UpdateWordOut() from getting called four times when I initialize the bits.  Note that I have to initialize the bits for UpdateWordOut() to work.  They don't have to be initialized to 0 though.

4) finally, UpdateWordOut() looks at that flag, and if its not set, it does the code I mentioned, creating an array, then using from.bit() on it and sending the result to my WordOut channel.

 

The trick is using the event and calling UpdateWordOut().   That uses all the bit channels to construct the actual word.  So, you can change any one bit, and the others will remain the same.

bitOutput.ctl

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.