Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

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


0
romilson
Asked:
romilson
  • 4
  • 4
  • 3
  • +3
1 Solution
 
AxterCommented:
What OS?
What compiler?
0
 
romilsonAuthor Commented:
OS : WIN95 or NT
Compiler:
I use VC++ 6.0, a Win32 Console Application


Thanks a lot.

Romilson
0
 
jkrCommented:
>>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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AlexVirochovskyCommented:
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.
0
 
zebadaCommented:
If you need to send/receive non rs232 data then you can use the CTS pin and directly "read" the +/- voltage levels.

I used the following functions to do this:

var
  Fd: THandle;
  flag: DWORD;

SetCommMask(fd,EV_CTS);
WaitCommEvent(fd,flag,nil);

WaitCommEvent() will return whenever the CTS pin changes from +ve to -ve or -ve to +ve.

Paul

0
 
AlexVirochovskyCommented:
romilson ???
0
 
romilsonAuthor Commented:
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
0
 
jkrCommented:
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.
0
 
zebadaCommented:
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
0
 
AlexVirochovskyCommented:
Done
0
 
romilsonAuthor Commented:
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
0
 
zebadaCommented:
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.
0
 
AlexVirochovskyCommented:
romilson ???
I think, zebada must get points.
0
 
zebadaCommented:
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.

0
 
griesshCommented:
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: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
griesshCommented:
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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now