Serial Communication Starting Point
Hardware: PXI/CompactPCI>>Controllers
Problem: Where can I learn about serial communication?
Solution: This document will provide you with the basic knowledge and tools required to understand serial communication.
- General Terminology and Concepts
- Loopback Test to Verify Hardware Operation
- Common Problems
- Virtual Instrument Software Architecture (VISA)
- Printing Reports & Error -37
- Modem Communication in LabVIEW
- User Support Newsgroups
- Advanced Troubleshooting of COM Port Data
- High Baud Rate Causes Freeze on Data Lines
- General Terminology and Concepts Back to Top
For a description of the general concepts of serial communication and terminology, see KnowledgeBase 1M9E1L6Q: Serial Communications General Concepts
- Loopback Test to Verify Hardware Operation Back to Top
There are three ways to verify the operation of the serial port: LabVIEW, HyperTerminal and LabWindows/CVI. All perform a loopback test on the serial port by shorting out the Transfer and Receive pins on the cable connected to that port. Step 1 describes the procedure for shorting the pins and is the first step to all three methods. The LabVIEW procedure is listed below and both the HyperTerminal and LabWindows/CVI procedures are linked at the end of the page. It is recommended to use the following procedure if you are using LabVIEW.
- Connect a cable to your serial port. The most common port is RS-232 with either a 9-pin or 25-pin (DB-9 or DB-25) connector. On the cable, short lines 2 and 3 together. This will connect the Transfer line coming out of the computer to the Receive line going in. Once shorted, the top row of the DB-9 cable should look like this [1 2=3 4 5].
On an RS-485 port, voltages are referenced differentially. Therefore, you must short TXD+ and RXD+ together and TXD- and RXD- together (pin 4 to 8 and pin 5 to 9 on a DB-9 cable; pin 1 to 5 and pin 2 to 4 on a Combicon connector; pin 2 to 6 and pin 3 to 7 on modular jack connector). Make sure your software is configured in 4-wire mode.
Note: Refer to Appendix A of "Serial Hardware and Software for Windows User Manual" (linked below) for more information on the different type of connectors.
- For HyperTerminal or LabWindows/CVI, follow the links at the end of the page.
- For LabVIEW, open the shipped example program entitled "Serial Communication.vi". Depending upon which version of LabVIEW you are running, you can find this example in different locations:
- In LabVIEW 7.1 and later, go to Help » Find Examples » Hardware Input and Output » Serial » Basic Serial Write and Read.vi
- In LabVIEW 6.1, go to Help » Find Examples » Hardware Input and Output » Serial » Serial Communication VI.
- In LabVIEW 6.0, go to Help » Examples » Search Examples » I/O Interfaces Examples » Serial Communication Examples » Serial Communication VI.
- In LabVIEW 5.1 and 5.0, go to Help » Search Examples » I/O Interfaces Examples » Serial Communication Examples » Serial Communication VI.
- If you are running LabVIEW 4.0 or below, follow the link below to Developer Zone Example: Loopback Test for Serial Port.
Now use this example VI to write the sample string in the Write String box to the serial port, and then read it back in the Read String box. Before you run the VI, make sure you select the correct COM port/resource name.
- If this works correctly and the string of words is read back in, the serial port hardware works. Most serial port communication problems occur in the software concerning timing of read/write calls or configuration of the serial port.
- Common Problems Back to Top
Most problems are in the software, not the hardware. Always use example programs as a reference point for ways to wire up the diagram panel. Use the sample VIs that are shipped with LabVIEW under Help » Search Examples or from the VIs located in the Example Programs Database that is linked at the end of the page.
- Port Referencing: In LabVIEW, ports are numbered starting at zero. Therefore port 0 in LabVIEW is COM 1 on a PC.
- PC: 0 = COM1, 1= COM2, etc.
- Mac: 0 = Modem, 1 = Printer, etc.
- Sun: 0 = ttya, 1 = ttyb, etc.
- Timing In Program, Hanging: Many times a read operation is performed on a port that causes the Serial Port Read VI to stop running and wait or "hang". This can happen if you perform a read operation and the program specifies a certain number of bytes that the Serial Port Read VI is supposed to receive. The Serial Port Read VI was told to get a designated number of bytes, and if there are not that many bytes in the serial port receive buffer, it will wait until more bytes are written to the serial port. If no more bytes are written to the serial port to complete the operation, the program will hang and wait indefinitely. This can be eliminated by using the Serial Read with Timeout VI that is located under Help » Search Examples » Serial Communication. Another method is to use the Bytes At Serial Port VI to check the port and see how many bytes are in the buffer. Wiring the result as the input for the number of bytes to read ensures that the read operation will complete. Look at the examples under Serial Communication in LabVIEW for an excellent starting point.
- Timing In Program, Incomplete Transfer: Another common problem occurs when a read operation is done on the serial port and it does not receive the complete string of information that it expected. This may happen when you have a read operation happening before the write operation has time to complete. This could be resolved by using a Sequence structure or by using data flow dependency. Using a Sequence structure with the read after the write will make sure the data is completely written first. Data flow dependency involves wiring a connection (usually error cluster) to the read VI after (downstream from) the initial write VI.
- Instrument's Termination Character: Serial communication can be very tricky when trying to communicate with devices, because the serial port will put out exactly what it is programmed to. This means you must know and program the exact sequence of code, including termination characters, to ensure that the instrument operates properly. Common problems occur in not using the termination character required by the instrument or by leaving an extra space/character in the instruction string. There is a new standard for communicating with devices in LabVIEW called Virtual Instrument Software Architecture (VISA). VISA is a single interface library for controlling VXI, GPIB, RS-232 and other types of instruments. If you are having problems with serial I/O, VISA is a great place to help eliminate termination character errors and to make your code reusable and interchangeable with devices in the future.
- Virtual Instrument Software Architecture (VISA) Back to Top
VISA is a single interface library for controlling VXI, GPIB, RS-232 and other types of instruments on all LabVIEW platforms. VISA is a standard endorsed by the VXIplug & play Systems Alliance, which includes more than 35 of the largest instrumentation companies in the industry. The VISA standard unifies the industry to make software interpretable and reusable over time, regardless of instrument I/O operation. NI-VISA is a driver that is available from our web site (see the Software Downloads link, below). For help with VISA questions, see the link to the VISA Support Page, below.
- Printing Reports & Error -37 Back to Top
LabVIEW 5.1 and above for Windows include Report Generation VIs that allow you to send text to the printer. For an example of how to use the Report Generation VIs, select Help»Search Examples»Advanced»Report Generation. For printing with serial communication and earlier versions of LabVIEW, see the link below.
If the printer is connected to a local parallel port, such as LPT1 (or LPT2, etc.), you can use the Serial Port Write VI. For more information, see the "Serial Port VIs" chapter in your LabVIEW documentation.
If the printer is a network printer, you cannot use the Serial Port Write VI; you must use other methods to print, such as one of the following:
- Use Programmatic Printing of a subVI to programmatically print a string. Select "Print VI When VI Completes Execution" in VI Setup of the subVI.
- Use the System Exec VI to programmatically print a string by using Notepad.exe. The string to use in the System Exec VI could be: Notepad.exe /p c:\junk.txt, where junk.txt is the name of an ASCII text file containing the text to be printed.
- For problems with Error -37 follow the link below to check the settings in the labview.ini file.
- Modem Communication in LabVIEW Back to Top
Modem communication in LabVIEW is an operation that is well supported by companies in the Alliance program. These companies are recommended by National Instruments because of their quality work.
- Developer Exchange Forum Back to Top
The National Instruments Developer Exchange is a powerful discussion forum offering full-text search, e-mail subscription features, and much more. Take advantage of the expertise of your fellow scientists and engineers by browsing, searching, or posting a question in this community.
- Advanced Troubleshooting of COM Port Data Back to Top
Advanced users who want to analyze the exact bits being transferred over the serial port to detect differences between programs (for example, LabVIEW vs. HyperTerminal), can use a program called Portmon. Portmon is provided by Sysinternals, whose web site provides you with advanced utilities, technical information, and source code related to Windows 9x and Windows NT/2K internals.
- High Baud Rate Causes Freeze on Data Lines Back to Top
An issue that could come up is if you initialize a port that is consistently receiving high speed (115200 bps or higher) serial communication, the data lines could freeze and you could see 0 bytes read error. If you first flush the buffer then initialize the port, you will not see a freeze on your data line.
Related Links: Product Manuals: Serial Hardware and Software for Windows User ManualDeveloper Zone: Instrument DriversDeveloper Zone Example: Loopback Test for Serial PortDeveloper Zone: Developer Exchange Discussion ForumsSupport: Serial Products SupportSupport: Verifying a Serial Port, HyperTerminal Test Support: Verifying a Serial Port, LabWindows/CVI TestDrivers and Updates: All VersionsKnowledgeBase 1M9E1L6Q: Serial Communications General ConceptsKnowledgeBase 0ISAJ838: I Receive Error -37 When Writing to the Parallel Port (LPT1) in LabVIEW Support: Instrument Drivers (IVI, VISA) SupportSysinternals Website
Attachments:
Report Date: 06/10/1999
Last Updated: 02/11/2009
Document ID: 1M9D8H6Q
|
|