Solved

Connect to serial port

Posted on 2001-08-02
16
343 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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
 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best book to learn C++ 4 78
Unable to start eclipse ? 17 141
Header of docx file 17 102
PC freezes and shutdowns becoming more frequent 17 119
Today companies are subjected to more-and-more data, and it won't stop any time soon.  But there are obvious opportunities for reducing data, particularly data duplicated among companies.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.

808 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