F
ollowing is LoopBack Program to Write and Read from UART
while (flag) {
mraa_uart_write(uart, buffer, sizeof(buffer));
sleep(5);
mraa_uart_read(uart,buffer1,sizeof(buffer1));
printf("Data Read %s\n",buffer1);
sleep(5);
buffer1[0]='\0';
}
I tried with /dev/ttyS0 /ttyS1 /ttyS2 /ttyS3 and /dev/ttyS4. I short Port-4 pins (Pin no.1 and Pin no.2) pins
I genuinely believed you would probably have something useful to say. All I hear is a bunch of whining about something that you can fix if you were not too busy looking for attention. After all, I know it was my choice to read.. i99pro
I have added and shared your site to my social media accounts to send people back to your site because I am sure they will find it extremely helpful too. Face mask
Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information.. Outdoor sauna
Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information.. Window Replacement Companies
I appreciate several from the Information which has been composed, and especially the remarks posted I will visit once more. sauna
/* standard headers */ #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> /* mraa header */ #include "mraa/uart.h" #ifndef FALSE #define FALSE 0 #define TRUE (!FALSE) #endif /* UART port name */ const char* dev_path = "/dev/ttyS4"; volatile sig_atomic_t flag = 1; void sig_handler(int signum) { if (signum == SIGINT) { fprintf(stdout, "Exiting...\n"); flag = 0; } } int main(int argc, char** argv) { mraa_result_t status = MRAA_SUCCESS; mraa_uart_context uart; char buffer[] = "Hello Mraa!"; char buffer1[20]; int baudrate = 9600, stopbits = 1, databits = 8; mraa_uart_parity_t parity = MRAA_UART_PARITY_NONE; unsigned int ctsrts = FALSE, xonxoff = FALSE; const char* name = NULL; /* install signal handler */ signal(SIGINT, sig_handler); /* initialize mraa for the platform (not needed most of the time) */ mraa_init(); //! [Interesting] /* initialize uart */ uart = mraa_uart_init_raw(dev_path); if (uart == NULL) { fprintf(stderr, "Failed to initialize UART\n"); return EXIT_FAILURE; } /* set serial port parameters */ status = mraa_uart_settings(-1, &dev_path, &name, &baudrate, &databits, &stopbits, &parity, &ctsrts, &xonxoff); if (status != MRAA_SUCCESS) { goto err_exit; } while (flag) { /* send data through uart */ mraa_uart_write(uart, buffer, sizeof(buffer)); sleep(5); mraa_uart_read(uart,buffer1,sizeof(buffer1)); printf("Data Read %s\n",buffer1); sleep(5); buffer1[0]='\0'; } /* stop uart */ mraa_uart_stop(uart); //! [Interesting] /* deinitialize mraa for the platform (not needed most of the time) */ mraa_deinit(); return EXIT_SUCCESS; err_exit: mraa_result_print(status); /* stop uart */ mraa_uart_stop(uart); /* deinitialize mraa for the platform (not needed most of the times) */ mraa_deinit(); return EXIT_FAILURE; }
Try out the code above using the appropriate toolchain.
Let me know if this code worked for you.
I request you to do a loopback test for the designated UART port using the process below and then proceed for the program in C or Python:
RuggedBoard has 5x UART on different ports. To test UART1, locate P4 port on your RB (refer hardware manual) and
Step1:
Locate the port P4 on the RuggedBoard. Refer to the Hardware manual.
Step 2:
Short PIN 1 (RS232_TX_1) and PIN 2 (RS232_RX_1) using a patch cord provided in the Box to echo the transmitted data back to RuggedBoard.
Step 3:
Use the following command to check the loop-back of the transmitted data. Microcom is an application used to access serial ports.
microcom /dev/ttyS4
Note: Microcom is a serial terminal and it is installed in RuggedBoard by default.
Step 4:
Type anything on the keyboard. If the system is working fine the characters you are typing should be displayed in the terminal.
Step 5:
Press Ctrl + X to come out of the Microcom terminal and back to the Minicom.
Note: After entering the above command you will notice that whatever you type on your keyboard are getting displayed on the terminal and when you remove short of the PIN 1 and PIN 2 keyboard entries are not displayed anymore and resume the loop-back once you short the PINs again.