ni.com is currently experiencing unexpected issues.

Some services may be unavailable at this time.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Control ER-16 in Labwindows/cvi

I am trying to control the Relay board ER-16 in Labwindows/cvi , I have made a task in MAX but cannot call it to perform the desired function, I want to turn on and off the relay, I think it is not big of a task once you know how to do it and I am a novice. Any help will be really appreciated. I have one more question I need to send the following string "Atest Equipment" through the comport. It has to follow with a carriage return (x0d) Will string concetanate help? How to concetanate a CR with a string. One more thing when I need to read it will give me bytes and I need to read it for certain information. Any help is really appreciated
0 Kudos
Message 1 of 23
(4,436 Views)
While I cannot help you in controlling the relay board, I can give you some hint on serial comm.

To add a carriage return to a string, simply append "\r" to it. For example: ComWrt (1, "Atest Equipment\r", 16);

To read from the comport you can rely either on termination characters (if any) or on message lenght (if known). Study InstallComCallback function and check if you can use one of the two methods: to react on termination character pass LWRS_RXFLAG as Event mask parameter and the temination character in the Event Character field; to operate on message-lenght basis, use LWRS_RECEIVE as the mask and the number of characters as Notify Count field.

Last and simplest method: if the device attached to the com port is communicating on-demand (i.e. you issue a command and wait a response) you can simply wait for a desired time and look if there are characters on the input queue. In this case you can set a proper time-out with SetComTime.

In any case, once the response has been read in a buffer, you can use sscanf or Scan functions to interpret the response: the scanning will depend on the protocol the device is using in communicating.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 23
(4,415 Views)
I have to communicate in two modes, firstly send a string atestequipment with a carriage return. thanks for explaining that, then if the communication is established then the unit replies by sending a string of atest equipment. How can I look into that. Now after the communication is established. I need to then send 16 bytes (as gverned by the protocol for the board). The board then sends back 16 bytes giving the status of various outputs from the micro. Each byte consists of 8 bits, which can be represented by a hex number. Then each hex number is broken down into bits. Could you explain how to communicate with the bytes. What can I use for that.

With Regards
Mehar
0 Kudos
Message 3 of 23
(4,407 Views)
From what you say, it seems that the communication is Q&A type (you send a command and the equipmant responds to it). In this case I suggest you set a time-out limit for serial communication to a appropriate time for the device to respond, and after ComRd terminates verify if the amount of bytes received is equal to what expected: if not, either the device is off or there is a problem in communication. In both cases your application must react someway.

So, in pseudo-code (you'll have to detail a bit):

//----------------------------------------
// Configure the port for communication
//----------------------------------------
OpenComConfig
SetComTime

//----------------------------------------
// Establish communication
//----------------------------------------
ComWrt ("atestequipment\r")
ComRd (buf) // "Buf" is the string into which device response is stored
// Test lenght of response
if ( strlen (buf) < expected lenght) error routine

// Test response
it depends on equipment protocol
if the device responds with a fixed string, you can simply use
if (strcmp (buf, "expected string")) error string
if the protocol is more complicated or variable you'll have to break down the answer and test individual fields

//----------------------------------------
// Communicate with the equipment
//----------------------------------------
// Issue command
sprintf (buf, ..., ...) // Format comand to issue according to device protocol
ComWrt (buf)
ComRd (buf)

// Again, test response lenght
if (strlen (buf) < 16) error routine

// Test response
for (i = 0; i < 16; i++) { // For every byte
// Decode individual bits by testing with the "&" operator: if =0 bit off else bit on
bit0 = buf[i] & 0x1 ? 1 : 0
bit1 = buf[i] & 0x2 ? 1 : 0
...
bit4 = buf[i] & 0x10 ? 1 : 0
bit5 = buf[i] & 0x20 ? 1 : 0
...
}



Hope this helps
Roberto


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 4 of 23
(4,398 Views)
Roberto,

Thanks a lot for all your help. A little note on using sprintf

Is this the correct way to use it
sprintf(buf,buf1,byte1,byte2,........,byte16);

P.S.-buf is target string, buf1 is format string and sources to be bytes; what is the diff between format string and target string.

or would I have to convert each byte as string and then concetanate it.

Secondly when I receive the bytes from the equipment- and I want o look at a particular bit example 6th byte and 5th bit
can i just do

byte_6_bit5 = buf[6] & 0x1 ? 1 : 0

and know the status of that particular bit

All your help is being greatly appreciated

With regards
Mehar
0 Kudos
Message 5 of 23
(4,393 Views)
Hi Mehar,

sprintf is a powerful instruction but needs a lot of study to fully comprehend all that's able to do. I suggest you gave a look to some C-language book to deep into its characteristics; some years go CVI was distributed with a copy of Kernigham & Ritchie's manual: it's a pity that it's not still so today. Anyway...

sprintf: the target string is the buffer destined to reach the formatted data, while format string is a descriptor of the aspect that data must have when formatted. For example, while formatting double numbers I can specifiy field width, padding, number of decimals... a full description of all directives takes six pages on the manual!

In your case, since you are expected to send bytes each bit of them has a special meaning I suppose you are formatting command into some integer numbers to be converted to their ASCII equivalent. For example, if you must send in one byte the rightmost bit and the seventh from right (that is pattern 0100 0001) the numeric representation of these informations is number 65 (decimal) or 0x41, that correspond to ASCII character "A". To obtain that "A" you can use the "%c" directive in format string. To create a string with four bytes created this way and formatted you can use sprint (buf, "%c%c%c%c", byte1, byte2, byte3, byte4).

To simplify the notation, sprintf permits you to elaborate an array of integers with one directive only, but it's better that you find it on the manual.

Second question: you absolutely don't need to elaborate all bytes and bits to extract desired informations: you can simply get the bit you need leaving the rest of the string uninterpreted. One only thing is that arrays in C are zero-based, so your example must be modified this way: byte_6_bit5 = buf[5] & 0x10 ? 1 : 0 (bit 1 is the rightmost or least significant).

Good luck with your project
Roberto


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 23
(4,379 Views)
Thanks Roberto


1st question, What will be the formatting string(as %c corresponds to Ascii) if I am planning to use a hex instead of Ascii Characters, I mean if I am planning to use 0x41 instead of A. I think it is %x, if I am not mistaken. One more thing can I use concatenation. The reason I am asking this is - In a certain test, I might be just using a certain byte to see the equipment's response.


2nd Question - Thanks for letting me know that, I was actually considering the bytes to start from zero.


With Regards
Mehar
0 Kudos
Message 7 of 23
(4,378 Views)
Dear Mehar,

I am not sure to understand what you mean by "concatenation": can you detail a little more?

As per formatting with hex output you're right: %x it the directive to be used ("%#x" if you do want the "0x" prefix). Again, I'm not sure to understand why you want to format the string this way: sending "A" to an instrument is different from sending "41" or "0x41" which is 2 or 4 characters long. It depends on device protocol wether to accept one or the other representation, but if you want to manipulate all 8 bits in a byte a char is a easy and practical solution for it (provided this is what your device expects...).


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 8 of 23
(4,347 Views)
Roberto,

Thanks for your suggestion - Almost everything else is working - except that I could not get the communication

going after the initial talk (ATEST EQUIPMENT). It is somehow not writing the bytes as one string (consisting of

16 byytes)

Mehar
0 Kudos
Message 9 of 23
(4,344 Views)
Do you receive anyting from the device? Maybe the timeout you set is too short: test raising the time and put a breakpoint in the function to see what is been received (number of characeters in the input queue and recevie buffer).


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 10 of 23
(4,340 Views)