#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#if !defined C104P_C
#define C104P_C
#endif
#include "\hxn\lib\moxa\C104P.H" //
void cdecl port_init(const int port)
{
if(!sio_open (port))
{
if (sio_ioctl(port,B9600,BIT_8| P_EVEN| STOP_1)){
printf("Port #%d IOCTL error !\n", port + 1);
delay(500);
exit(0);
}
sio_lctrl(port, C_DTR | C_RTS); /* set DTR RTS on */
sio_flowctrl(port,0); /* set NO flow control */
}
else
{
printf("Port #%d IOCTL error !\n", port + 1);
delay(500);
exit(0);
}
}
void cdecl waijian_init(const int port)
{
if(!sio_open (port))
{
if (sio_ioctl(port,B1200,BIT_7| P_EVEN| STOP_1)){
printf("Port #%d IOCTL error !\n", port + 1);
delay(500);
exit(0);
}
sio_lctrl(port, C_DTR | C_RTS); /* set DTR RTS on */
sio_flowctrl(port,0); /* set NO flow control */
}
else
{
printf("Port #%d IOCTL error !\n", port + 1);
delay(500);
exit(0);
}
}
void cdecl lattice_init(const int port)
{
if(!sio_open (port))
{
if(sio_ioctl(port,B9600,BIT_8| P_NONE| STOP_1)){
printf("Port #%d IOCTL error !\n", port + 1);
delay(500);
exit(0);
}
sio_lctrl(port, C_DTR | C_RTS); /* set DTR RTS on */
sio_flowctrl(port,0); /* set NO flow control */
}
else
{
printf("Port #%d IOCTL error !\n", port + 1);
delay(500);
exit(0);
}
}
unsigned char cdecl r_status(const int port)
{
return (sio_lstatus(port));
}
void cdecl sport(const int port,const char send)
{
int i;
while(!( r_status(port)&C_RTS));
sio_putch(port,send);
}
void cdecl sport2(const int port, const unsigned char send)
{
//for(i=0;i<Sport2Delay;i++);
while(!( r_status(port)&C_RTS));
//for(i=0;i<Sport2Delay;i++);
sio_putch(port,send);
}
/*
void cdecl send_string(const int port,char *str)
{
while(*str)
{
sport(port,*str);
str++;
}
}
void cdecl SendString(const int port,char *str, const int num)
{
int i;
for(i=0; i<num; i++){
sport(port,*str);
str++;
}
}
void cdecl send_string2(const int port, char *str, const int num)
{
int i, j;
for(i=0; i<num; i++){
for(j=0; j<200; j++);
sport(port, str[i]);
}
}
void SendStringEcho(int port,char *str, int num)
{
#define TryTimeSport 40
int i, j;
for(i=0; i<num; i++){
sport(port,*(str+i));
j=0;
while(j<TryTimeSport){
if( rport(port) == 0xF0 ){
break;
}
j++;
};
}
}
void SendStringEcho3(int port,char *str, int num)
{
#define TryTimeSport 400
int i, j;
for(i=0; i<num; i++){
sport(port,*(str+i));
j=0;
while(j<TryTimeSport){
if( rport(port) == 0xF0 ){
break;
}
j++;
};
}
}
unsigned char rport(const int port)
{
int i;
no_data = 0;
for(i=0;i<1000; i++){
if( r_status(port) & C_DTR ){
return(sio_getch(port);
}
}
no_data = 1;
return(0);
}
*/
void cdecl send_string(const int port,char *str)
{
int num;
num = strlen(str);
sio_putb(port, str, num);
// sio_putb(port, str, num);
}
void cdecl SendString(const int port,char *str, const int num)
{
sio_putb(port, str, num);
}
void cdecl send_string2(const int port, char *str, const int num)
{
sio_putb(port, str, num);
}
void SendStringEcho(const int port,char *str, const int num)
{
sio_putb(port, str, num);
}
void SendStringEcho3(const int port,char *str, const int num)
{
sio_putb(port, str, num);
}
unsigned char rport(const int port)
{
int i;
no_data = 0;
if( r_status(port) & C_DTR){
return(sio_getch(port));
}
no_data = 1;
return(0);
}
unsigned char rports(const int port)
{
int i;
no_data = 0;
if( r_status(port) & C_DTR)
{
return( sio_getch(port));
}
no_data = 1;
return(0);
}
unsigned char rport2(const int port,unsigned char *chget)
{
int i;
for(i=0;i<RPortTryTime; i++){
if( r_status(port) & C_DTR ){
*chget=sio_getch(port);
return(HAVEDATA);
}
}
return(NODATA);
}
char * cdecl rec_string(const int port,const int bytes) // return no_data == 1 && 0; when fault
{
static char buf[500];
static char ch;
int i;
Recs_Error = 0;
memset(buf,'\0',500);
for(i=0;i<bytes;i++) {
ch = rport(port);
if( !no_data ) *(buf+i) = ch;
else{
Recs_Error = 1;
return(0);
}
}
*(buf+i)='\0';
return (buf);
}
char * cdecl RecStringEcho(const int port,const int bytes) /* Error time.Resc_Error =1 */
{
static char buf[700];
char chls;
int i,j;
Recs_Error = 0;
for(i=0;i<bytes;i++){
chls=rport(port);
if(!no_data) break;
if(!no_data){
buf[i] = chls;
sport(port,0xF0);
} else {
Recs_Error = 1;
return(0);
}
}
buf[i]='\0';
return (buf);
}
char * cdecl RecStringEcho3(const int port,const int bytes) /* Error time.Resc_Error =1 */
{
static char buf[801];
char chls;
int i,j;
Recs_Error = 0;
//memset(buf,'\0',200); //in main-sub com need it!
//disable();
for(i=0;i<bytes;i++){
// for(j=0; j<TryTimeRPort; j++){
chls=rport(port);
if(!no_data) break;
// }
//printf("%i",j);
if(!no_data){
buf[i] = chls;
sport(port,0xF0);
} else {
Recs_Error = 1;
return(0);
}
}
//enable();
buf[i]='\0';
return (buf);
}
char cdecl McRecString2(const int port, unsigned char *str, const int bytes) /* Error time.Resc_Error =1 */
{
char chls;
char *strp;
int i,j;
strp=str;
//send alloc
sport2(port, MC_REQDATA);
//wait answer
//rport2(port,&chls);
for(i=0;i<WaitCommandAnswerTime;i++){
if(i==(WaitCommandAnswerTime-1)){
sport2(port,CLEARDATA);
return(1);
}
if(rport2(port,&chls)==HAVEDATA){
sport2(port,CLEARDATA);
break;
}
}
if(chls!=SC_SENDANSWER){
return(chls);
}
//wait datahead
for(i=0;i<WaitDataHeadTime;i++){
if(i==(WaitDataHeadTime-1)) return(3);
rport2(port,&chls);
if(chls==DATAHEAD)
break;
}
//receive data
for(i=0;i<bytes;i++){
rport2(port, strp++);
}
//add verfy
if(Verify1(str, bytes)==OK){
sport2(port, MC_RECOK);
return(0);
}else{
sport2(port, MC_RECNOT);
return(4);
}
}
char cdecl McRecString3(const int port, unsigned char *str, const int bytes) /* Error time.Resc_Error =1 */
{
char chls;
char *strp;
char *strg;
int i,j;
strp=str;
rport2(port,&chls);
//send alloc
sport2(port, MC_REQDATA);
//wait answer
//rport2(port,&chls);
for(i=0;i<WaitCommandAnswerTime;i++){
if(i==(WaitCommandAnswerTime-1)){
sport2(port,CLEARDATA);
return(1);
}
if(rport2(port,&chls)==HAVEDATA){
sport2(port,CLEARDATA);
break;
}
}
if(chls!=SC_SENDANSWER){
return(chls);
}
//rec data
strg=RecStringEcho3(port,bytes); /* Error time.Resc_Error =1 */
//add verfy
if((Resc_Error!=1)&&(Verify1(strg, bytes)==OK)){
sport2(port, MC_RECOK);
for(i=0;i<bytes;i++)
strp[i]=strg[i];
return(0);
}else{
sport2(port, MC_RECNOT);
return(4);
}
}
void cdecl MakeVerify(unsigned char *str, const int bytes)
{
int i;
union VR{
unsigned char *str;
unsigned int ui;
} vr;
vr.ui=0;
for(i=0;i<bytes-2;i++)
vr.ui+=str[i];
str[bytes-2]=vr.str[0];
str[bytes-1]=vr.str[1];
}
int cdecl Verify(unsigned char *str, const int bytes)
{
int i;
union VR{
unsigned char *str;
unsigned int ui;
} vr;
unsigned int ils=0;
for(i=0;i<bytes-2;i++)
ils+=str[i];
vr.str[0]=str[bytes-2];
vr.str[1]=str[bytes-1];
if(vr.ui==ils) return(OK);
else return(NOT);
}
void cdecl MakeVerify1(unsigned char *str, const int bytes)
{
int i;
str[bytes-1]=0x00;
for(i=0;i<bytes-1;i++)
str[bytes-1]^=str[i];
}
int cdecl Verify1(unsigned char *str, const int bytes)
{
int i;
unsigned char chls=0x00;
for(i=0;i<bytes-1;i++)
chls^=str[i];
if(str[bytes-1]==chls) return(OK);
else