Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   Communications (http://www.xtremevbtalk.com/communications/)
-   -   Frequently Asked COMMUNICATION Questions.... (http://www.xtremevbtalk.com/communications/37689-frequently-communication-questions.html)

BillSoo 08-27-2002 02:00 PM

Frequently Asked COMMUNICATION Questions....
 
1) Where can I find more info about the winsock control? - Try here:
winsockvb.com on archive.org

2) Where can I find more info about the Serial port? - This is a good place to start:
http://www.lvr.com/serport.htm

3) Where can I find more info about the Parallel port? - Try this site:
http://www.lvr.com/parport.htm

4) How can I control the individual lines of a serial/parallel port? - You need to use a 3rd party control that allows you to use INP and OUT commands (these commands are in QBasic but not VB). You can find INPOUT32.DLL at www.lvr.com which gives you access to these commands.

BillSoo 11-29-2003 08:23 AM

1 Attachment(s)
I've attached the INPOUT32.DLL zip from Logix4u. This zip contains the DLL, the source code, some examples, documentation etc. This should help get you going with writing directly to the parallel or serial ports.

BillSoo 09-15-2004 10:44 PM

Some links to Tutorial articles:
Serial Port communications
http://www.xtremevbtalk.com/tutors-corner/163212-basic-serial-port-communications.html

BillSoo 05-13-2005 04:15 AM

Setting the Baud Rate directly with INPOUT32.dll
 
Sometimes you need to set a baudrate that mscomm does not support. If you are lucky, then you can use the API and use getCommState and setCommState to set the baud rate to one of the predefined rates in Windows. But if your baud rate is not on the list then what?

Well, you might be able to use INPOUT32.dll (or similar product) to give you the INP and OUT functions that used to be available in QBasic. With these functions, you can address the port directly and change its divisor value, thereby changing its baud rate.

First, you should understand what the divisor is. The comm port has a crystal that oscillates at 115200 hz. This means that the fastest possible clock speed you are going to get will be 115200. This means that it is firing evey tick of the clock. For any other baud rate, the comm port simply waits n ticks before it fires. So the next fastest baud rate is 57600 since that is 115200/2, then 115200/3 or 38400. The divisor is the integer by which the base clock rate (115200) is divided to get the baud rate.

OK. To talk to the port, we need to know it's address. Normally, the base address of COM1 is &H3F8 and COM2 is &H2F8 but this is not always the case. You can check this by using the System applet in the control Panel to check the resources used by your com ports. It will tell you the address ranges it uses. Whatever the base address is, we will call it BaseAddr.

Next, we need to get INPOUT32.dll. You can find this above in the FAQ. All you need to do is copy INPOUT32.dll to your system directory and then add the following function declarations to your code:
Code:
Private Declare Function Inp Lib "inpout32.dll" _ Alias "Inp32" (ByVal PortAddress As Integer) As Integer Private Declare Sub Out Lib "inpout32.dll" _ Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer)

Now add the following code:
Code:
Public Sub SetBaudDirect(ByVal BaseAddr As Integer, ByVal BaudRate As Long) Dim divisor As Integer Dim oldLCR As Integer Dim LCRaddr As Integer Dim divisorMSBaddr As Integer Dim divisorLSBaddr As Integer Dim msb As Integer Dim lsb As Integer divisorLSBaddr = BaseAddr divisorMSBaddr = BaseAddr + 1 LCRaddr = BaseAddr + 3 divisor = 115200\BaudRate msb = divisor \ 256 lsb = divisor MOD 256 oldLCR = InP(LCRaddr) 'save the old latch register values Out LCRaddr, (oldLCR Or &H80) 'make dlab=1 Out divisorMSBaddr,msb 'set the MSB of the divisor Out divisorLSBaddr,lsb 'set the LSB of the divisor Out LCRaddr, oldLCR 'restore the old latch values End Sub

One final caveat....on some occasions I found that after setting the baudrate, Windows set it back automatically. This did not happen all the time, but it did happen sometimes. So basically, I got into the habit of setting the baud rate immediately before I needed to use the port.

BillSoo 05-31-2005 12:17 AM

It is not possible to "spy" on a serial or parallel port being used by another application with pure VB. I *suspect* that you could do it in another language, but not in VB...

On the other hand, you CAN build a cable that "copies" the data flow to a SECOND port and then simply read that.

webbone found this link that describes how such a cable can be built:
http://www.lammertbies.nl/comm/cable...y-monitor.html

Here are the main points:
1) you cannot read a port being used by another app
2) you can build a "Y" cable that splits the data and sends a copy of it to a 3rd port
3) you can read EITHER the Rx OR Tx data with a single port but not both at once
4) if you need to see both Rx and Tx data at the same time, you need another spy port
5) you could build a cable that can switch between Rx and Tx data via a simple toggle switch

BillSoo 07-17-2005 02:08 AM

Update to Direct Baud Rate Setting
 
I got a message from mkaras that pointed out some errors in my above post about setting the baud rate directly. Rather than rewrite it, I've decided to copy his message here:

Quote:

BillSoo:

In your string of postings in the CommTutorials section you had a piece on the setting of the baud rate for the COMM port by direct means using the INPOUT32 dll.

In this article you stated that the UART baud rate crystal was 115200 Hz and thus the fastest baud rate was thus 115200 bps (bits per second).

This is not actually true. From early PC days legacy COMM ports have used a crystal or oscillator frequency of 1.8432 MHz. A typical UART, including the PC UART, uses a clock rate that is 16 times faster than the actual baud rate. The divisor register in the PC UART actually holds a count that is used to divide the 1.8432 MHz master input clock down to the 16x rate used internal to the UART. The 16x rate is particularly important for the receiver section of a UART so that it can oversample the incoming serial data stream and find start bits, estimate the data bits center position and setup the optimum time to clock in the actual serial data stream.

You are correct that the legacy UART configuration in a PC has a top end baud rate of 115200 bps. There are however many newer hardware sets that support higher baud rates including 230400 bps, 460800 bps and others. The most common place to find this capability today is in some motherboard computer chipsets and in todays very common USB to RS232 adapter cables. I have some cables with drivers that support baud rates above 1000000 bps! Most times the higher baud rate capabilites come into play when the actual UART hardware is "hidden" from programmers that typically work with the likes of VB6 under Windows. The details of the non-legacy behavior of the UART are buried inside the Windows device drivers and so there is no need to be aware of how the 16x clocking is derived.

Good Luck
Michael Karas


All times are GMT -6. The time now is 05:28 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.