How do I feel about adding a project to the C code of a DoS downstream serial port?

Source: Internet
Author: User

After returning more than 1000 posts and judging nearly papers, I took a long breath.

Recently, the serial port solution has finally been finalized, and it will soon end. Otherwise, it will take a lot of time and delay is the devil.

I found a code for reference.

# Include <dos. h>
# Include <fstream. h>
# Include <conio. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
# Define rxd 0 // receive
# Define txd 0 // send
# Define LSB 0 // The baud rate is adjusted as low as 8 bits
# Define MSB 1 // adjust the baud rate to 8 bits in height
# Define ier 1 // interrupt start register
# Define IIR 2 // interrupt ID 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 ierv 1
# Define out2 0x08
# Define ERTs 2
# Define edtr 1
# Define empty 0x20
# Define ready 0x30
# Define icreg 0x20
# Define imaskreg 0x21
# Define EOI 0x20
# Define waitcount 5000
# Define bufflen 2048 // the bounds of the array used to store characters
# Define alte 0x12
# Define altq 0x10
# Define ser_baud_1200 96
# Define ser_baud_2400 48
# Define ser_baud_9600 0x0c
# Define ser_baud_19200 6
# Define ser_stop_1 0/*/1 stop bit per character */
# Define ser_stop_2 4/*/2 Stop bits per character */
# Define ser_bits_5 0/*/send 5 Bit characters */
# Define ser_bits_6 1/*/send 6 bit characters */
# Define ser_bits_7 2/*/send 7 bit characters */
# Define ser_bits_8 3/*/send 8 bit characters */
# Define ser_parity_none 0/*/No parity */
# Define ser_parity_odd 8/*/odd parity */
# Define ser_parity_even 24/*/Even Parity */
Int port;
Int comnum;
Unsigned portaddr;
Unsigned portf;
Unsigned int baudtable [] = {0x180, 0xc0, 0x60, 0x30, 0x18, 0x0c, 0x06 };
Unsigned char paritytable [] = {0 x, 0 x, 0 x, 0x28, 0x38 };
Unsigned char buffer [bufflen]; // Recv Buf
Int buffin = 0;
Int buffout = 0;
Int incount = 0;
Int outcount = 0;
Void interrupt (* vect_com )();

Void putb (unsigned char ch) // write a char to the recvbuf to write the interrupted data to the buffer zone
{
Int temp;
Temp = buffin;
If (++ buffin = bufflen)
Buffin = 0;
If (buffin! = Buffout)
{Buffer [buffin] = CH;
// Printf ("Bufferin [% d] = % C", buffer [buffin]);
// Getch ();
}
Else
Buffin = temp;
};
Unsigned char getb () // read a char from the recvbuf
{
If (buffout! = Buffin)
{
If (++ buffout = bufflen)
Buffout = 0;
// Printf ("bufferout [% d] = % C", buffout, buffer [buffout]);
Return (buffer [buffout]);
}
Else
Return (0xff );
};
/** // * Unsigned char sender (unsigned char ch)
{

Outportb (portaddr2 + txd, CH );
Printf ("sender outportdata = % C", CH );
Outcount ++;
Return (1 );
 
};
*/
Void interrupt receiver ()
{
Unsigned char ch;
Ch = inportb (portaddr + rxd );
Putb (CH );
Incount ++; // records the number of data records received.
Outportb (icreg, EOI );
};
Void serinit (INT Baud, int configuration)
{
Disable ();
Outportb (portaddr + LCR, 0x80 );
Outportb (portaddr + LSB, baud );
Outportb (portaddr + MSB, 0x00 );
Outportb (portaddr + LCR, configuration );
Enable ();
};
Void seropen (void)
{
Vect_com = getvect (portf + 8 );
Disable ();
Inportb (portaddr + rxd );
Inportb (portaddr + MSR );
Inportb (portaddr + LSR );
Inportb (portaddr + IIR );
Outportb (portaddr + IER, ierv );
Outportb (portaddr + MCR, out2 | ERTs | edtr );
Outportb (imaskreg, inportb (imaskreg )&(~ (1 <portf )));
Setvect (portf + 8, receiver );
Enable ();
};
Void serclose (void)
{
Disable ();
Outportb (portaddr + IER, 0 );
Outportb (portaddr + MCR, 0 );
Outportb (imaskreg, inportb (imaskreg) | (1 <portf ));
Enable ();
Setvect (portf + 8, vect_com );
};
Void getportaddr (INT port)
{
Switch (port)
{
Case 0: portaddr = 0x3f8; break;
Case 1: portaddr = 0x2f8; break;
Case 2: portaddr = 0x3e8; break;
Case 3: portaddr = 0x2e8; break;
}
Portf = (Port = 0 )? 4: 3;
};

Void int ()
{
Unsigned char key, key2;
Port = ComNum-1;
If (PEEK (0x40, port * 2) = 0)
{
Printf ("have no special com .");
Exit (1 );
}
Else
{
Printf ("the used port is: Com % d", comnum );
};
Getportaddr (port); // get the serial port address
Serinit (ser_baud_9600, ser_parity_even | ser_bits_8 | ser_stop_1); // initialize the serial port and set the baud rate.
Seropen ();
Do
{

If (kbhit ())
{
Key2 = getch ();
If (key2 = 27) {break ;}

};
Key = getb ();
If (key! = 0xff)
{
Printf ("% x", key );
File * FP;
Fp = fopen ("C:/receivedata. dat", "AB"); // you can select the file path and file name for receiving data.
If (FP = NULL) printf ("file open error! ");
// Fputc (Key, FP );
Fwrite (& Key, sizeof (unsigned char), 1, FP );
Fclose (FP );

}

} While (key2! = 27 );
Serclose ();

// Printf ("% d Char has been encoded ed", incount );
// Printf ("% d Char has been sended", outcount );
// Printf ("sum = % d", sum );
}

Void main () // COM1 receive
{
Printf ("Please input the receive comnum :( 1 ~ 4 )");
Cin> comnum;
Printf ("the current COM set is: 9600, even, data bits 8, stop 1 ,");
INT ();

Printf ("over ");
Exit (0 );
};

 

# Include <dos. h>
# Include <bios. h>
# Include <stdio. h>
# Include <math. h>
# Include <conio. h>
# Include <graphics. h>
# Ifdef _ cplusplus
# DEFINE _ cppargs
# Else
# DEFINE _ cppargs
# Endif
# Define ser_rbf 0
# Define ser_thr 0
# Define ser_ier 1
# Define ser_iir 2
# Define ser_lcr 3
# Define ser_mcr 4
# Define ser_lsr 5
# Define ser_msr 6
# Define ser_dll 0
# Define ser_dlh 1

# Define ser_baud_1200 96
# Define ser_baud_2400 48
# Define ser_baud_9600 12
# Define ser_baud_19200 6
# Define ser_gp02 8
# Define com_1 0x3f8
# Define com_2 0x2f8/*/base port address of Port 1 */
# Define com_3 0x3e8
# Define com_4 0x2e8
# Define ser_stop_1 0/*/1 stop bit per character */
# Define ser_stop_2 4/*/2 Stop bits per character */
# Define ser_bits_5 0/*/send 5 Bit characters */
# Define ser_bits_6 1/*/send 6 bit characters */
# Define ser_bits_7 2/*/send 7 bit characters */
# Define ser_bits_8 3/*/send 8 bit characters */
# Define ser_parity_none 0/*/No parity */
# Define ser_parity_odd 8/*/odd parity */
# Define ser_parity_even 24/*/Even Parity */
# Define ser_div_latch_on 128/*/used to turn Reg into divisor latch */
# Define pic_imr 0x21/*/PIC's Interrupt Mask Reg .*/
# Define pic_rn 0x20/*/PIC's interupt control Reg .*/
# Define int_ser_port_0 0x0c/*/port 0 interrupt COM 1 & 3 */
# Define int_ser_port_1 0x0b/*/port 0 interrupt com 2 & 4 */
# Define serial_buff_size 128/*/current size of circulating Receive Buffer */

Void interrupt far (* old_isr) (_ cppargs);/** // */holds old COM port interrupt handler */

Char ser_buffer [serial_buff_size];/** // */The Receive Buffer */

Int ser_end =-1, ser_start =-1;/** // */indexes into Receive Buffer */
Int ser_ch, char_ready = 0;/** // */current character and ready flag */
Int old_int_mask;/** // */The old Interrupt Mask on the PIC */
Int open_port;/** // */The currently open port */
Int serial_lock = 0;/** // */serial ISR semaphore so the buffer */
/** // */Isn' t altered will it is being written */
/** // */To by the ISR */

/** // * ------------- Write the serial port -----------------*/
Void interrupt far serial_isr (_ cppargs)
{
Serial_lock = 1;
Ser_ch = Indium (open_port + ser_rbf );
If (++ ser_end> SERIAL_BUFF_SIZE-1)
Ser_end = 0;
Ser_buffer [ser_end] = ser_ch;

++ Char_ready;
Outp (pic_rn, 0x20 );
Serial_lock = 0;

}

Int ready_serial ()
{
Return (char_ready );

}

/** // * -------------- Read the serial port --------------*/

Int serial_read ()
{
Int ch;
While (serial_lock ){}
If (ser_end! = Ser_start)
{
If (++ ser_start> SERIAL_BUFF_SIZE-1)
Ser_start = 0;
Ch = ser_buffer [ser_start];
Printf ("% x", CH );
If (char_ready> 0)
-- Char_ready;
Return (CH );

}
Else
Return (0 );

}

/** // * -------------- Write the serial port -----------------*/
Serial_write (char ch)
{
While (! (Indium (open_port + ser_lsr) & 0x20 )){}
ASM CLI
Outp (open_port + ser_thr, CH );
ASM STI
}

/** // * ----------- Initialize the serial port ---------------*/
Open_serial (INT port_base, int Baud, int configuration)
{
Open_port = port_base;
Disable ();
Outp (port_base + ser_lcr, ser_div_latch_on );
Outp (port_base + ser_dll, baud );
Outp (port_base + ser_dlh, 0 );
Outp (port_base + ser_lcr, configuration );
Outp (port_base + ser_mcr, ser_gp02 );
Outp (port_base + ser_ier, 1 );
If (port_base = com_1 | port_base = com_3)
{
Old_isr = _ dos_getvect (int_ser_port_0 );
_ Dos_setvect (int_ser_port_0, serial_isr );
Printf ("opening communications channel COM port #1/3 ");

}
Else
{
Old_isr = _ dos_getvect (int_ser_port_1 );
_ Dos_setvect (int_ser_port_1, serial_isr );
Printf ("opening communications channel COM port #2/4 ");
}
Old_int_mask = Indium (pic_imr );
Outp (pic_imr, (port_base = com_1 )? (Old_int_mask & 0xef): (old_int_mask & 0xf7 ));
Enable ();
}
/** // * ------------- Close the serial port --------------*/
Close_serial (INT port_base)
{
Outp (port_base + ser_mcr, 0 );
Outp (port_base + ser_ier, 0 );
Outp (pic_imr, old_int_mask );
If (port_base = com_1)
{
_ Dos_setvect (int_ser_port_0, old_isr );
Printf ("Closing communications channel COM port #1 .");
}
Else
{
_ Dos_setvect (int_ser_port_1, old_isr );
Printf ("Closing communications channel COM port #2 .");
}

}

/** // * ------------- Send application ----------------*/

Void main (INT argc, char * argv [])
{

Char CH, press;
Int done = 0;
File * FP;
Argc = 2;
// Argv [1] = "C:/comfile. c ";
If (argc <2)
{
Printf ("Usage: Display filename.wav !!! ");
// Exit (1 );
}
If (FP = fopen (argv [1], "R + B") = NULL)
{
Printf ("cannot open the file ");
// Exit (0 );
}
Fseek (FP, 0, seek_set );
Open_serial (com_1, ser_baud_9600, ser_parity_even | ser_bits_8 | ser_stop_1 );
Printf ("com: 1; BPS: 9600; Parity: Even; bits: 8; stop bit: 1 ");
Printf ("press any key to begin sending ");
Getch ();
// Serial_write (''); // This statement can be used to send a single character

While (! Done & Ch! = EOF) // starts to send the file
{
Ch = fgetc (FP );
// If (CH = EOF) serial_write (27 );
Serial_write (CH );
Delay (30 );
If (kbhit ())
{
Press = getch ();
If (Press = 27)
{
Serial_write (27 );
Done = 1;
}
}
}

Close_serial (com_1 );
Fclose (FP );
}

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.