在VC6.0环境下编写串口程序
在VC下不能调用dos.h和bois.h 甚是疑惑
如果我要写串口通信程序又不用到MFC的类,我该用什么函数呢?
下面是我写的程序 但在vc不能编 曾尝试过用TC编译但它说头文件都错了 极度郁闷
请各位帮帮忙!
# include "dos.h "
# include "stdlib.h "
# include "stdio.h "
# define PORT 1
void port_init(int port,unsigned char code);
void sport(int port,char c);
unsigned int check_stat(int port);
unsigned char rport(int port);
void send_file(char *fname);
void send_file_name(char *f);
void wait(int port);
void port_init(int port,unsigned char code)
{
union REGS r;
r.x.dx=port; //define PORT
r.h.ah=0;//初始化端口功能
r.h.al=code;//初始参数
int86(0x14,&r,&r);//调用中断
}
void sport(int port,char c)
{
union REGS r;
r.x.dx=port;
r.h.al=c;//要发送的字符
r.h.ah=1;//初始化端口功能
int86(0x14,&r,&r);
if(r.h.ah&128)//发送不成功 则打印错误信息
{
printf( "Send error detected in serial port ");
exit(1);
}
}
//检测端口状态函数
unsigned int check_stat(int port)
{
union REGS r;
r.x.dx=port;
r.h.ah=3;//初始化端口功能
int86(0x14,&r,&r);
return r.x.ax;
}
//接收字符函数
unsigned char rport(int port)
{
union REGS r;
//等待一个字符,若“数据就绪”位为0则等待 为1 或按任意键推出
while(!(check_stat(port)&256))
{
if(kbhit())
gerch();
exit(1);
}
r.x.dx=port;
r.h.ah=2;//初始化端口功能
int86(0x14,&r,&r);
if(r.h.ah&128)
{
printf( "read error ");
}
return r.h.al;
}
/////////////////////////////////////////////
//发送文件函数
void send_file(char *fname)
{
FILE *fp;
char ch;
int i;
union
{
char c[sizeoff(long)];
long count;
}cnt;
if(!(fp=fopen(fname, "rb ")))
{
printf( "cannot open input file\n ");
exit(1);
}
//统计文件字节数
cnt.count=filesize(fp);
printf( "file size:%ld\n ",cnt.count);
send_file_name(fname); //发送文件名
//发送字节
for(i=0;i <sizeoff(long);i++)
{
sport(PORT,cnt.c[i]);
wait(PORT);
}
i=0;
do
{
ch=getc(fp);
if(ferror(fp))
{
printf( "error reading input file.\n ");
break;
}
//等待直到接收方就绪
if(!feof(fp))
{
sport(PORT,ch);
wait(PORT);
}
i++;
if(i==1024)
{
printf( ". ");
i=0;
}
}while(!feof(fp));
fclose(fp);
}
void send_file_name(char *f)
{
printf( "trasmitter waitting...\n ");
do
{
sport(PORT, '? '); //发送询问“?”
}while(!kbhit()&&!(check_stat(PORT)&256));//有键盘按下或者接收方就绪 停止发送
if(kbhit())
{
getch();
exit();
}
wait(PORT);
printf( "sending the file%s\n\n ",f);
while(*f)
{
sport(PORT,*f++);
wait(PORT);
}
sport(PORT, '\0 ');//结束符号“\0”
wait(PORT);
}
//等待认可
void wait(int port)
{
if(rport(port)!= '. ')
{
printf( "communication error\n ");
exit(1);
}
}
void main(void)
{
char *file_send;
*file_send= "1.txt "; //要发送的文件
port_init(PORT,0x83);//初始化端口
send_file(file_send);//发送文件
}
[解决办法]
代码在下面,2002年写的了,刚才看了一遍,实在惨不忍睹,简直想从头些,当时是赶任务,做完了就没再理会。。。。献丑了
// commctl.h: interface for the commctl class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_COMMCTL_H__8BA1CDBD_85B3_42A8_B757_C33560A636F3__INCLUDED_)
#define AFX_COMMCTL_H__8BA1CDBD_85B3_42A8_B757_C33560A636F3__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "stdio.h "
#include "stdlib.h "
#include "string.h "
#include "windows.h "
class commctl
{
public:
commctl();
virtual ~commctl();
BOOL PortOpen(void);
BOOL PortClose(void);
WORD PortGet(void);
BOOL PortSet(WORD port = 1);
BOOL ReadPort(char *);
BOOL WritePort(char *);
public:
char Name[255];
long rdBuf;
protected:
HANDLE hComm;
WORD Port;
DCB dcb;
private:
BOOL Init();
BOOL Free();
};
#endif // !defined(AFX_COMMCTL_H__8BA1CDBD_85B3_42A8_B757_C33560A636F3__INCLUDED_)
// commctl.cpp: implementation of the commctl class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h "
#include "CallMsgDll.h "
#include "commctl.h "
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
commctl::commctl() {
if(!Init()) {
MessageBox(NULL, "Initialize serial port failed. ", "Error ",0);
return;
}
}
commctl::~commctl() {
Free();
}
BOOL commctl::Init(void) {
hComm = 0;
Port = 0;
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fBinary = true;
dcb.fParity = false;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
strcpy(Name, "NSMS Communication Controler.Created by Neil. ");
rdBuf = 1024;
return (TRUE);
}
BOOL commctl::Free(void) {
if(hComm)
CloseHandle(hComm);
return (TRUE);
}
BOOL commctl::PortClose(void) {
if(hComm) {
CloseHandle(hComm);
hComm = 0;
}
return (TRUE);
}
WORD commctl::PortGet(void) {
return (Port);
}
BOOL commctl::PortOpen(void) {
if(hComm)
return (TRUE);
return (FALSE);
}
BOOL commctl::PortSet(WORD port) {
COMMTIMEOUTS CommTimeouts;
char * tmpPort,* tmp;
if(!port)
return (FALSE);
Port = port;
tmpPort = (char *) malloc(sizeof(char) * 10);
tmp = (char *) malloc(sizeof(char) * 10);
memset(tmpPort, '\0 ',10);
memset(tmp, '\0 ',10);
strcpy(tmpPort, "COM ");
strcat(tmpPort,itoa(Port,tmp,10));
free(tmp);
hComm = CreateFile(tmpPort,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);
if(hComm == INVALID_HANDLE_VALUE)
return (FALSE);
SetCommMask(hComm, EV_RXCHAR|EV_TXEMPTY);
SetupComm(hComm,4096,2048);
CommTimeouts.ReadIntervalTimeout = 500;
CommTimeouts.ReadTotalTimeoutMultiplier = 500;
CommTimeouts.ReadTotalTimeoutConstant = 500;
CommTimeouts.WriteTotalTimeoutMultiplier = 500;
CommTimeouts.WriteTotalTimeoutConstant = 500;
if (!SetCommTimeouts(hComm, &CommTimeouts)) {
CloseHandle(hComm);
return (FALSE);
}
SetCommState(hComm,&dcb);
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
return (TRUE);
}
BOOL commctl::ReadPort(char * str) {
DWORD dwRead = 0;
BOOL fWaitingOnRead = FALSE;
OVERLAPPED osRead = {0};
BOOL fRes;
osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!osRead.hEvent)
return FALSE;
if(!fWaitingOnRead) {
if (!ReadFile(hComm,str,rdBuf,&dwRead,&osRead)) {
if (GetLastError() != ERROR_IO_PENDING)
fRes = FALSE;
else {
fWaitingOnRead = TRUE;
if (!GetOverlappedResult(hComm, &osRead, &dwRead, TRUE))
fRes = FALSE;
else
fRes = TRUE;
}
}
else
fRes = TRUE;
}
CloseHandle(osRead.hEvent);
if(fRes)
return(TRUE);
return FALSE;
}
BOOL commctl::WritePort(char * str) {
DWORD dwWritten;
BOOL fWaitingOnWrite = FALSE;
OVERLAPPED osWrite = {0};
BOOL fRes;
int err = 0;
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!osWrite.hEvent)
return FALSE;
if(!fWaitingOnWrite) {
if (!WriteFile(hComm,str,strlen(str),&dwWritten,&osWrite)) {
err = GetLastError();
if (err != ERROR_IO_PENDING)
fRes = FALSE;
else {
fWaitingOnWrite = TRUE;
if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, TRUE))
fRes = FALSE;
else
fRes = TRUE;
}
}
else
fRes = TRUE;
}
CloseHandle(osWrite.hEvent);
if(fRes)
return(TRUE);
return FALSE;
}