Reading dates from serial port in C++

  • Thread starter Germán Pérez Argüelles
  • Start date
G

Thread Starter

Germán Pérez Argüelles

I´m making a program in C++, and I don´t know how can I take dates from the serial port and send dates to this port. I need it to take information from a microcontroller that have this port. Thank you.

my e-mail is [email protected]
 
Read This program and then send me your idea

/*--------------*
1 /*Liberary rs_li2.c - PC serial line function controlling the 8250 ACE*/
2
3 #include<dos.h> /* MS-DOS specified header file */
4 #incliude<conio.h> /* direct console I/O functions */
5 #include<stdio.h> /* standard I/O header file */
6
7 /*define 8250 port base addreses for RS232 ports 0 to 3 (com1: to com:4)*/
8 satic const shorrt int rs_ports[]={0x3f8,0x3e8,0x2e8};
9
10 /*8250 register offsets from base address*/
11 enum{datareg,intena,intdent,linectrl,modctrl,,line status,modstatus};
12
13 /*8250 bits in line status,modem satus,modem control registers*/
14 enum{DR=0x100,THRE=0x2000,CTS=0x10,RTS=0x2};
15
16 /*-----------------------------------------------------------------------------------*
17 *serial port:initialise io_port,det DTR and RTS to high */
18 void rs_initialise(const int io_port)
19 {
20 union REGS registers; /* variable to hold 8086 register */
21
22 registers.h.ah=0; /* command initialise */
23 /*set baud rate to 9600,8 data bits, no parity and 1 stop bit*/
24 registers.h.al=0xe3; /* baud rate to 9600 */
25 registers.x.dx=io_port; /* for port */
26 int86(0x14,®isters,®isters); /* interrupt 14 hexadecimal */
27 /*set DTR and RTS in modem control reg*/
28 outp(rs_ports[io_port]+modctl,DTR+RTS);
29 }
30
31 /*-----------------------------------------------------------------------------------*
32 *serial port:terminate io_port, sets DTR and RTS to low */
33 void rs_terminate(const int io_port)
34 {
35 /*set all bits 0 in modem control register */
36 outp(rs_ports[io_port]+modctrl,0);
37 }
38
39 /*-----------------------------------------------------------------------------------*
40 *serial port :return status of io_port *
41 * line status in top byte (ah),modem status in lower byte (al) */
41 int rs_status(const int io_port)
43 {
44 int status=0;
45
46 status=inp(rsports[io_port]+linesatus); /* read line status */
47 /*shift line status to top byte and then read modem status */
48 status=(status<<8)+inp(rs_ports[io_port]+modstatus);
49 return status; /* and return value */
50 }
51
52 /*----------------------------------------------------------------------------------*
53 *serial port:transmit character to io_port */
54 void rs_putch (const int io_port,const int character)
55 {
56 while (!((rs_status(io_port) & THRE) && (rs_status(io_port) & CTS)))
57 /*wait for transmitter holding register empty and CTS*/
58 /*putch(character);*/ /* echo character if required */
59 outp(rs_ports[io_port],character; /* transmit character */
60 }
61
62 /*----------------------------------------------------------------------------------*
63 *sereial port:transmita string of characters to io_port */
64 void rs_putstring(const int io_port,const char *string)
65 {
66 while(*string !='\0')
67 rs_putch(io_port,*string++);
68 }
69
70 /*----------------------------------------------------------------------------------*
71 *serial port:return TRUE if character available from serial line */
72 int rs)received(int io_port)
73 {
74 int status;
75
76 status =rs_status(io_port); /* get status */
77 if(status & 0xe00) /* receve error ?? */
78 printf("\n\aRead error on aerial line ,status =%#x\n",status & 0xe00);
79 return(status & DR); /* test DR bit in line status register */
80 }
81
82 /*----------------------------------------------------------------------------------*
83 *serial port:read character from io_port */
84 char rs_getch(const int io_port)
85 {
86 while(! rs_received(io_port))
87 /*wait for character erceived*/
88 return((char) inp(rs_ports[io_port]); /* read character */
89 }
90 /*---------------------------------------------------------------------------------*/
 
> > I don't recall ever having bought a program for my own use
> > (including professional use) that required any consulting to use. If
> > it did, I probably wouldn't use it. I think that enabling people to
> > do complex tasks without consulting is the primary benefit of the
> > majority of software that is sold (or downloaded for free for that
> > matter).

Francis Lovering:
> Well, I have seen an awful lot of people using Word, Excel and Access
> that could do with a lot of consulting - or at least training.

Not to mention that if you need something written well, you contract or employ a professional writer of some sort - technical, marketing, legal, whatever is required in the situation.

> > the expertise isn't in the drawing of the boxes themselves, but in
> > knowing which boxes to draw. I don't think any software can help
> > with that.

> I agree the first sentence but I beg to differ with the second. Yes,
> there is expertise knowing what to put on a diagram, but examples and
> some automated functions can help greatly.

Examples can help, and the software can be of some limited assistance; but at a certain point only a professional will do.

> > ControlDraw, as an OSS project, might become successful but how can
> > you build a for-profit company on a model where nobody pays anything
> > for ControlDraw?

> Quite so.

> As a consultant I often spend time developing models and functions
> (outside ControlDraw) that greatly speed up the production of
> DCS/PLC/HMI software from a ControlDraw model. That is paid directly
> as consulting charges. Suppose instead that I were to spend 1000
> hours improving the software to add the capability to generate their
> automation software directly from a ControlDraw model without the
> users having to employ me as a consultant. How would I get paid for
> that other than by charging for the software?

"Civilization advances by extending the number of important operations which we can perform without thinking of them."
--Alfred North Whitehead

Once the simple cases are automated, so that consultants are no longer needed for them, industry will move on to automating more complicated cases, which the software does not (yet) cover - thus there will always be demand for consultants.


Jiri
--
Jiri Baum <[email protected]> http://www.csse.monash.edu.au/~jirib
MAT LinuxPLC project --- http://mat.sf.net --- Machine Automation Tools
 
Two questions:

1. Do you already know how to read and write from the serial port in C++?

2. Do you know what data format the microcontroller uses for dates? (i.e. ASCII string, 32-bit integer seconds since 1970, days since 1970 + milliseconds since yesterday, etc)
 
B

Bashar Duheidel

I am looking into connecting an AND gate to the serial port and sending inputs through the serial port is there a program that will enable me to do that.

thanks
 
<p>I came back <br>
Read this<br>
i coplate and reform the program <br>
..<br>
if u have a question <br>
just send massege me

<p>[email protected]
<pre>
***********************************************
/*--------------*/
/*Liberary rs_li2.c - PC serial line function controlling the 8250 ACE*/

#include<dos.h> /* MS-DOS specified header file */
#include<conio.h> /* direct console I/O functions */
#include<stdio.h> /* standard I/O header file */

/*define 8250 port base addreses for RS232 ports 0 to 3 (com1: to com:4)*/
static const short int rs_ports[]={ 0x3f8 , 0x2f8 , 0x3e8 , 0x2e8 };

/*8250 register offsets from base address*/
enum{Datareg,IntEna,IntIdent,LineCtrl,ModCtrl,LineStatus,ModStatus};

/*8250 bits in line status,modem satus,modem control registers*/
enum{DR=0x100,THRE=0x2000,CTS=0x10,DTR=0x1,RTS=0x2};
/*----------------------------------------------------------*/
void main (void)
{

void rs_initialise(const int io_port);
void rs_terminate(const int io_port);
int rs_status(const int io_port);
void rs_putch (const int io_port,const int character);
char rs_getch(const int io_port);
int rs_received(int io_port);
void rs_putstring(const int io_port,const char *string);
const int io_port=0;
int kb_char=0,rs_char;
rs_initialise(io_port);
rs_putstring(io_port,"testing Line \x0d\x0a");

clrscr();
printf("Simple terminal emulator ,hit<ESC> to end\n\n");

while(kb_char!=27)
{
if(kbhit())
{
kb_char = getch();
putch(kb_char);
if (kb_char==0x0d)
rs_putstring(io_port,"\x0d\x0a");
if (kb_char>=' ')
rs_putch(io_port,kb_char);
}
/* if(rs_received(io_port))
{
/*rs_char=rs_getch(io_port);*/
/*if(rs_char==0x0d)
{
putch(10);
putch(13);
}
else
{
if (rs_char>=' ')
putch(rs_char);
}*/
/* putch(rs_getch(io_port));
/* }*/
}

rs_terminate(io_port);

}
/*----------------------------------------------------------------------------*
*serial port:initialise io_port,det DTR and RTS to high */
void rs_initialise(const int io_port)
{
union REGS regin,regout; /* variable to hold 8086 register */

regin.h.ah=0; /* command initialise */
/*set baud rate to 9600,8 data bits, no parity and 1 stop bit*/
regin.h.al=0xe3; /* baud rate to 9600 */
regin.x.dx=io_port; /* for port */
int86(0x14,&regin,&regout); /* interrupt 14 hexadecimal */
/*set DTR and RTS in modem control reg*/
outp(((rs_ports[io_port])+ModCtrl),(DTR+RTS));
}

/*----------------------------------------------------------------------------*
*serial port:terminate io_port, sets DTR and RTS to low */
void rs_terminate(const int io_port)
{
/*set all bits 0 in modem control register */
outp(rs_ports[io_port]+ModCtrl,0);
}
/*----------------------------------------------------------------------------*
*serial port:transmit character to io_port */
void rs_putch (const int io_port,const int character)
{
while (!((rs_status(io_port) & THRE) && (rs_status(io_port) & CTS)))
/*wait for transmitter holding register empty and CTS*/
putch(character); /* echo character if required */
outp(rs_ports[io_port],character); /* transmit character */
}

/*----------------------------------------------------------------------------*
*sereial port:transmita string of characters to io_port */
void rs_putstring(const int io_port,const char *string)
{
while(*string !='\0')
rs_putch(io_port,*string++);
}
/*----------------------------------------------------------------------------*
*serial port :return status of io_port *
* line status in top byte (ah),modem status in lower byte (al) */
int rs_status(const int io_port)
{
int status=0;
status=inp(rs_ports[io_port]+LineStatus); /* read line status */
/*shift line status to top byte and then read modem status */
status=(status<<8)+inp(rs_ports[io_port]+ModStatus);
return status; /* and return value */
}

/*----------------------------------------------------------------------------*
*serial port:return TRUE if character available from serial line */
int rs_received(int io_port)
{
int status;

status =rs_status(io_port); /* get status */
if(status & 0x0e00) /* receive error ?? */
printf("\n\aRead error on aerial line ,status =%#x\n",status & 0xe00);
return(status & DR); /* test DR bit in line status register */
}

/*----------------------------------------------------------------------------*
*serial port:read character from io_port */
char rs_getch(const int io_port)
{
char s1;
while(! rs_received(io_port))
/*wait for character erceived*/
s1=inp(rs_ports[io_port]);
return (s1); /* read character */
}
/*---------------------------------------------------------------------------*/
</pre>
 
Top