Trying to hook up a GPS for a quick and dirty autopilot. I have tried a number of examples of writing and read serial port in C. I finally settled on one, but at first no data out. I tried a number of tty devices until I found that ttyAMA0 would output serial data. But I cannot read serial data - gives me an error due to the length of the data read is less than 0. I know the baud, parity, etc is correct. I am using a RPi 5 writing in C without using WiringPi on any other 3rd part access to the serial port.
Hopefully someone smarter than me can point out the error of my way - well at least this error in my ways.
compiles as gcc -o RS232 main.c Setup_UART.c TransByte.c RecvBytes.c CloseUART.c BSP.c
Hopefully someone smarter than me can point out the error of my way - well at least this error in my ways.
compiles as gcc -o RS232 main.c Setup_UART.c TransByte.c RecvBytes.c CloseUART.c BSP.c
Code:
//File main.c#include <stdio.h>#include <unistd.h>//Used for UART#include <fcntl.h>//Used for UART#include <termios.h>//Used for UART#include "BSP.h" int main(int argc, char ** argv) { Setup_UART(); TransByte(); RecvBytes(); CloseUART(); printf("main\n");} //File Setup_UART.c#include <stdio.h>#include <unistd.h>//Used for UART#include <fcntl.h>//Used for UART#include <termios.h>//Used for UART#include "BSP.h"void Setup_UART(void){//At bootup, pins 8 and 10 are already set to UART0_TXD, UART0_RXD (ie the alt0 function) respectivelyuart0_filestream = open("/dev/ttyAMA0", O_RDONLY | O_NOCTTY | O_NDELAY);//Open in non blocking read/write modeif (uart0_filestream == -1){//ERROR - CAN'T OPEN SERIAL PORT}struct termios options;tcgetattr(uart0_filestream, &options);options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;//<Set baud rateoptions.c_iflag = IGNPAR;options.c_oflag = 0;options.c_lflag = 0;tcflush(uart0_filestream, TCIFLUSH);tcsetattr(uart0_filestream, TCSANOW, &options);}//File RecvBytes.c #include <stdio.h>#include <unistd.h>//Used for UART#include <fcntl.h>//Used for UART#include <termios.h>//Used for UART#include "BSP.h"void RecvBytes(void){if (uart0_filestream != -1){unsigned char rx_buffer[256];int rx_length = read(uart0_filestream, (void*)rx_buffer, 255);/if (rx_length < 0){printf("no data error\n");}else if (rx_length == 0){//No data waiting}else{//Bytes receivedrx_buffer[rx_length] = '\0';}}}//File TransByte.c#include <stdio.h>#include <unistd.h>//Used for UART#include <fcntl.h>//Used for UART#include <termios.h>//Used for UART#include "BSP.h"void TransByte(void){unsigned char tx_buffer[20];unsigned char *p_tx_buffer;p_tx_buffer = &tx_buffer[0];*p_tx_buffer++ = 'H';*p_tx_buffer++ = 'e';*p_tx_buffer++ = 'l';*p_tx_buffer++ = 'l';*p_tx_buffer++ = 'o';if (uart0_filestream != -1){int count = 0;//write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));/}}//File BSP.hextern int fd;extern unsigned char tx_buffer[];extern unsigned char *p_tx_buffer;extern int uart0_filestream;void TransByte(void);void TransString(void);//(char tx_string);void RecvBytes(void);void CloseUART(void);void Setup_UART(void);int read_port(void);//File BSP.c#include "BSP.h"int fd;int uart0_filestream;// = -1;
Statistics: Posted by DFansler — Wed Nov 06, 2024 4:36 am