SHOW:
|
|
- or go back to the newest paste.
| 1 | import serial | |
| 2 | import random | |
| 3 | ||
| 4 | #Sub Program | |
| 5 | #CRC Calculation | |
| 6 | def CRCcal(msg): | |
| 7 | #CRC (Cyclical Redundancy Check) Calculation | |
| 8 | CRC = 0xFFFF | |
| 9 | CRCHi = 0xFF | |
| 10 | CRCLo = 0xFF | |
| 11 | CRCLSB = 0x00 | |
| 12 | for i in range(0, len(msg)-2,+1): | |
| 13 | CRC = (CRC ^ msg[i]) | |
| 14 | for j in range(0, 8): | |
| 15 | CRCLSB = (CRC & 0x0001); | |
| 16 | CRC = ((CRC >> 1) & 0x7FFF) | |
| 17 | ||
| 18 | if (CRCLSB == 1): | |
| 19 | CRC = (CRC ^ 0xA001) | |
| 20 | CRCHi = ((CRC >> 8) & 0xFF) | |
| 21 | CRCLo = (CRC & 0xFF) | |
| 22 | return (CRCLo,CRCHi) | |
| 23 | ||
| 24 | ||
| 25 | #CRC Valdation | |
| 26 | def CRCvalid(resp): | |
| 27 | CRC = CRCcal(resp) | |
| 28 | if (CRC[0]==resp[len(resp)-2]) & (CRC[1]==resp[len(resp)-1]):return True | |
| 29 | return False | |
| 30 | ||
| 31 | #Modbus Function Code 6 = Preset Single Register | |
| 32 | def Func6Modbus(slave,start,value): | |
| 33 | Slave_Address = slave | |
| 34 | Function = 6 | |
| 35 | Starting_Address = start | |
| 36 | message = [0 for i in range(6 + 2)] | |
| 37 | ||
| 38 | #index0 = Slave Address | |
| 39 | message[0] = (Slave_Address & 0xFF) | |
| 40 | #index1 = Function | |
| 41 | message[1] = (Function & 0xFF) | |
| 42 | #index2 = Starting Address Hi | |
| 43 | message[2] = ((Starting_Address >> 8) & 0xFF) | |
| 44 | #index3 = Starting Address Lo | |
| 45 | message[3] = (Starting_Address & 0xFF) | |
| 46 | #Data Hi, index4 and index9 | |
| 47 | message[4] = ((value >> 8) & 0xFF) | |
| 48 | #Data Lo, index5 and index10 | |
| 49 | message[5] = value & 0xFF | |
| 50 | ||
| 51 | #CRC (Cyclical Redundancy Check) Calculation | |
| 52 | CRC = CRCcal(message) | |
| 53 | ||
| 54 | #index11= CRC Lo | |
| 55 | message[len(message) - 2] = CRC[0]#CRCLo | |
| 56 | #index12 = CRC Hi | |
| 57 | message[len(message) - 1] = CRC[1]#CRCHi | |
| 58 | ||
| 59 | if ser.isOpen: | |
| 60 | ser.write("".join(chr(h) for h in message))
| |
| 61 | reading = ser.read(8) | |
| 62 | response = [0 for i in range(len(reading))] | |
| 63 | for i in range(0, len(reading)): | |
| 64 | response[i] = ord(reading[i]) | |
| 65 | ||
| 66 | if len(response)==8: | |
| 67 | CRCok = CRCvalid(response) | |
| 68 | if CRCok & (response[0]==slave) & (response[1]==Function):return True | |
| 69 | return False | |
| 70 | ||
| 71 | #Modbus Function Code 16 = Preset Multiple Registers | |
| 72 | def Func16Modbus(slave,start,values): | |
| 73 | Slave_Address = slave | |
| 74 | Function = 6 | |
| 75 | Starting_Address = start | |
| 76 | NumberofRegisters = len(values) | |
| 77 | Byte_Count = NumberofRegisters * 2 | |
| 78 | message = [0 for i in range(9 + 2 * NumberofRegisters)] | |
| 79 | ||
| 80 | #index0 = Slave Address | |
| 81 | message[0] = (Slave_Address & 0xFF) | |
| 82 | #index1 = Function | |
| 83 | message[1] = (Function & 0xFF) | |
| 84 | #index2 = Starting Address Hi | |
| 85 | message[2] = ((Starting_Address >> 8) & 0xFF) | |
| 86 | #index3 = Starting Address Lo | |
| 87 | message[3] = (Starting_Address & 0xFF) | |
| 88 | #index4 = Number of Registers Hi | |
| 89 | message[4] = ((NumberofRegisters >> 8) & 0xFF) | |
| 90 | #index5 = Number of Registers Lo | |
| 91 | message[5] = (NumberofRegisters & 0xFF) | |
| 92 | #index6 = Byte Count | |
| 93 | message[6] = (Byte_Count & 0xFF) | |
| 94 | ||
| 95 | for i in range(0, NumberofRegisters): | |
| 96 | #Data Hi, index7 and index9 | |
| 97 | message[7 + 2 * i] = ((values[i] >> 8) & 0xFF) | |
| 98 | #Data Lo, index8 and index10 | |
| 99 | message[8 + 2 * i] = values[i] & 0xFF | |
| 100 | ||
| 101 | #CRC (Cyclical Redundancy Check) Calculation | |
| 102 | CRC = CRCcal(message) | |
| 103 | ||
| 104 | #index11= CRC Lo | |
| 105 | message[len(message) - 2] = CRC[0]#CRCLo | |
| 106 | #index12 = CRC Hi | |
| 107 | message[len(message) - 1] = CRC[1]#CRCHi | |
| 108 | ||
| 109 | if ser.isOpen: | |
| 110 | ser.write("".join(chr(h) for h in message))
| |
| 111 | reading = ser.read(8) | |
| 112 | response = [0 for i in range(len(reading))] | |
| 113 | for i in range(0, len(reading)): | |
| 114 | response[i] = ord(reading[i]) | |
| 115 | ||
| 116 | if len(response)==8: | |
| 117 | CRCok = CRCvalid(response) | |
| 118 | if CRCok & (response[0]==slave) & (response[1]==Function):return True | |
| 119 | return False | |
| 120 | ||
| 121 | #Modbus Function Code 03 = Read Holding Registers | |
| 122 | def Func03Modbus(slave,start,NumOfPoints): | |
| 123 | #Function 3 request is always 8 bytes | |
| 124 | message = [0 for i in range(8)] | |
| 125 | Slave_Address = slave | |
| 126 | Function = 3 | |
| 127 | Starting_Address = start | |
| 128 | Number_of_Points = NumOfPoints | |
| 129 | ||
| 130 | #index0 = Slave Address | |
| 131 | message[0] = Slave_Address | |
| 132 | #index1 = Function | |
| 133 | message[1] = Function | |
| 134 | #index2 = Starting Address Hi | |
| 135 | message[2] = ((Starting_Address >> 8)& 0xFF) | |
| 136 | #index3 = Starting Address Lo | |
| 137 | message[3] = (Starting_Address& 0xFF) | |
| 138 | #index4 = Number of Points Hi | |
| 139 | message[4] = ((Number_of_Points >> 8)& 0xFF) | |
| 140 | #index5 = Number of Points Lo | |
| 141 | message[5] = (Number_of_Points& 0xFF) | |
| 142 | ||
| 143 | #CRC (Cyclical Redundancy Check) Calculation | |
| 144 | CRC = CRCcal(message) | |
| 145 | ||
| 146 | #index6= CRC Lo | |
| 147 | message[len(message) - 2] = CRC[0]#CRCLo | |
| 148 | #index7 = CRC Hi | |
| 149 | message[len(message) - 1] = CRC[1]#CRCHi | |
| 150 | ||
| 151 | if ser.isOpen: | |
| 152 | ser.write("".join(chr(h) for h in message))
| |
| 153 | responseFunc3total = 5 + 2 * Number_of_Points | |
| 154 | reading = ser.read(responseFunc3total) | |
| 155 | response = [0 for i in range(len(reading))] | |
| 156 | ||
| 157 | for i in range(0, len(reading)): | |
| 158 | response[i] = ord(reading[i]) | |
| 159 | ||
| 160 | if len(response)==responseFunc3total: | |
| 161 | CRCok = CRCvalid(response) | |
| 162 | if CRCok & (response[0]==slave) & (response[1]==Function): | |
| 163 | #Byte Count in index 3 = responseFunc3[2] | |
| 164 | #Number of Registers = byte count / 2 = responseFunc3[2] / 2 | |
| 165 | registers = ((response[2] / 2)& 0xFF) | |
| 166 | values = [0 for i in range(registers)] | |
| 167 | for i in range(0, len(values)): | |
| 168 | #Data Hi and Registers1 from Index3 | |
| 169 | values[i] = response[2 * i + 3] | |
| 170 | #Move to Hi | |
| 171 | values[i] <<= 8 | |
| 172 | #Data Lo and Registers1 from Index4 | |
| 173 | values[i] += response[2 * i + 4] | |
| 174 | negatif = values[i]>>15 | |
| 175 | if negatif==1:values[i]=values[i]*-1 | |
| 176 | return values | |
| 177 | return () | |
| 178 | ||
| 179 | ||
| 180 | #Main Program | |
| 181 | #Serial Port 9600,8,E,1 | |
| 182 | #Serial Open | |
| 183 | try: | |
| 184 | ser = serial.Serial( | |
| 185 | port = '/dev/serial0', | |
| 186 | baudrate = 9600, | |
| 187 | bytesize = serial.EIGHTBITS, | |
| 188 | parity = serial.PARITY_EVEN, | |
| 189 | stopbits = serial.STOPBITS_ONE, | |
| 190 | timeout = 0.2 | |
| 191 | ) | |
| 192 | except Exception, e: | |
| 193 | raise ValueError(e) | |
| 194 | ||
| 195 | ||
| 196 | print "START" | |
| 197 | #Serial Open Check | |
| 198 | if not ser.isOpen:ser.open() | |
| 199 | ||
| 200 | #Read of Registers | |
| 201 | Func03ArrayValue = Func03Modbus(100,1,1);#slave,start,number of registers | |
| 202 | if len(Func03ArrayValue)>0: | |
| 203 | for i in range(0, len(Func03ArrayValue)): | |
| 204 | print "Read of Registers" + str(i) + " = " + str(Func03ArrayValue[i]) | |
| 205 | ||
| 206 | #Fill Random Value for Write | |
| 207 | #totalvalue=2 | |
| 208 | #val = [0 for i in range(totalvalue)] | |
| 209 | #for i in range(0, len(val)): | |
| 210 | # val[i] = random.randrange(-32767,32767) #Random Valiue from -32767 to max 32767 | |
| 211 | print "Alive: " + str(random.randrange(-32767,32767)) | |
| 212 | ||
| 213 | #Write of Registers | |
| 214 | - | WriteValid = Func6Modbus(100,1,4) #slave,start,value |
| 214 | + | WriteValid = Func6Modbus(100,1,2) #slave,start,value |
| 215 | print "#################################" | |
| 216 |