Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 29th, 2006, 4:12 PM   #1
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 251
Rep Power: 4 Brent is on a distinguished road
rs232 class problem

I have a class that sends data through the com port 1 via rs232. The code compiles fine, but everytime I use the class in a program and run the program, a windows error message pops up and says that my program has encountered and error and needs to exit and asks me if i want to send an error report. Im running Windows XP and using a Digital Mars compiler(crappy, I know, but its all I have at the moment until I get VC++ installed again)

here's the class:
#ifndef _RS232_H
#define _RS232_H

#include<iostream.h>
#include<string.h>
#include<conio.h>


#define DLL 0 //divisor latch low byte 
#define DLH 1 //divisor latch high byte 
#define THR 0 //transmit hold register 
#define RBR 0 //recieve buffer register 
#define IER 1 //interrupt enable register 
#define LCR 3 //line control register 
#define MCR 4 // modem control register 
#define LSR 5 //line status register 
#define MSR 6 //modem status register 
#define RTS 0x02 // request to send 
#define CTS 0x10 //clear to send 
#define DTR 0x01 //data terminal ready 
#define DSR 0x20 //data set ready 
#define RBF 0x01 //bit 0 of LSR, rec buf full 
#define THRE 0x20 //bit 5 of LSR, trans reg 0 
#define DISINT 0x00 //disable interrupts in IER 
#define ABRG 0x83 //access baud rate generator 



class rs232
{
	public:
		rs232();
		rs232(int far*);
		
		
		void init(int,int,char*,int,int);
		void set_id(char [50]);
		void transmit(char,int);
		char receive(int);
		char rec(unsigned int);
		void send(unsigned int,int);
		void send_msg();
		void recv_msg();
		void listen();
		
	private:
		unsigned int divisorh, divisorl, format, acia[2], add;
		int val, comport,a;
		char byte, data, u_id[50], id[50];
		int far* bios;
		int com_port, baud_rate, word_size, stops;
		char *parity;
		char c[10];
};

rs232::rs232()
{


}

rs232::rs232(int far* bios)
{
	data=0;
	bios=(int far*)0x00400000l;
}

void rs232::send(unsigned int add, int val)
{
	_DX=add;
    _AL=val;
    asm out dx,al
}

char rs232::rec(unsigned int add)
{
    _DX=add;
    asm in al,dx
    return(_AL);
}

void rs232::set_id(char id[50])
{
	strcpy(u_id,id);
}
				
void rs232::init(int com_port,int baud_rate,char *parity,int stops, int word_size)
{
	acia[0]=*bios; 
	acia[1]=*(bios+1);
		
	send(acia[com_port]+IER, DISINT); 
	send(acia[com_port]+LCR, ABRG); 
	
	switch(baud_rate) 
	{
		case 300: 
		{
			divisorh = 01; 
			divisorl = 0x80; 
		}
		break;
		case 600: 
		{
			divisorh = 00; 
			divisorl = 0xc0; 
		}
		break;
		case 1200: 
		{
			divisorh = 00; 
			divisorl = 0x60;
		}
		break;
		case 2400: 
		{
			divisorh = 00; 
			divisorl = 0x30; 
		}
		break;
		case 4800: 
		{
			divisorh = 00; 
			divisorl = 0x18; 
		}	
		break;
		case 9600: 
		{
			divisorh = 00; 
			divisorl = 0x0c; 
		}
		break;
		default: 
		{
			cout<<"ERROR: INVALID BAUD RATE\n";
		}
	} 
	send(acia[com_port] + DLL, divisorl);
	send(acia[com_port] + DLH, divisorh);
	
	format = 0; 
	
	if((strcmp(parity,"E")==0) || (strcmp(parity,"O")==0)) 
	{
		format=format | 0x28; 
		if(strcmp(parity,"E")==0)
		{
			format=format | 0x10; 
		}
	}
	if(stops==2)
	{
		format=format|0x04;
		switch(word_size) 
		{
			case 8: 
			{
				format=format | 0x03; 
			}	
			break;
			case 7: 
			{
				format=format | 0x02; 
			}	
			break;
			case 6: 
			{
				format=format | 0x01; 
			}	
			break;
			case 5: 
			break;
			default: 
			{
				cout<<"ERROR: UNSUPPORTED WORD SIZE\n";
			}
		} 
		send(acia[com_port] + LCR, format);
	}
}
			
void rs232::transmit(char byte, int comport)
{
	send(acia[comport]+MCR, (RTS | DTR)); 
	
	while((rec(acia[comport]+LSR) & THRE)==0) 
				
	send(acia[comport]+THR, byte); 
	send(acia[comport]+MCR, 0);
}

char rs232::receive(int comport) 	
{
	send(acia[comport] + MSR, (RTS | DTR));
	
	while((rec(acia[comport]+LSR) & RBF)==0) 
			
	send(acia[comport] + MCR,0); 
	byte = rec(acia[comport] + RBR);
	return(byte);
}

void rs232::send_msg()
{	
	data=0;
	while(data < 50)
	{
		data=getch();
		cout<<data;
		transmit(data,0);
	}
}

void rs232::recv_msg()
{
	data=0;
	while((data!=50)&&(data!=0))
	{
		data=receive(0);
		cout<<data;
	}
}

#endif


and here's the sample program:
#include"com.h"


int main()
{
	rs232 sock;
	
	sock.init(0,9600,0,1,8);
	
	sock.send_msg();
	
	return 0;
}

sorry for the wacked out indentations.
Brent is offline   Reply With Quote
Old Jun 29th, 2006, 4:36 PM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
I don't really know anything about this kind of program, so this is more of a question than anything: on a modern OS like XP wouldn't you have to be running in Kernel mode to write to hardware? or at least go thought a driver?
Cache is offline   Reply With Quote
Old Jun 29th, 2006, 5:00 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Cache is correct. I'm thinking you've picked up some ooooold code somewhere. Have you resorted to MSDN?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 29th, 2006, 5:38 PM   #4
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 251
Rep Power: 4 Brent is on a distinguished road
Alright thanks, Ill check out MSDN
Brent is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC