📄 port.cpp
字号:
#define Bus 0xf208 //0x248,0xf228,0xf248,0xf268,0xf308,0xf328,0xf348,0xf368
short parPort=0,old=0;
short portType=0; //0:bus,1:par,2:rs232
short noSend;
short noRead;
#define bufsize 256
#define W0 2 //REVERSE DEFAULT=0
#define R0 4
#define R1 8 //slct in REVERSE DEFAULT=0
#define DIR 0x20
unsigned buffer[bufsize];
short bfs=0,bfe=0;
#include <windows.h>
#include <stdio.h>
#include <stdio.h>
#include <conio.h>
#define inp _inp
#define outp _outp
#include "at45.h"
short AT45_Set232Baud(long baud);
HANDLE sfile;
short Open232(short port,long baud)
{
COMMTIMEOUTS CommTimeOuts ;
char s[80];
if (port<1 || port >4) port=2;
sprintf(s,"COM%d:",port);
if ((sfile =CreateFile( s, GENERIC_READ | GENERIC_WRITE,0, NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL )) == (HANDLE) -1 )
return (erPortError) ;
else
{
SetCommMask( sfile, EV_RXCHAR ) ;
SetupComm( sfile, 4096, 4096 ) ;
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF ;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0 ;
CommTimeOuts.ReadTotalTimeoutConstant = 0 ;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0 ;
CommTimeOuts.WriteTotalTimeoutConstant = 5000 ;
SetCommTimeouts( sfile, &CommTimeOuts ) ;
}
DCB dcb ;
dcb.DCBlength = sizeof( DCB ) ;
GetCommState( sfile, &dcb ) ;
sprintf(s,"COM%d:baud=%ld parity=N data=8 stop=1",port,baud);
BuildCommDCB(s,&dcb);
SetCommState( sfile, &dcb ) ;
return 0;
}
short Close232Port()
{
SetCommMask( sfile, 0 ) ;
EscapeCommFunction( sfile, CLRDTR ) ;
PurgeComm( sfile, PURGE_TXABORT | PURGE_RXABORT| PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
CloseHandle( sfile ) ;
return 0 ;
} // end of CloseConnection()
short ReadCommBlock(LPSTR lpszBlock, short nMaxLength )
{
BOOL fReadStat ;
COMSTAT ComStat ;
DWORD dwErrorFlags, dwLength ;
OVERLAPPED rd = {0};
ClearCommError( sfile, &dwErrorFlags, &ComStat) ;
rd.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(rd.hEvent == NULL) return 0;
dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ;
if (dwLength > 0)
{
fReadStat = ReadFile( sfile, lpszBlock,dwLength, &dwLength, &rd ) ;
if (!fReadStat)
{
if (GetLastError() == ERROR_IO_PENDING)
{
if (WaitForSingleObject( rd.hEvent, 1000 ))
dwLength = 0 ;
else
{
GetOverlappedResult( sfile,&rd,&dwLength, FALSE ) ;
rd.Offset += dwLength ;
}
}
else
dwLength = 0 ;
}
}
CloseHandle(rd.hEvent);
return (short)( dwLength ) ;
} // end of ReadCommBlock()
BOOL WriteCommByte( BYTE bByte )
{
BOOL fWriteStat ;
DWORD dwBytesWritten ;
OVERLAPPED wrt = {0};
wrt.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if(wrt.hEvent==NULL) return FALSE;
fWriteStat = WriteFile( sfile, (LPSTR) &bByte, 1, &dwBytesWritten, &wrt ) ;
if (!fWriteStat && (GetLastError() == ERROR_IO_PENDING))
{
// wait for a second for this transmission to complete
if (WaitForSingleObject( wrt.hEvent, 1000 ))
dwBytesWritten = 0 ;
else
{
GetOverlappedResult( sfile,&wrt,&dwBytesWritten, FALSE ) ;
wrt.Offset += dwBytesWritten ;
}
}
CloseHandle(wrt.hEvent);
return ( TRUE ) ;
} // end of WriteCommByte()
short get232Byte(){
char c;
short i=ReadCommBlock(&c, 1 );
if (i==0) return -1;
else return c&0xff;
}
short send232Byte(short dt){
BOOL i=(BOOL) WriteCommByte((unsigned char)dt);
if (i==TRUE) return 0; else return -1;
}
short testReader();
NOMANGLE short CCONV AT45_ClosePort(short port){
if (portType==2)
{
AT45_Set232Baud(9600l);
Close232Port();
}
return 0;
}
short OpenPort(short port,long baud)
{
short i;
noSend=0;
noRead=0;
if (port>0 && port<5) {
i=Open232(port,baud);
portType=2;
} else if (port<11 || port >14) {
if (port==0) port=(short)Bus;
parPort=port;
portType=0;
i=0;
} else
{
port=(port-11)*2;
parPort=0x378;
old=inp(parPort+2)&(0xff-W0-R0-R1-DIR);
outp(parPort+2,old+R0+DIR);
portType=1;
i=0;
}
if (i<0) return i;
if (testReader()==0) return 0;
AT45_ClosePort();
return -1;
}
NOMANGLE short CCONV AT45_OpenPort(short port)
{
if(OpenPort(port,9600)!=0) return 1;
if (port>0 && port<5)
{
AT45_Set232Baud(115200l);
Close232Port();
if(OpenPort(port,115200l)!=0) return 1;
}
return 0;
}
short Par_readByte(short &a){
if (portType==0){
a=inp(Bus+2);
if ((a&0x20)==0) return -2; //Q5
if ((a&0x80)==0) return -1; //Q6
return inp(Bus);
}
outp(parPort+2,old+R0+DIR+R1); //R1=0
a=inp(parPort);
outp(parPort+2,old+R0+DIR); //R1=1
if ((a&0x20)==0) return -2; //Q5
if ((a&0x80)==0) return -1; //Q7
outp(parPort+2,old+DIR); //R0=0
short b=inp(parPort);
outp(parPort,old+R0+DIR); //R0=1
return b;
}
short ckByte(){
short a;
short bf=bfe+1;
if (bf>=bufsize) bf=0;
short ch;
while ((ch=Par_readByte(a))==-2);
if (bf!=bfs)
if (ch!=-1) {
buffer[bfe]=ch;
bfe=bf;
}
return a;
}
short sendByte(short dt){
if (portType==2){
return send232Byte(dt);
}
short a=ckByte();
if ((a&0x20)==0) return -1; //Q5
if ((a&0x40)==0) return -1; //Q6
if (portType==0)
{
outp(Bus,dt);
return 0;
}
outp(parPort,dt);
outp(parPort+2,old+R0+W0);
outp(parPort+2,old+R0);
outp(parPort+2,old+R0+DIR);
return 0;
}
short getByte(){
if (portType==0||portType==1) ckByte();
#ifdef _WINDOWS
if (portType==2) return get232Byte();
#endif
if (bfe==bfs) return -1; else {
short ch=buffer[bfs++];
if (bfs>=bufsize) bfs=0;
return ch;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -