Skip to content

Commit

Permalink
feat(plc4py): Change data types that the crc checks in Modbus use to …
Browse files Browse the repository at this point in the history
…native types
  • Loading branch information
hutcheb committed May 28, 2023
1 parent 70f8f3b commit 7529d84
Show file tree
Hide file tree
Showing 63 changed files with 909 additions and 1,135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,65 +141,50 @@ public String getLanguageTypeNameForTypeReference(TypeReference typeReference, S
SimpleTypeReference simpleTypeReference = typeReference.asSimpleTypeReference().orElseThrow();
switch (simpleTypeReference.getBaseType()) {
case BIT:
emitRequiredImport("from ctypes import c_bool");
return "c_bool";
return "bool";
case BYTE:
emitRequiredImport("from ctypes import c_byte");
return "c_byte";
return "int";
case UINT:
IntegerTypeReference unsignedIntegerTypeReference = simpleTypeReference.asIntegerTypeReference().orElseThrow();
if (unsignedIntegerTypeReference.getSizeInBits() <= 8) {
emitRequiredImport("from ctypes import c_uint8");
return "c_uint8";
return "int";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 16) {
emitRequiredImport("from ctypes import c_uint16");
return "c_uint16";
return "int";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 32) {
emitRequiredImport("from ctypes import c_uint32");
return "c_uint32";
return "int";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 64) {
emitRequiredImport("from ctypes import c_uint64");
return "c_uint64";
return "int";
}
emitRequiredImport("from ctypes import c_longlong");
return "c_longlong";
return "int";
case INT:
IntegerTypeReference integerTypeReference = simpleTypeReference.asIntegerTypeReference().orElseThrow();
if (integerTypeReference.getSizeInBits() <= 8) {
emitRequiredImport("from ctypes import c_int8");
return "c_int8";
return "int";
}
if (integerTypeReference.getSizeInBits() <= 16) {
emitRequiredImport("from ctypes import c_int16");
return "c_int16";
return "int";
}
if (integerTypeReference.getSizeInBits() <= 32) {
emitRequiredImport("from ctypes import c_int32");
return "c_int32";
return "int";
}
if (integerTypeReference.getSizeInBits() <= 64) {
emitRequiredImport("from ctypes import c_int64");
return "c_int64";
return "int";
}
emitRequiredImport("from ctypes import c_longlong");
return "c_longlong";
return "int";
case FLOAT:
case UFLOAT:
FloatTypeReference floatTypeReference = simpleTypeReference.asFloatTypeReference().orElseThrow();
int sizeInBits = floatTypeReference.getSizeInBits();
if (sizeInBits <= 32) {
emitRequiredImport("from ctypes import c_float");
return "c_float";
return "float";
}
if (sizeInBits <= 64) {
emitRequiredImport("from ctypes import c_double");
return "c_double";
return "float";
}
emitRequiredImport("from ctypes import c_longdouble");
return "c_longdouble";
return "float";
case STRING:
case VSTRING:
return "str";
Expand Down
19 changes: 9 additions & 10 deletions sandbox/plc4py/plc4py/protocols/modbus/StaticHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from ctypes import c_uint16, c_byte, c_uint8

from plc4py.protocols.modbus.readwrite.ModbusPDU import ModbusPDU
from plc4py.spi.generation.WriteBuffer import WriteBufferByteBased
Expand Down Expand Up @@ -63,26 +62,26 @@
)


def rtu_crc_check(address: c_uint16, pdu: ModbusPDU) -> int:
def rtu_crc_check(address: int, pdu: ModbusPDU) -> int:
# Using the algorithm from PI_MBUS_300.pdf page 121
write_buffer: WriteBufferByteBased = WriteBufferByteBased(
pdu.length_in_bytes() + 2, byte_order=ByteOrder.LITTLE_ENDIAN
)
write_buffer.write_unsigned_short(address, 8)
pdu.serialize(write_buffer)
m_view: memoryview = write_buffer.get_bytes()
uch_crc_hi: c_byte = c_byte(0xFF)
uch_crc_lo: c_byte = c_byte(0xFF)
u_index: int = 0
uch_crc_hi: int = 0xFF
uch_crc_lo: int = 0xFF
u_index: int
for b in m_view:
u_index = (int(uch_crc_hi.value) ^ b) & 0xFF
uch_crc_hi = c_byte(int(uch_crc_lo.value) ^ auch_crc_hi[u_index])
uch_crc_lo = c_byte(auch_crc_lo[u_index])
return ((int(uch_crc_hi.value) << 8) & 0xFFFF) | (int(uch_crc_lo.value) & 0x00FF)
u_index = (uch_crc_hi ^ b) & 0xFF
uch_crc_hi = uch_crc_lo ^ auch_crc_hi[u_index]
uch_crc_lo = auch_crc_lo[u_index]
return ((uch_crc_hi << 8) & 0xFFFF) | (uch_crc_lo & 0x00FF)


# 8 Bit checksum, (byte) transported as 2 characters
def ascii_lrc_check(address: c_uint8, pdu: ModbusPDU) -> int:
def ascii_lrc_check(address: int, pdu: ModbusPDU) -> int:
write_buffer: WriteBufferByteBased = WriteBufferByteBased(
pdu.length_in_bytes() + 2, byte_order=ByteOrder.LITTLE_ENDIAN
)
Expand Down
Loading

0 comments on commit 7529d84

Please sign in to comment.