Use the C language interrupt mode to implement serial port read/write

Source: Internet
Author: User
# Include <stdio. h>
# Include <dos. h>
# Include <time. h>

# Define LSB 0
# Define MSB 1

# Define RXR 0
# Define txr 0
# Define ier 1
# Define IIR 2
# Define LCR 3
# Define MCR 4
# Define LSR 5
# Define MSR 6

# Define combench base 0x3f8
# Define uchar unsigned char

Volatile uchar inputdata;
Volatile uchar oninput = 0x00;
Volatile uchar onoutput = 0x00;
Volatile uchar inputdata [256];/* set the size of the receiving buffer */
Volatile unsigned int count;/* Number of data received during interruption */
Int datacount;/* Number of currently sent data lines */

Void interrupt (* oldvect) ();/* function is used to obtain Interrupt ProcessingProgram*/
Void interrupt serialisr ();

Void initcom ()
{
Uchar inttemp;
/* Set the baud rate or something */
Outportb (combench base + LCR, 0x80);/* set the high level of LCR to 1 to read other registers */
Outportb (combench base + LSB, 0x0c);/* divisor lock (8-bit low) DLL */
Outportb (comstmbase + MSB, 0x00);/* divisor lock (8-bit high) DLH generates 2400 baud rate */
Outportb (combench base + LCR, 0x03);/* 8-bit data, 1-bit stop bit, no verification */
Outportb (combench base + IER, 0x01);/* interrupt mode for receiving */

/* Set the interrupt vector */
Oldvect = getvect (0x0c);/* the function is used to obtain the entry address of the interrupt handler */
Disable ();
Inttemp = inportb (0x21) & 0xef;
Outportb (0x21, inttemp );
Setvect (0x0c, serialisr);/* set the interrupt entry address of serialisr to 0x0c */
Enable ();
}

Void closecom ()
{
Disable ();
Outportb (combench base + IER, 0x00); * disable interruption */
Outportb (combench base, 0x00 );
Outportb (0x21, inportb (0x21) | ~ (0xef ));
Setvect (0x0c, oldvect );
}

Void interrupt serialisr ()
{
/* Serial port interruption ServiceCode*/
Inputdata = inportb (comdeskbase + RXR );
Oninput = 0x01;
Inputdata [count] = inputdata;
Count ++;
/* End the Service Code */
Outportb (0x20, 0x20);/* code for stopping */
}

Void sendchar (uchar key)
{
While (inportb (combench base + LSR) & 0x40) = 0 );
Outportb (combench base + txr, key );
}
/* ............ Latency function ......................*/
Void delay (unsigned int N)
{
Unsigned int I, K;
For (k = 0; k <n; k ++)
{
For (I = 1; I <1142; I ++)
;
}
}

/* ...... The CDMA Command sends the function .................*/
Void CDMA _commandsend (char * P, unsigned int N)
{

Int I, K, M, L;
Int flag = 0;
Int selse_flag = 0; int selse_flag_1 = 0;
Int selse_flag1 = 0; int selse_flag1_1 = 0;
Time_t start, end;
Double DIF = 0;
Char;

Uchar bexit_flag = 0x00;
Count = 0;
While (! Bexit_flag)
{
Count = 0;
For (I = 0; I <n; I ++)
{Sendchar (* P); delay (9000); P ++ ;}

/* ...... Wait for 2 S ..................*/
Time (& START );
DIF = 0;
While (DIF <2)
{
Delay (9000 );
Time (& End );
DIF = difftime (end, start );
}
If (count> 14)
{
Time (& START );
DIF = 0;
While (DIF <3)
{
Delay (9000 );
Time (& End );
DIF = difftime (end, start );
}
}

/* ...... Determine whether the sending command is successful ..................*/
For (m = 0; m <count; m ++)
{
If (count> 10)
{
For (L = 0; L <count; l ++)
{
If (inputdata [L-1] = 'n' & inputdata [L-2] = 'E' & inputdata [L-3] = 'P' & inputdata [L-4] = = 'O' & inputdata [l-5] = 'P' & inputdata [L-6] = 'P' & inputdata [L-7] = 'P' & inputdata [L-8] = 'V ') {selse_flag_1 = 1 ;}
If (inputdata [L-1] = 'n' & inputdata [L-2] = 'E' & inputdata [L-3] = 'P' & inputdata [L-4] = = 'O' & inputdata [l-5] = 'P' & inputdata [L-6] = 'C' & inputdata [L-7] = 'T' & inputdata [L-8] = 'V ') {selse_flag1_1 = 1 ;}
}

If (selse_flag_1 = 1) {If (inputdata [M-1] = '3' & inputdata [m-2] = '3' & inputdata [M-3] = ': '& inputdata [M-4] = 'T') selse_flag = 1 ;}
If (selse_flag1_1 = 1) {If (inputdata [M-1] = '0' & inputdata [m-2] = ', '& inputdata [M-3] = '1' & inputdata [M-4] =': '& inputdata [M-5] = 'n') selse_flag1 = 1 ;}

If (selse_flag_1 = 1 | selse_flag1_1 = 1)
{
If (selse_flag = 1) {If (inputdata [M-1] = '0' & inputdata [m-2] = ': '& inputdata [M-3] = 'n' & inputdata [M-4] = 'E') Flag = 1 ;}
If (selse_flag1 = 1) {If (inputdata [M-1] = '0' & inputdata [m-2] = ', '& inputdata [M-3] = '1' & inputdata [M-4] =': '& inputdata [M-5] =' s ') Flag = 1 ;}
}
Else {If (inputdata [M-1] = 'O' & inputdata [m] = 'k') {flag = 1 ;}}
}
Else {If (inputdata [M-1] = 'O' & inputdata [m] = 'k') Flag = 1 ;}
}
If (flag = 1) {bexit_flag = 0x01 ;}
Else {P = p-n ;}

For (I = 0; I <count; I ++) printf ("% C", inputdata [I]);
/*........ Press the space to stop the test ............................. */
If (kbhit () {A = getch (); if (a = '') {closecom (); exit (1 );}}

}
}

/* ...... Data test and sending task .....................*/
Void data_send (char * pcomd, int N, char * pdata, int m)
{
Int I, K, L; time_t start, end;
Double DIF = 0;
Count = 0;
For (I = 0; I <n; I ++)
{
Sendchar (* pcomd); delay( 9000); pcomd ++;
}
Time (& START );
DIF = 0;
While (DIF <1, 0.2)
{
Delay (9000 );
Time (& End );
DIF = difftime (end, start );
}

For (L = 0; L <m; l ++)
{
Sendchar (* pdata); pdata ++;
}

Printf ("the data num: % d", datacount );
Datacount ++;
}

void main ()
{int I; char a;
/*........ module test command .................................. */
uchar CDMA _command_1 [] = {"at \ r" };< br> uchar CDMA _command_2 [] = {"atz \ r "};
uchar CDMA _command_3 [] = {"ate1v1 \ r" };< br> uchar CDMA _command_4 [] = {"at + CRM = 1; + CPS = 33; + cmux = 1; + CTA = 0 \ r "};
uchar CDMA _command_5 [] = {" at + vpppopen \ r "};
uchar CDMA _command_6 [] = {"at + vtcpopen = 1, \" 60.63.42.129 \ ", 1234 \ r"};
uchar CD Ma_command_7 [] = {"at + vtcpsend = 1, 20 \ r" };
uchar CDMA _data [] = {"abcdefghijklmnopqrst "};
uchar CDMA _command_9 [] = {"at + vpppclose \ r"};
uchar CDMA _command_8 [] = {"at + vtcpclose = 1 \ r "};
uchar bexit_flag1 = 0x00;
time_t start, end;
double DIF = 0;
initcom (); /* initialize Port 1 */
inputdata = inportb (comdeskbase + RXR);
while (! Bexit_flag1)
{
/*............................... .................................. */
If (kbhit ()) /* press any key to start testing CDMA */
{< br> /*...................... ........................................... */

/*......... send the TCP disconnection command ..................................... */
CDMA _commandsend (CDMA _command_8, strlen (CDMA _command_8);
/*......... send the PPP disconnection command .................................... .. */
CDMA _commandsend (CDMA _command_9, strlen (CDMA _command_9);

/*......... send the AT command ..................................... ..... */
CDMA _commandsend (CDMA _command_1, strlen (CDMA _command_1);
/*......... send the atz command ..................................... .... */
CDMA _commandsend (CDMA _command_2, strlen (CDMA _command_2);
/*......... send the ate1v1 command ...................................... */
CDMA _commandsend (CDMA _command_3, strlen (CDMA _command_3);
/*......... send the at initialization command .................................... */
CDMA _commandsend (CDMA _command_4, strlen (CDMA _command_4);
/*......... send the PPP dialing command ..................................... */
CDMA _commandsend (CDMA _command_5, strlen (CDMA _command_5);
/*......... send the network connection command .................................... */
CDMA _commandsend (CDMA _command_6, strlen (CDMA _command_6);
/*......... send data ...................................... ...... */
datacount = 0;
while (1)
{
data_send (CDMA _command_7, strlen (CDMA _command_7), CDMA _data, strlen (CDMA _data ));

Time (& START );
DIF = 0;
While (DIF <1, 0.2)
{
Delay (9000 );
Time (& End );
DIF = difftime (end, start );
}

If (kbhit () {A = getch (); if (a = '') {bexit_flag1 = 0x01; break ;}}
}
/*......... Send the TCP disconnection command ..................................... */
For (I = 0; I <3; I ++)
CDMA _commandsend (CDMA _command_8, strlen (CDMA _command_8 ));
/*......... Send the PPP disconnection command .................................... .. */
For (I = 0; I <3; I ++)
CDMA _commandsend (CDMA _command_9, strlen (CDMA _command_9 ));

Bexit_flag1 = 0x01;
}

/*...................................... ........................... */

}
Closecom ();/* close Port 1 */
Printf ("Bye ~. \ N ");
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.