📄 winreg2.c
字号:
#include <time.h>
#include <ctype.h>
#include <windows.h>
#include <winbase.h>
#include <stdlib.h>
#include <stdio.h>
#include "Comm32.h"
#define WINAPI __stdcall
int __stdcall read_hold_reg(int reg_num,int slave,int port,int baud,int parity)
{
time_t start,finish;
double diff_time;
int bytes_required = 7;
int bytes_received = 0;
int rx_char = -1;
int timeout = 5;
unsigned int temp,n;
int function = 3;
int offset = reg_num -1;
int offset_hi = offset >> 8;
int offset_lo = offset & 0x00FF;
int numreg_hi = 0;
int numreg_lo = 1;
unsigned char crc_lo = 0;
unsigned char crc_hi = 0;
int reg_contents;
static unsigned char mess[8];
static unsigned char recv[20];
HANDLE ComHandle;
DWORD BytesRead;
mess[0] = slave;
mess[1] = function;
mess[2] = offset_hi;
mess[3] = offset_lo;
mess[4] = numreg_hi;
mess[5] = numreg_lo;
/****************************** open comport *********************/
ComHandle = Open_Comport (port,baud,parity); /* setup for no parity */
if(ComHandle == 0)
{
/* failed to open port */
return 1;
}
/*****************************************************************/
temp = crc(mess,0,6);
crc_hi = temp >> 8 ;
crc_lo = temp & 0x00FF;
mess[6] = crc_hi;
mess[7] = crc_lo;
mess[8] = NULL;
/********************** purge input buffer ************************/
/********************* start timeout timer ************************/
time(&start); /* save the time the message was sent */
/******************************************************************/
/*************************** send packet to plc **************************/
Write_Comport(ComHandle,8,mess);
/*************************************************************************/
while(bytes_received < bytes_required)
{
/******************************** read a byte from plc **********************/
Read_Comport(ComHandle,&BytesRead,1,&rx_char);
/****************************************************************************/
if (BytesRead != 0 )
{
recv[bytes_received] = rx_char;
bytes_received++;
}
time(&finish);
diff_time = difftime(finish,start);
if(diff_time > timeout)
{
/* message box timeout */
printf("timeout");
break;
}
}
temp = recv[3];
temp = temp << 8;
temp = temp | recv[4];
reg_contents = temp;
recv[7] = NULL;
/************************* Close Comport **************************/
Close_Comport(ComHandle);
/*************************************************************************/
return temp;
}
/*
*****************************************************************************
***************************** [ BEGIN: crc ] ******************************
*****************************************************************************
INPUTS:
buf -> Array containing message to be sent to controller.
start -> Start of loop in crc counter, usually 0.
cnt -> Amount of bytes in message being sent to controller/
OUTPUTS:
temp -> Returns crc byte for message.
COMMENTS:
This routine receives the data message to be sent down to the controller
and calculates the crc high and low byte of that message.
*****************************************************************************
*/
unsigned int crc(unsigned char buf[],int start,int cnt)
{
int i,j;
unsigned temp,temp2,flag;
temp=0xFFFF;
for (i=start; i<cnt; i++){
temp=temp ^ buf[i];
for (j=1; j<=8; j++){
flag=temp & 0x0001;
temp=temp >> 1;
if (flag) temp=temp ^ 0xA001;
}
}
/*
** Reverse byte order.
*/
temp2=temp >> 8;
temp=(temp << 8) | temp2;
return(temp);
}
/*
************************** [ END: crc ] ************************************
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -