Solved

Connect to serial port

Posted on 2001-08-02
16
336 Views
Last Modified: 2013-11-15
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
Comment
Question by:romilson
  • 4
  • 4
  • 3
  • +3
16 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6345643
What OS?
What compiler?
0
 

Author Comment

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


Thanks a lot.

Romilson
0
 
LVL 86

Expert Comment

by:jkr
ID: 6345731
>>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
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6347391
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
 
LVL 6

Accepted Solution

by:
zebada earned 120 total points
ID: 6348293
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
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6375433
romilson ???
0
 

Author Comment

by:romilson
ID: 6379631
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
 
LVL 86

Expert Comment

by:jkr
ID: 6379771
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:zebada
ID: 6381890
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
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6382879
Done
0
 

Author Comment

by:romilson
ID: 6384321
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
 
LVL 6

Expert Comment

by:zebada
ID: 6386369
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
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6422207
romilson ???
I think, zebada must get points.
0
 
LVL 6

Expert Comment

by:zebada
ID: 6423556
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
 
LVL 11

Expert Comment

by:griessh
ID: 6842601
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
 
LVL 11

Expert Comment

by:griessh
ID: 6842604
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article covers general Notes 8.5 troubleshooting information including recreating the Notes\Data folder.
Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now