RS485 Master
From Nutwiki
Test Environments
| Hardware Comments |
Nut/OS 4.6.4 |
Nut/OS 4.7.4 |
Nut/OS 4.8.0 | |
| Ethernut 2.1 B | Jumper Settings | OK Binaries |
OK Binaries |
OK Binaries |
| Compiler: ARM-GCC 4.2.2 ; AVR-GCC 4.3.0 | ||||
Description
File:Duplexconf.png
Nut/OS Configurator
This is an example on how to communicate between 2 (or more) Ethernut 2 boards (Slave(s) and Master) by using the RS485 interface. This page holds the code for the master Board.
Note that you have to configure the UART1 Driver settings, as shown on the right.
Do not forget to set the correct jumpers and connect the cables. [1]
Source Code
<source lang="c">
- include <dev/board.h>
- include <string.h>
- include <stdio.h>
- include <io.h>
int main(void) {
uint32_t baud_dbg = 115200; uint32_t baud_485 = 1200; uint32_t flow_485 = USART_MF_HALFDUPLEX; uint32_t to_485 = 1000; uint32_t cnt = 0; FILE *rs485; char buffer[16];
NutRegisterDevice(&DEV_DEBUG, 0, 0);
freopen(DEV_DEBUG_NAME, "w", stdout);
_ioctl(_fileno(stdout), UART_SETSPEED, &baud_dbg);
puts("\nRS485 Master Sample");
if (NutRegisterDevice(&DEV_UART1, 0, 0)) {
puts("Failed to register " DEV_UART1_NAME);
}
if ((rs485 = fopen(DEV_UART1_NAME, "r+")) == NULL) {
puts("Failed to open " DEV_UART1_NAME);
}
if (_ioctl(_fileno(rs485), UART_SETFLOWCONTROL, &flow_485)) {
puts("Failed to set flow control");
}
if (_ioctl(_fileno(rs485), UART_SETSPEED, &baud_485)) {
puts("Failed to set baudrate");
}
if (_ioctl(_fileno(rs485), UART_SETREADTIMEOUT, &to_485)) {
puts("Failed to set read timeout");
}
sbi(PORTD, 2);
for (;;) {
printf("Tx(9):%08lX\n", cnt);
fprintf(rs485, "%08lX\n", cnt);
fflush(rs485);
for (;;) {
if (fgets(buffer, sizeof(buffer), rs485)) {
printf("Rx(%d):%s", strlen(buffer), buffer);
cnt++;
}
else {
if (ferror(rs485)) {
puts("Receive error!");
}
break;
}
}
}
return 0;
} </source>
Output
RS485 Master Sample Tx(9):00000000 Rx(9):00000000 Tx(9):00000001 Rx(9):00000001 Tx(9):00000002 Rx(9):00000002 Tx(9):00000003 Rx(9):00000003 Tx(9):00000004 Rx(9):00000004 ...