Apparaat – RS232 – Seriële communicatie
Het volgende voorbeeld laat zien hoe je HEX strings naar een serieel RS232 apparaat stuurt en dan het “antwoord” uit kan lezen in HEX strings. C++ / Arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <SoftwareSerial.h> byte command[2] = {0xAA, 0x41}; byte response[5]; SoftwareSerial rs232device(10, 11, SERIAL_8N1); // TX, RX. void setup() { Serial.begin(9600); rs232device.begin(9600); } void loop() { rs232device.write(command, sizeof(command)); rs232device.readBytes(response, 5); Serial.print(response[0], HEX);Serial.print(" "); Serial.print(response[1], HEX);Serial.print(" "); Serial.print(response[2], HEX);Serial.print(" "); Serial.print(response[3], HEX);Serial.print(" "); Serial.println(response[4], HEX); delay(500); } |
Python / Raspberry Pi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
#!/usr/bin/env python import serial import time #------------------------------------------------------------------------------- def ByteToHex( byteStr ): """ Convert a byte string to it's hex string representation e.g. for output. """ # Uses list comprehension which is a fractionally faster implementation than # the alternative, more readable, implementation below # # hex = [] # for aChar in byteStr: # hex.append( "%02X " % ord( aChar ) ) # # return ''.join( hex ).strip() return ''.join( [ "%02X " % ord( x ) for x in byteStr ] ).strip() #------------------------------------------------------------------------------- def HexToByte( hexStr ): """ Convert a string hex byte values into a byte string. The Hex Byte values may or may not be space separated. """ # The list comprehension implementation is fractionally slower in this case # # hexStr = ''.join( hexStr.split(" ") ) # return ''.join( ["%c" % chr( int ( hexStr[i:i+2],16 ) ) \ # for i in range(0, len( hexStr ), 2) ] ) bytes = [] hexStr = ''.join( hexStr.split(" ") ) for i in range(0, len(hexStr), 2): bytes.append( chr( int (hexStr[i:i+2], 16 ) ) ) return ''.join( bytes ) #------------------------------------------------------------------------------- # USB D- = RXD TTL STICK # USB D+ = TXD TTL STICK ### POORT INSTELLINGEN ser = serial.Serial() ser.port = "/dev/ttyUSB0" ser.baudrate = 9600 ser.parity=serial.PARITY_NONE ser.stopbits=serial.STOPBITS_ONE ser.bytesize=serial.EIGHTBITS ser.timeout=1 try: ser.open() print("Connected to: " + ser.portstr) except Exception, e: print "error open serial port: " + str(e) exit() while 1: ### METHODE #1 ser.write([0xAA, 0x41]) ### METHODE #2 #ser.write(serial.to_bytes([0xAA,0x41])) ### METHODE #3 #ser.write("\xAA\x41") ### METHODE #4 #ser.write(chr(0xAA)) #ser.write(chr(0x41)) ### METHODE #5 #msg = bytearray([0xAA, 0x41]) #ser.write(msg) #data = ser.read(8) ### AANTAL BYTES LEZEN data = ser.readline() ## HELE LIJN LEZEN #data = ser.readall() ### ALLES LEZEN print "SIZE: ", len(data) print "RAW: " + data print "HEX1: " + ByteToHex(data) print "HEX2: " + data.encode('hex') time.sleep(2) |
1 2 3 4 |
SIZE: 7 RAW: WAWA HEX1: 57 41 57 41 00 00 00 HEX2: 57415741000000 |