00001
00002
00003
00004
00005
00006
00007
00008 #ifndef MCONTROL_H
00009 #define MCONTROL_H
00010
00011 #include <GNU_defs.h>
00012 #include <realtime.h>
00013 #include <ser.h>
00014
00015
00016
00017 #define MAXPDUDATASIZE 100
00018
00019 #define PDU_AVAIL 5 //number of PDUs allocatable
00020
00021
00022 typedef struct PDUstruct{
00023 u8 function;
00024 u8 data[MAXPDUDATASIZE];
00025 u8 attributes;
00026 int datasize;
00027 }PDUstruct;
00028
00029 typedef struct PDU_Node{
00030 struct PDU_Node *previous;
00031 struct PDU_Node *next;
00032 PDUstruct PDU;
00033 }PDU_Node;
00034
00035 typedef struct PDU_List{
00036 PDU_Node *head;
00037 PDU_Node *tail;
00038 int size;
00039 }PDU_List;
00040
00041 typedef struct modcounters
00042 {
00043 u16 bus_message;
00044 u16 bus_CRC_error;
00045 u16 slave_exception;
00046 u16 slave_message;
00047 u16 no_response;
00048 u16 slave_NAK;
00049 u16 slave_busy;
00050 u16 character_overrun;
00051 }modcounters;
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 typedef struct Modbus_Port{
00064
00065 int parity;
00066 int protocol;
00067 int speed;
00068 int delim;
00069 int address;
00070 int port;
00071 int count;
00072 int idle;
00073 int diagnostic;
00074 u8 status;
00075
00076
00077
00078
00079
00080
00081 modcounters counters;
00082 RTaskNode *RTUtasklet;
00083 RTIME t25;
00084 PDU_List PDUlist;
00085 }Modbus_Port;
00086
00087
00088
00089 #define MODBUS_PORTNUM NUM_COMPORTS
00090
00091
00092 #define ADDCHANGE 1
00093 #define NEWDELIM 2
00094
00095 #define CLEARLOG 8
00096 #define RESTART 0x10
00097
00098
00099
00100 #define BROADCAST 1
00101
00102
00103 #define NULL_FUNCTION 0
00104 #define ALLOCATED 1
00105
00106
00107 #define READ_COILS 1
00108 #define READ_DISCRETE_INPUTS 2
00109 #define READ_HOLDING_REGISTERS 3
00110 #define READ_INPUT_REGISTERS 4
00111 #define RI_DATANUM_BYTES 1
00112
00113 #define WRITE_SINGLE_COIL 5
00114 #define ON_REQUEST 0xff00
00115 #define OFF_REQUEST 0
00116
00117 #define WRITE_SINGLE_REGISTER 6
00118 #define WRITE_SING_RES_SIZE 4
00119
00120 #define WRITE_MULTIPLE_COILS 15
00121
00122 #define WRITE_MULTIPLE_REGISTERS 0x10
00123 #define WRITEM_RESPONSE_BYTES 4
00124 #define WR_DATASTART 5
00125
00126 #define MASK_WRITE_REGISTER 0x16
00127 #define MASK_WRITE_SIZE 6
00128
00129 #define DIAGNOSTICS 8
00130
00131 #define RETURN_QUERY 0
00132 #define RESTART_COMM 1
00133 #define RETURN_DIAGN 2
00134 #define CH_ASCII_IND 3
00135 #define LISTEN_ONLY 4
00136 #define CLEAR_COUNT 0x0a
00137 #define RETURN_BUSM 0x0b
00138 #define COMM_ERR_CNT 0x0c
00139 #define BUS_EXCP_CNT 0x0d
00140 #define SLV_MSG_CNT 0x0e
00141 #define SLV_NO_RESP 0x0f
00142 #define SLV_NAK_CNT 0x10
00143 #define SLV_BUSY_CNT 0x11
00144 #define RETRN_OVERRN 0x12
00145 #define CLEAR_OVERRN 0x14
00146
00147
00148
00149 #define STARTCOUNT 1
00150 #define TRIGBITS 6
00151 #define ASTOPBIT 8
00152 #define ARELOADBIT 0x10
00153 #define COUNTENABLE 0x80
00154
00155
00156 #define BAUD_RATE_BIT 0x01
00157 #define PARITY_TYPE_BIT 0x08
00158 #define PARITY_ON_BIT 0x10
00159 #define PROTOCOL_BIT 0x80
00160
00161 #define MODBUS_DEFAULT BAUD_RATE_BIT|PARITY_TYPE_BIT
00162 #define MODBUS_ADDRESS_DEFAULT 2
00163
00164
00165 #define ILLEGAL_FUNCTION 1
00166 #define ILLEGAL_DATA_ADDRESS 2
00167 #define ILLEGAL_DATA_VALUE 3
00168
00169 PDU_Node __attribute__((far)) *GetPDUnode(void);
00170 void __attribute__((far)) DumpPDUQueue(Modbus_Port *modport);
00171 int __attribute__((far)) Dump_Frames(Modbus_Port *modport);
00172 int __attribute__((far)) PDU_exe(Modbus_Port *modport, PDUstruct *PDU);
00173 int __attribute__((far)) EnQueue_PDU(PDU_Node *newnode, Modbus_Port *modport);
00174 PDUstruct __attribute__((far)) *DeQueue_PDU(Modbus_Port *modport);
00175 Modbus_Port __attribute__((far)) *Modbus_Device_Open(int port, int speed,int address,
00176 int parity, int protocol,char delim);
00177 int __attribute__((far)) Modbus_Diagnostic(Modbus_Port *modport,u8 *data, int datasize);
00178 int __attribute__((far)) Modbus_Slave_Control(Modbus_Port *modport);
00179 int __attribute__((far)) Modbus_Device_Close(Modbus_Port *modport);
00180
00181
00182
00183
00184
00185
00186
00187 static inline Modbus_Port *Modport_Restart(Modbus_Port *port){
00188 return(Modbus_Device_Open(port->port,port->speed,
00189 port->address,port->parity,port->protocol,port->delim));
00190 }
00191
00192 #include <protocol/mserial.h>
00193 static inline void print_modbus_state(int comport,Modbus_Port *port){
00194 ser_printf(comport,"address: %u\r\n",(long)port->address);
00195 ser_printf(comport,"protocol: %s\r\n",(port->protocol==RTU_PROTOCOL)?"RTU":"ASCII");
00196 ser_printf(comport,"speed: %u\r\n",(long)port->speed);
00197 ser_printf(comport,"delim: %x\r\n",(long)port->delim);
00198
00199 if(port->parity==NO_PARITY)
00200 ser_printf(comport,"parity: OFF\r\n");
00201 else
00202 ser_printf(comport,"parity: %s\r\n",(port->parity==EVEN_PARITY)?"EVEN":"ODD");
00203 }
00204
00205 #endif
00206
00207
00208
00209
00210
00211