LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Help wirh RS232

I try to communicate with a RelaisCard with RS232.
After a hard start, i now can send my initstring

viCheckErr( viWrite (io, Buffer, 4, VI_NULL));

but i do not receive the correct answer with

viCheckErr( viRead (io, BufferRet, 4, VI_NULL));

I always got a framing error during transfer.

I also read some Postings here, all i found out, that maybe the BoudRate or Parity is wrong, but i dont know where to edit or change them.

Can someone help me ?
0 Kudos
Message 1 of 10
(5,228 Views)
Since you are using VISA functions, you must set characteristics of serial port using viSetAttribute. Examples of serial communications specific attributes are:

viSetAttribute (io, VI_ATTR_ASRL_BAUD, 9600);
viSetAttribute (io, VI_ATTR_ASRL_PARITY, VI_ASRL_PAR_NONE);
viSetAttribute (io, VI_ATTR_ASRL_DATA_BITS, 8);
viSetAttribute (io, VI_ATTR_ASRL_STOP_BITS, 1);

These set the serial communication to 9600 baud, no parity, 8 data bits, 1 stop bit. You can use other values to adapt to your device.

It's often necessary to add some small delay before reading the answer to let the device do its job: device documentation should tell you wether it's needed and how long to wait for each command.

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 2 of 10
(5,221 Views)
Thank you very very much for this perfect and fast answer. This is EXACTLY what i need. And after Testing it finally worked !!

I thank you so much !
0 Kudos
Message 3 of 10
(5,221 Views)
Ref: RS232   CVI
I am getting 9data bits to the computer serial port.  How I could read 9th. bit  in software control "manually"?
Thanks
0 Kudos
Message 4 of 10
(5,024 Views)
Hi Miso,
 
I'm not quite sure what you mean when you say "manually." Do you mean that your program reads when a user event, such as a button callback, occurs? If this is indeed what you mean, you simply need to add a button with a callback function that performs the serial read.
 
You can see two great examples of RS-232 communication in CVI in our shipping examples. The examples are located at <CVI>\samples\rs232 (where <CVI> is the directory where you have CVI installed). Specifically the Serial project is excellent. In this example, the ReadCallBack function shows you a way to read data using serial.
 
 
Matt M.
National Instruments

Message Edited by Matt Mueller on 11-13-2006 09:38 AM

0 Kudos
Message 5 of 10
(4,999 Views)
Actually it sounds like you're after a data byte size of 9 bits (as opposed to the 5, 6, 7, or 8 bits that OpenComConfig allows).  The CVI RS-232 library does not support a 9-bit byte size for serial communication.  The issue is really that much of the work of the serial protocol is handled by the hardware.  Each byte comes in a frame that can include a start bit and various parity and stop bits.  The serial hardware actually parses the frames and does parity checking, etc.  I don't know of any serial hardware offhand that supports a 9-bit byte size, and from what I have read, the Windows SDK functions do not really support 9-bit bytes either.

There may be some low-level, hacky ways to make it work, by manipulating the serial hardware use of a parity bit, but I have no clue how to begin implementing such a thing.  Maybe someone else in the community has an idea how to do 9-bit serial communication, but I think you'll have to venture outside the realm of CVI libraries to do it.

Here is a thread from the comp.arch.embedded newsgroup that discusses the issue.

Good luck.

Mert A.
National Instruments.
0 Kudos
Message 6 of 10
(4,992 Views)

Actually I have hardware what sends 8bit data and 9th bit  as start of the packet.  Each packet would contain few bytes of data. So, just only first byte's 9th bit would have logical Hi others bytes would have logical low. So 9bit serve to for alignment of the pockets.  Could I set-up on computer side as parity bit or mark or space, but I need way to read it  by CVI if I enable parity?

Question is How I can read parity bit of each byte in CVI?

0 Kudos
Message 7 of 10
(4,974 Views)
Hi Miso,

Unfortunately the CVI RS-232 library does not provide this ability.  It is a higher level interface, and thus does not give direct access to the various framing bits that are a part of the transmission protocol.  The packet delimiting scheme you describe is not standard RS-232, as RS-232 has no concept of packets and no special bit for marking a start byte.  If you used a lower level API, you might be able to use the parity bit for this purpose, but even then you might have difficulties preventing the receiving driver from actually using this bit for parity checking.

The one solution I can think of would be to try to configure your sending device to send an extra "start byte" at the beginning of each packet.  Instead of checking for the 9th bit to be set, you can compare each byte against a known "start byte" (which should ideally be a value that you know your device would never actually send as data).  This has the unfortunate side affect of increasing the size of each packet by one byte, which if your packets are only a few bytes long, can significantly slow down your overall transmission.

Mert A.
National Instruments
0 Kudos
Message 8 of 10
(4,956 Views)
One more question. If I choose standard  RS232 serial communication with 8bit data 1bit parity 1bit stop
How I can check parity error of the each incoming byte in CVI?
0 Kudos
Message 9 of 10
(4,936 Views)
Parity checking will be done automatically.  If one of the ComRd functions encounters a parity error, it returns kRS_GeneralIOFailure (-151).  Because this error can be returned for a number of different reasons, you should call GetComStat, which will then clarify whether a parity or some other error occurred.

if ((numRead = ComRd(port, ...)) < 0)
{
     if (GetComStat(port) & kRS_ParityError)
          // parity error
}

You can also register a callback function to be called whenever a line-status error (framing, overrun, or parity) error is encountered.  Just pass LWRS_ERR as the event mask in a call to InstallComCallback.  Within the callback, you would still want to call GetComStat to determine if the error was a parity error.

Note that unless you are reading one byte at a time, it is not possible to know which byte read had the parity problem.

Hope this helps.

Mert A.
National Instruments

Message Edited by Mert A. on 11-15-2006 10:24 AM

Message Edited by Mert A. on 11-15-2006 10:26 AM

0 Kudos
Message 10 of 10
(4,916 Views)