www.digitalmars.com         C & C++   DMDScript  

c++ - program crashing...want to read and write to serial port in pure C functions

reply kuldip(kal) <kuldip(kal)_member pathlink.com> writes:
Guys!!! urgent help is needed......

Help in any pointers to deal with serial port through pure C, code snippets or
even direct program will be appreciated!!! I tried the program given by 
http://www.beyondlogic.org/serial/serial.htm

I compiled this program with DIGITAL MARS compiler. Program compiled and linked
fine but when I run the program it crashes...I do not understand why...
My board is P6VAP+ by company ECS. I checked with the manual.It assigns IRQ 3 to
COM1 and at address 3F8. I do understand that COM1 will go from 0x3F8 - 0x3FF
and COM2 from 0x2F8-0x2FF.

But I dont understand why my program is crashing and how to read and write to
the port.

thanks and I appreciate your help...

kuldip(kal) 

#include <stdio.h>
#include <dos.h>

int main()
{
unsigned int far *ptraddr;  /* Pointer to location of Port Addresses */
unsigned int address;       /* Address of Port */
int a;

ptraddr=(unsigned int far *)0x00000400;
getch();  
return 0;
}

kuldip
Dec 14 2005
next sibling parent reply Arjan <arjan ask.me> writes:
kuldip(kal) wrote:
 Guys!!! urgent help is needed......
 
 Help in any pointers to deal with serial port through pure C, code snippets or
 even direct program will be appreciated!!! I tried the program given by 
 http://www.beyondlogic.org/serial/serial.htm
 
 I compiled this program with DIGITAL MARS compiler. Program compiled and linked
 fine but when I run the program it crashes...I do not understand why...
 My board is P6VAP+ by company ECS. I checked with the manual.It assigns IRQ 3
to
 COM1 and at address 3F8. I do understand that COM1 will go from 0x3F8 - 0x3FF
 and COM2 from 0x2F8-0x2FF.

Are you actually using dos? Otherwise check the the OS api for the correct way to use a serial device. In Win32 you should use CreateFile ( .. ) The MSDN http://msdn.microsoft.com has several articles about serialport programming.
 
 But I dont understand why my program is crashing and how to read and write to
 the port.
 
 thanks and I appreciate your help...
 
 kuldip(kal) 
 
 #include <stdio.h>
 #include <dos.h>
 
 int main()
 {
 unsigned int far *ptraddr;  /* Pointer to location of Port Addresses */
 unsigned int address;       /* Address of Port */
 int a;
 
 ptraddr=(unsigned int far *)0x00000400;
 getch();  
 return 0;
 }
 
 kuldip

Dec 15 2005
parent reply kal(kuldip) <kal(kuldip)_member pathlink.com> writes:
Hi 

thanks for the reply.. But I can not user the Windows kernel API for sure. Now I
rebuild the program. I am trying to use the program temproll.c.

http://www.beyondlogic.org/serial/termpoll.c

When i run that program  it is saying undefined function outportb and inportb...


I think that program is compiled with TC++. As I looked in the archives i came
to know that I can not use inportb and outportb with digital mars. I will have
to use _inp and _outp. But I dont know how to use that. can u help me.

thanks in advance....

kuldip


In article <dnrte5$2ev$1 digitaldaemon.com>, Arjan says...
kuldip(kal) wrote:
 Guys!!! urgent help is needed......
 
 Help in any pointers to deal with serial port through pure C, code snippets or
 even direct program will be appreciated!!! I tried the program given by 
 http://www.beyondlogic.org/serial/serial.htm
 
 I compiled this program with DIGITAL MARS compiler. Program compiled and linked
 fine but when I run the program it crashes...I do not understand why...
 My board is P6VAP+ by company ECS. I checked with the manual.It assigns IRQ 3
to
 COM1 and at address 3F8. I do understand that COM1 will go from 0x3F8 - 0x3FF
 and COM2 from 0x2F8-0x2FF.

Are you actually using dos? Otherwise check the the OS api for the correct way to use a serial device. In Win32 you should use CreateFile ( .. ) The MSDN http://msdn.microsoft.com has several articles about serialport programming.
 
 But I dont understand why my program is crashing and how to read and write to
 the port.
 
 thanks and I appreciate your help...
 
 kuldip(kal) 
 
 #include <stdio.h>
 #include <dos.h>
 
 int main()
 {
 unsigned int far *ptraddr;  /* Pointer to location of Port Addresses */
 unsigned int address;       /* Address of Port */
 int a;
 
 ptraddr=(unsigned int far *)0x00000400;
 getch();  
 return 0;
 }
 
 kuldip


Dec 15 2005
parent Heinz Saathoff <hsaat despammed.com> writes:
Hello,

 thanks for the reply.. But I can not user the Windows kernel API for sure. Now
I
 rebuild the program. I am trying to use the program temproll.c.
 
 http://www.beyondlogic.org/serial/termpoll.c
 
 When i run that program  it is saying undefined function outportb and
inportb...

These are MSDOS specific functions and therefore not standardized.
 I think that program is compiled with TC++. As I looked in the archives i came
 to know that I can not use inportb and outportb with digital mars. I will have
 to use _inp and _outp. But I dont know how to use that. can u help me.

The easy way is to use #define to rename the functions: /* ...... */ #include <dos.h> #include <stdio.h> #include <conio.h> /* this two lines will do the rename */ #define inportb(port) _inp(port) #define outportb(port,byte) _outp(port,byte) #define PORT1 0x3F8 /* ..... rest of code unchanged ......*/ I've compiled the example but not tested the executable. HTH, Heinz
Dec 16 2005
prev sibling parent Heinz Saathoff <hsaat despammed.com> writes:
Hello,

 Help in any pointers to deal with serial port through pure C, code snippets or
 even direct program will be appreciated!!! I tried the program given by 
 http://www.beyondlogic.org/serial/serial.htm
 
 I compiled this program with DIGITAL MARS compiler. Program compiled and linked
 fine but when I run the program it crashes...I do not understand why...
 My board is P6VAP+ by company ECS. I checked with the manual.It assigns IRQ 3
to
 COM1 and at address 3F8. I do understand that COM1 will go from 0x3F8 - 0x3FF
 and COM2 from 0x2F8-0x2FF.
 
 But I dont understand why my program is crashing and how to read and write to
 the port.

The code you've posted doesn't crash, even if built as Win32 executable. The situation changes when the memory area is accessed by ptraddr. Dereferencing the far ptr will cause a crash when compiled for Win32. Be sure to build your program as DOS executable by adding the correct mmory model switch to DMC. Call the compiler this way: dmc -ms serial.c The resulting serial.exe will run on pure DOS. Runing on Win-NT/2000/XP will not crash but also not work as DOS programs run in a virtual machine. You can't access hardware directly in this OSes. - Heinz
Dec 16 2005