Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   rs232 class problem (http://www.programmingforums.org/showthread.php?t=10575)

Brent Jun 29th, 2006 5:12 PM

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.

Cache Jun 29th, 2006 5:36 PM

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?

DaWei Jun 29th, 2006 6:00 PM

Cache is correct. I'm thinking you've picked up some ooooold code somewhere. Have you resorted to MSDN?

Brent Jun 29th, 2006 6:38 PM

Alright thanks, Ill check out MSDN


All times are GMT -5. The time now is 8:00 AM.

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