commit d1582cabd01cc234a89fad2e7dc3a417ee17de63 Author: Piotr Dobrowolski Date: Sun Feb 24 08:10:26 2019 +0100 Initial commit diff --git a/modbus_test.py b/modbus_test.py new file mode 100644 index 0000000..a3a4d7f --- /dev/null +++ b/modbus_test.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +from pymodbus.client.sync import ModbusSerialClient +import time + +import logging +logging.basicConfig() +log = logging.getLogger() +log.setLevel(logging.DEBUG) + +client = ModbusSerialClient(method='rtu', port='/dev/serial/by-id/usb-FTDI_TTL232R_FTF48YOZ-if00-port0', timeout=0.02, baudrate=115200) +client.connect() +time.sleep(1) +log.debug("Read input registers") +start = time.time() +found = 0 +for n in range(256): + rr = client.read_input_registers(1, 1, unit=n) + found += not rr.isError() + print(rr) + print(time.time() - start, found) +client.close() diff --git a/notes.md b/notes.md new file mode 100644 index 0000000..d03fbcc --- /dev/null +++ b/notes.md @@ -0,0 +1,12 @@ +Fails: +* r1 was supposed to be a pullup +* programming/icsp header is broken + + + +Prog +==== +* avrdude -c avrisp -b 19200 -p m328p -P /dev/ttyUSB5 -vvv + +* avrdude -pm328pb -cstk500v1 -P/dev/ttyUSB5 -b19200 -e -Uefuse:w:0xf5:m -Uhfuse:w:0xDE:m -Ulfuse:w:0xFF:m +* avrdude -pm328pb -cstk500v1 -P/dev/ttyUSB5 -b19200 -Uflash:w:/home/informatic/.arduino15/packages/arduino/hardware/avr/1.6.8/bootloaders/optiboot/optiboot_atmega328.hex:i -Ulock:w:0x0F:m diff --git a/software/.gitignore b/software/.gitignore new file mode 100644 index 0000000..6c69f4c --- /dev/null +++ b/software/.gitignore @@ -0,0 +1,2 @@ +.pioenvs +.piolibdeps diff --git a/software/lib/readme.txt b/software/lib/readme.txt new file mode 100644 index 0000000..dbadc3d --- /dev/null +++ b/software/lib/readme.txt @@ -0,0 +1,36 @@ + +This directory is intended for the project specific (private) libraries. +PlatformIO will compile them to static libraries and link to executable file. + +The source code of each library should be placed in separate directory, like +"lib/private_lib/[here are source files]". + +For example, see how can be organized `Foo` and `Bar` libraries: + +|--lib +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| |--Foo +| | |- Foo.c +| | |- Foo.h +| |- readme.txt --> THIS FILE +|- platformio.ini +|--src + |- main.c + +Then in `src/main.c` you should use: + +#include +#include + +// rest H/C/CPP code + +PlatformIO will find your libraries automatically, configure preprocessor's +include paths and build them. + +More information about PlatformIO Library Dependency Finder +- http://docs.platformio.org/page/librarymanager/ldf.html diff --git a/software/platformio.ini b/software/platformio.ini new file mode 100644 index 0000000..4479d8e --- /dev/null +++ b/software/platformio.ini @@ -0,0 +1,21 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/page/projectconf.html + +[env:uno] +platform = atmelavr +board = uno +framework = arduino +monitor_port = /dev/serial/by-id/usb-FTDI_TTL232R_FTF48YOZ-if00-port0 +upload_port = /dev/serial/by-id/usb-FTDI_TTL232R_FTF48YOZ-if00-port0 +targets = upload, monitor +monitor_baud = 115200 + +lib_deps = + https://github.com/yaacov/ArduinoModbusSlave/archive/03e0182795fee5e7025c14c5817cac835f760eae.zip diff --git a/software/src/main.ino b/software/src/main.ino new file mode 100644 index 0000000..6ff16bd --- /dev/null +++ b/software/src/main.ino @@ -0,0 +1,35 @@ +#include +#include + +// explicitly set stream to use the Serial serialport +Modbus modbus(Serial, 1, 3); // stream = Serial, slave id = 1, rs485 control-pin = 8 + +void setup() { + pinMode(13, OUTPUT); + + Serial.begin(115200); + Serial.print("Initializing! "); + for (int i = 0; i < 16; i++) { + Serial.print(EEPROM.read(1)); + Serial.print(" "); + } + Serial.println(); + + modbus.cbVector[CB_READ_INPUT_REGISTERS] = ReadAnalogIn; + modbus.begin(115200); +} + +void loop() { + modbus.poll(); + + digitalWrite(13, (millis() / 200) % 2); +} + +// Handel Read Input Registers (FC=04) +uint8_t ReadAnalogIn(uint8_t fc, uint16_t address, uint16_t length) { + // write registers into the answer buffer + for (int i = 0; i < length; i++) { + modbus.writeRegisterToBuffer(i, analogRead(address + i)); + } + return STATUS_OK; +}