background image

一个 c 语言的串口通信程序范例

最近接触一个项目,用 HL-C1C 激光位移传感器+易控组态软件完成生产线高度跳变检测,
好久没有接触 c c#,一些资料,找来做个记录,也许大家用的着

#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <string.h>

#define COM232  0x2f8
#define COMINT  0x0b

#define MaxBufLen      500
#define Port8259       0x20
#define EofInt        0x20

static int  comportaddr;
static char intvectnum;
static unsigned char maskb;
static unsigned char Buffer[MaxBufLen];
static int  CharsInBuf,CircIn,CircOut;

static void (interrupt far *OldAsyncInt)();
static void interrupt far AsyncInt(void);

void Init_COM(int ComPortAddr, unsigned char IntVectNum, int Baud,
       unsigned char Data, unsigned char Stop, unsigned char Parity)
{
 unsigned char High,Low;
 int f;

 comportaddr=ComPortAddr;
 intvectnum=IntVectNum;

 CharsInBuf=0;CircIn=0;CircOut=0;

 
 f=(Baud/100);
 f=1152/f; High=f/256;
 Low=f-High*256;
 outp(ComPortAddr+3,0x80);