Link to home
Start Free TrialLog in
Avatar of romilson
romilson

asked on

Connect to serial port

Hi,

I need connect a serial port ( com1 or com2 ) and send signals.
I have a device in the serial port , that receive my bits.
I have to send +12V and later , -12V.

I have tried a asm call ..

__asm {
   mov al , 1
   mov dx , 2F8H
   out dx , al
}

and

__asm {
   mov ah , 01H
   mov dx , 2F8H
   INT 14H
}

But , it wasn't successul.


Thanks , and i wait.

Romilson


Avatar of Axter
Axter
Flag of United States of America image

What OS?
What compiler?
Avatar of romilson
romilson

ASKER

OS : WIN95 or NT
Compiler:
I use VC++ 6.0, a Win32 Console Application


Thanks a lot.

Romilson
>>OS : WIN95 or NT

You cannot use serial ports in this fashin on a Win32 platform - see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwbgen/html/msdn_serial.asp ('Serial Communications in Win32') on how to use the serial port correctly.
You need to use something as WinIo :v1.0                          
.....
    Direct Hardware Access Under Windows 9x/NT/2000        
            Copyright 1998-2000 Yariv Kaplan                
                http://www.internals.com                   
------------------------------------------------------------

The WinIo library allows 32-bit Windows applications to directly
access I/O ports and physical memory. It bypasses Windows protection
mechanisms by using a combination of a kernel-mode device driver and
several low-level programming techniques.

Under Windows NT, the WinIo library can only be used by applications
that have administrative privileges. If the user is not logged on as
an administrator, the WinIo DLL is unable to install and activate the
WinIo driver. It is possible to overcome this limitation by installing
the driver once through an administrative account. In that case, however,
the ShutdownWinIo function must not be called before the application
is terminated, since this function removes the WinIo driver from the
system's registry.
....
If you want, I can send you Winio DLL + my example of using
(for 95, for NT must make little change): write your EMail.
ASKER CERTIFIED SOLUTION
Avatar of zebada
zebada

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
romilson ???
hi ,

-- Axter : ??

-- AlexVirochovsky :
Please , send your example : romilson@brfree.com.br

-- zebada :
The WaitCommEvent() is very important, but i need control the device. I need a function that I can change the volts level of +12v to -12v and -12v to +12v ..
This function (WaitCommEvent()) will can be used in another side( in a client ou listener process) that wait to a voltage change in the device. But , in moment i need a fuction that change the voltage of the port.

Thanks a lot,
and I wait.

Romilson
Again:

You cannot use serial ports in this fashion on a Win32 platform - see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwbgen/html/msdn_serial.asp ('Serial Communications in Win32')
on how to use the serial port correctly.
You can change the voltage levels of the RTS and/or the DTS pin(s) by using the following.

  DCB_DtrEnable=       $00000010; // Enable DTR line
  DCB_RtsEnable=       $00001000; // Enable RTS line

To switch the voltage ON use:

  if ( not GetCommState(Fd,Dcb) ) then
     ...error...;
  Dcb.Flags := DWORD(Dcb.Flags) or DWORD(flags);
  if ( not SetCommState(Fd,Dcb) ) then
     ...error...;

To switch the voltage OFF use:

  Dcb.Flags := DWORD(Dcb.Flags) and DWORD(not flags);

flags would be set to either DCB_DtrEnable or DCB_RtsEnable or both.

Be careful not to draw too much current, if you are only ddriving CTS/DSR pins then thats fine.

Cheers
Paul
zebada ,

Have I to use both pin(s) to control my device ?
because .. the device is only connected on the RTS pin..
and the idea is:

when I send +12v to rts pin, the device turn on
when I send -12v to rts pin, the device turn off

Can i use only the RTS pin ?

Romilson
You only need to use one pin, RTS is the one I use. However you can control TWO devices independantly by hooking one to the RTS pin and one to the DTR pin if you wish.
Paul

P.S. not sure if the voltage levels from a laptop are 12volts. Although the RS-232 spec says valotages are 12 volts, most laptops (to save power) only use about 5 volts.
romilson ???
I think, zebada must get points.
Romilson,

Previously I said to use SetCommState to change the DTR/RTS lines. A better way (and much faster way) is to use

  state: DWORD;

  if ( ...turn on DTR... ) then
    state := SETDTR
  else
    state := CLRDTR;
  if ( not EscapeCommFunction(fd,state) ) then
    ...error...    

And use SETRTS and CLRRTS for the RTS line.

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "Axter, zebada" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
romilson:

I don't know how far you came, but here is a word of caution: The RS232 electrical spec only requires -5 to +5 Volts (although most RS232 outputs work at +-12V). SO please don't rely on the RTS pin to be able to handle the load under all circumstances.

======
Werner