I'm new here.
I'm trying receive data from UART which is from Xbee.
The sample code below is allow me to check the API frame.
But what I need is make these to be a useful array.
How should i make this into a complete array?
The result shows what i need but they are independent.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <termios.h> //Used for UART
int main(int argc, char const *argv[])
{
//-------------------------
//----- SETUP USART 0 -----
//-------------------------
//At bootup, pins 8 and 10 are already set to UART0_TXD, UART0_RXD (ie the alt0 function) respectively
int uart0_filestream = -1;
//OPEN THE UART
//The flags (defined in fcntl.h):
// Access modes (use 1 of these):
// O_RDONLY - Open for reading only.
// O_RDWR - Open for reading and writing.
// O_WRONLY - Open for writing only.
//
// O_NDELAY / O_NONBLOCK (same function) - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status
// if there is no input immediately available (instead of blocking). Likewise, write requests can also return
// immediately with a failure status if the output can't be written immediately.
//
// O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.
uart0_filestream = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); //Open in non blocking read/write mode
if (uart0_filestream == -1)
{
//ERROR - CAN'T OPEN SERIAL PORT
printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
}
struct termios options;
tcgetattr(uart0_filestream, &options);
options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; //<Set baud rate
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(uart0_filestream, TCIFLUSH);
tcsetattr(uart0_filestream, TCSANOW, &options);
//----- TX BYTES -----
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 = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));
//Filestream, bytes to write, number of bytes to write
if (count < 0)
{
printf("UART TX error\n");
}
}
char r[100];
while(1){
//----- CHECK FOR ANY RX BYTES -----
if (uart0_filestream != -1)
{
//printf("OK");
// Read up to 255 characters from the port if they are there
unsigned char rx_buffer[256];
int rx_length = read(uart0_filestream, (void*)rx_buffer, 255); //Filestream, buffer to store in, number of bytes to read (max)
if (rx_length < 0)
{
//An error occured (will occur if there are no bytes)
}
else if (rx_length == 0)
{
//No data waiting
}
else
{
//Bytes received
rx_buffer[rx_length] = '\0';
printf("%i bytes read : %s\n", rx_length, rx_buffer);
//printf("%d\n",rx_buffer[0]);
}
}
}
return 0;
}
Related
I am trying to read some data from a text file and writing it to the ttyUSB* socket id.
I am using Hi3520d Dvr. I have it's RS485 port connected to a "RS485 to RS232 converter". This converter is connected to the PC through a USB port.
The text file is getting read properly to the buffer, but while writing last few lines of the text is not transmitting. This is happening with file with size more than 4.5kb exactly and without usleep() function.
I am using minicom on linux terminal to display both read and written text.
Thanks in advance for looking into this.
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#define Bdrate B9600
int Intserial(char *dev, int Baudrate)
{
//printf("Insterial func\n");
int sid;
int iDebug = -1;
struct termios serial_struct;
sid = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
if (sid > 0)
{
memset(&serial_struct, 0, sizeof(serial_struct)); /* clear the new struct */
serial_struct.c_cflag = Baudrate | CS8 | CLOCAL | CREAD;
serial_struct.c_iflag = IGNPAR;
serial_struct.c_oflag = 0;
serial_struct.c_lflag = 0;
serial_struct.c_cc[VMIN] = 0; /* block untill n bytes are received */
serial_struct.c_cc[VTIME] = 0; /* block untill a timer expires (n * 100 mSec.) */
iDebug = tcsetattr(sid, TCSANOW, &serial_struct);
if (iDebug < 0)
{
printf("Err 0\n"); //Unable to set serial port parameters
return (0);
}
}
else
{
printf("Err 1\n"); //Serial port not open
return (0);
}
//printf("sid is %d \n",sid);
return (sid);
}
int main()
{
int sid1 = -1, size = 0, i = 0, x, w;
size_t ln;
FILE *fd;
char buf[2233];
fd = fopen("h.txt", "r");
if (fd)
{
sid1 = Intserial("/dev/ttyAMA1", Bdrate); //RS485 port of Hi3520d
if (sid1 > -1)
{
system("himm 0x200F004C 0"); // commands transmitting and recieving
system("himm 0x201A0400 1");
system("himm 0x201a0004 1");
while (!feof(fd))
{
memset(buf, 0, sizeof(buf));
fread(buf, sizeof(buf), 1, fd);
printf("%s", buf);
write(sid1, buf, sizeof(buf));
usleep(5);
}
getchar();
}
else
printf("com port cant open\r\n ");
fclose(fd);
close(sid1);
}
else
printf("File cant open\r\n");
printf("task completed............\r\n");
}
You have to observe return value of fread for number of bytes read by fread function. the actual read size may not equal to bytes requested, also you have to pass number of bytes read by fread (as valid bytes in buffer) to write function as number of bytes to write.
The code should be something like this
memset(buf,0,sizeof(buf));
size_t bytesRead = fread(buf,sizeof(buf),1,fd);
if(bytesRead > 0)
write(sid1,buf, bytesRead);
Also as LPs said, fread doesn't end buffer with termination character, so passing buffer filled by fread to printf("%s") will be undefined behavior
There are numerous issues with your code, but the salient cause of "the text is not transmitting" is probably the failure to check the return value of
write(sid1, buf, sizeof(buf));
Because the serial terminal was opened in non-blocking mode, each write() will return immediately, before the data has been actually transmitted.
Since the serial terminal is configured for a rather slow 9600 baud, the data could be queued up in the line discipline buffer and other intermediate buffers.
The line discipline buffer is typically 4096 bytes long.
Assuming that the fread() operations are always successful (which you seem to have verified), then the second iteration of the write() of 2233 bytes could potentially saturate the line discipline buffer, and return with a short write return value (which would be ignored).
The third iteration of the write(), if it's quick enough, could then be outright rejected with a return value of -1 and an errno of EAGAIN to indicate that the write would block.
This error condition would be silently ignored, and this 2233 bytes of data will never be transmitted.
This seems to correlate perfectly with your observation of "last few lines of the text is not transmitting ... with file with size more than 4.5kb exactly and without usleep() function."
ADDENDUM
Revised code for blocking mode, proper terminal setup, and checking of return values is shown below.
A corrected version of #e.jahandar's suggestion and comments from #LPs are also incorporated.
...
sid = open(dev, O_RDWR | O_NOCTTY);
if (sid < 0) {
printf("Err 1\n"); //Serial port not open
return (-1);
}
if (tcgetattr(sid, &serial_struct) < 0) {
printf("Err 2\n");
return (-2);
}
cfsetospeed(&serial_struct, (speed_t)Baudrate);
cfsetispeed(&serial_struct, (speed_t)Baudrate);
cfmakeraw(&serial_struct);
serial_struct.c_cc[VMIN] = 1;
serial_struct.c_cc[VTIME] = 10;
serial_struct.c_cflag &= ~CSTOPB;
serial_struct.c_cflag &= ~CRTSCTS; /* no HW flow control? */
serial_struct.c_cflag |= CLOCAL | CREAD;
if (tcsetattr(sid, TCSANOW, &serial_struct) < 0) {
printf("Err 3\n"); //Unable to set serial port parameters
return (-3);
}
...
#define BUFSIZE 2233
char buf[BUFSIZE + 1];
...
size_t frv;
ssize_t wrv;
...
do {
frv = fread(buf, 1, BUFSIZE, fd);
buf[frv] = 0; /* terminate string for printf */
if (frv > 0) {
wrv = write(sid1, buf, frv);
if (wrv < frv) {
/* handle error or short write */
}
} else
break;
} while (1);
...
I am trying to write a function that reads a chunk of data sent through UART. I am using Raspbian Jessie running on a RaspberryPi model B but I wanted to use this C code (with any necessary revisions) on openwrt. So far, this is what I wrote.
Header:
#ifndef __UART_LIB__
#define __UART_LIB__
#include <stdlib.h> //Errors, etc
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <termios.h> //Used for UART
#include <sys/types.h> //These includes are for timeout
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h> //
#include <sys/ioctl.h>
#define BITS_PER_PACKAGE_ 11
#define WAIT_PROLONGATION_CONSTANT_ 1.1f
//Some values used by default, left for the user to change if needed
unsigned int BAUD_ ;
unsigned int NUM_BITS_ ;
char *UART_PATH_ ;
unsigned int MAX_SIZE_ ;
unsigned int OPEN_FLAG_ ;
time_t TIMEOUT_SEC_ ;
suseconds_t TIMEOUT_USEC_ ;
struct timeval WAIT_CONSTANT_ ;
int open_conf_UART_() ;
int read_UART_(int uart_filestream, char** dest, int max_len) ;
#endif
.c file:
#include "uartlib.h"
unsigned int BAUD_ = B115200 ;
unsigned int NUM_BITS_ = CS8 ;
char *UART_PATH_ = "/dev/ttyAMA0" ;
unsigned int MAX_SIZE_ = 128 ;
unsigned int OPEN_FLAG_ = O_RDWR ;
time_t TIMEOUT_SEC_ = 5 ;
suseconds_t TIMEOUT_USEC_ = 0 ;
int open_conf_UART_()
{
int indicator, old_fl;
int uart_filestream ;
struct termios options ;
// Opening the port in a read/write mode
uart_filestream = open(UART_PATH_, OPEN_FLAG_ | O_NOCTTY );
if (uart_filestream < 0)
{
// Unable to open the serial port, so produce an error and halt
return -1;
}
// Configuring the options for UART
// Retrieve the options and modify them.
indicator = tcgetattr(uart_filestream, &options);
if(indicator < 0)
{
// Unable to get the attributes
close(uart_filestream);
return -1;
}
// I found a question on stackoverlow where the answer said that VTIME and VMIN will be ignored unless I
// switch the FNDELAY flag off
old_fl = fcntl(uart_filestream, F_GETFL);
if(old_fl < 0)
{
return -1;
}
old_fl &= ~FNDELAY;
fcntl(uart_filestream, old_fl);
//Setting the options
options.c_cflag = CRTSCTS | BAUD_ | NUM_BITS_ | CLOCAL | CREAD ;
options.c_iflag = 0;
options.c_oflag = 0;
options.c_lflag = 0;
//I want the uart to wait 1/10 of a second between bytes at most
options.c_cc[VTIME] = 1;
options.c_cc[VMIN] = 0;
// Flushing the file stream (the input and the output area)
indicator = tcflush(uart_filestream, TCIOFLUSH);
if(indicator < 0)
{
// Unable to flush
close(uart_filestream);
return -1;
}
// Setting the options for the file stream.
indicator = tcsetattr(uart_filestream, TCSANOW, &options);
if(indicator < 0)
{
// Unable to set the attributes
close(uart_filestream);
return -1;
}
return uart_filestream;
}
int read_UART_(int uart_filestream, char** dest, int max_len)
{
int indicator;
int buffer_length;
indicator = tcflush(uart_filestream, TCIFLUSH);
if(indicator < 0)
{
// Unable to flush
return -1;
}
//Do the actual reading
buffer_length = read(uart_filestream, (void*)(*dest), max_len);
if(indicator < 0)
{
return -1;
}
else
{
// Returning number of read bytes
return buffer_length;
}
// Both branches of the if statement above have return, so this will not be reached
}
So, when I try to read more than 8 bytes, the message gets truncated to 8 bytes.
As I read, setting VTIME to a certain value allows the time interval between two bytes to be at most that long.
I am not certain what is going on but I suspect that the read() call reads the buffer before the receiving of the data is complete.
My wish is to read a chunk of data of undefined size. I also used select() with a timeout before the read to make sure the program won't block entirely.
I read a lot of forum topics, stackoverflow questions, guides, etc. on this topic and none seem to help me with my solution.
So, can anyone explain what is going on here? Is it possible to do what I want?
Note that I removed some of the code (I also wrote a function that writes to a UART port) so here might be some redundant includes, global variables, etc.
So, I solved my problem. I still can't flush (when the system is freshly booted up and there was a signal before I turned my program on, there is some old content in the buffer, as it seems to me).
I used this assumption: The package will arrive in smaller bursts and they will be separated by TIMEOUT_BYTE_ amount of time. If that expires, I assume that the package is over.
Also, I have a timeout for initial waiting for the data but I reckon that is situational.
Header file:
#ifndef UART_LIB_
#define UART_LIB_
#include <stdlib.h> //Errors, etc
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <termios.h> //Used for UART
#include <sys/types.h> //These includes are for timeout
#include <sys/select.h> //Used for select(), etc
//Some values used by default, left for the user to change if needed
//Used to set up the baud rate
unsigned int BAUD_ ;
//Used to indicate number of bits in one backage
unsigned int NUM_BITS_ ;
//Path to the UART device
char *UART_PATH_ ;
//Flag for opening the device
unsigned int OPEN_FLAG_ ;
//Timeout for answer from the other side
time_t TIMEOUT_SEC_ ;
suseconds_t TIMEOUT_USEC_ ;
//Time interval between two bursts of data inside the package
suseconds_t TIMEOUT_BYTE_ ;
int open_conf_UART_(void) ;
int read_UART_(int uart_filestream, char* dest, int max_len) ;
#endif
Source file:
#include <errno.h>
#include "uartlib.h"
unsigned int BAUD_ = B38400 ;
unsigned int NUM_BITS_ = CS8 ;
char *UART_PATH_ = "/dev/ttyAMA0" ;
unsigned int OPEN_FLAG_ = O_RDWR ;
time_t TIMEOUT_SEC_ = 2 ;
suseconds_t TIMEOUT_USEC_ = 0 ;
// This needs to be finely tuned
suseconds_t TIMEOUT_BYTE_ = 5000;
int open_conf_UART_()
{
// Variable section
int indicator;
int uart_filestream ;
struct termios options ;
// Opening the port in a read/write mode
uart_filestream = open(UART_PATH_, OPEN_FLAG_ | O_NOCTTY | O_NONBLOCK);
if (uart_filestream < 0)
{
// Unable to open the serial port, so produce an error and halt
return -1;
}
// Configuring the options for UART
// Flushing the file stream (the input and the output area)
indicator = tcflush(uart_filestream, TCIOFLUSH);
if(indicator < 0)
{
// Unable to flush
close(uart_filestream);
return -1;
}
// Retrieve the options and modify them.
indicator = tcgetattr(uart_filestream, &options);
if(indicator < 0)
{
// Unable to get the attributes
close(uart_filestream);
return -1;
}
// Setting the options
cfmakeraw(&options);
options.c_cflag |= BAUD_ | NUM_BITS_ | CREAD;
// Setting the options for the file stream.
indicator = tcsetattr(uart_filestream, TCSANOW, &options);
if(indicator < 0)
{
// Unable to set the attributes
close(uart_filestream);
return -1;
}
return uart_filestream;
}
int read_UART_(int uart_filestream, char* dest, int max_len)
{
// Variable section
int indicator;
int buffer_length;
char *tmp_dest;
fd_set set;
struct timeval timeout, init_timeout;
while(1)
{
// Reseting the set and inserting the uart_filestream in it
FD_ZERO(&set);
FD_SET(uart_filestream, &set);
// Setting the time for initial contact
init_timeout.tv_sec = TIMEOUT_SEC_ ;
init_timeout.tv_usec = TIMEOUT_USEC_ ;
// Waiting for the first contact. If this times out, we assume no contact.
indicator = select(uart_filestream + 1, &set, NULL, NULL, &init_timeout);
if(indicator < 0)
{
if(errno == EINTR)
{
// Try again
continue;
}
return -1;
}
else if(indicator == 0)
{ // Timeout has occurred
return -2;
}
else
{
break;
}
}
// This section means that there is something to be read in the file descriptor
buffer_length = 0 ;
tmp_dest = dest ;
// The first select is redundant but it is easier to loop this way.
while(buffer_length < max_len)
{
// select changes the timeval structure so it is reset here
timeout.tv_sec = 0;
timeout.tv_usec = TIMEOUT_BYTE_;
// Reinitialize the sets for reading
FD_ZERO(&set);
FD_SET(uart_filestream, &set);
// Wait for the file descriptor to be available or for timeout
indicator = select(uart_filestream+1, &set, NULL, NULL, &timeout);
if(indicator < 0)
{
if(errno == EINTR)
{
// Try again
continue;
}
// This indicates an error
return -1;
}
else if(indicator == 0)
{
// This indicates a timeout; We assume that the transmission is over once first timeout is reached
return buffer_length;
}
// There's been a select that didn't time out before this read
indicator = read(uart_filestream, (void*)tmp_dest, max_len - buffer_length);
if(indicator < 0)
{
if(errno == EINTR)
{
// If the call was interrupted, try again
continue;
}
// If it was any other condition, the read is corrupt.
return -1;
}
else if(indicator == 0)
{
// If, somehow, EOF was reached
break;
}
// Change the necessary values
buffer_length += indicator ;
tmp_dest += indicator;
}
// Both branches of the if statement above have return, so this will not be reached
// but a warning is generated
return buffer_length;
}
void flush_buffer_UART_(int uart_filestream)
{
char c;
while(read(uart_filestream, &c, 1) > 0);
}
I know it is not the topic here, but if someone knows how to solve the flush issue, please respond.
Also, any constructive criticism is very welcome.
P.S. I also have a write_UART() function but I did not deem necessary to post it as it represented no problem (measured with an oscilloscope and later, tried with echo. Echo wouldn't be able to give me the same message).
EDIT: Flush has been introduced and then merged with the source file. Still haven't figured out is it working or not.
I use this usb gpio device. It uses some command to send/receive data from input/output channel. There is a guide that explains how commands send on numato website. There are some sample code for C on Windows. But I use fedora and my code is below for read/write gpio. I can't read data.
Code sample for Windows
#include "stdafx.h"
#include "windows.h"
#include "string.h"
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hComPort;
char cmdBuffer[32];
char responseBuffer[32];
DWORD numBytesWritten;
DWORD numBytesRead;
/*
Lookup the port name associated to your GPIO device and update the
following line accordingly. The port name should be in the format
"\\.\COM<port Number>". Notice the extra slaches to escape slashes
themselves. Read http://en.wikipedia.org/wiki/Escape_sequences_in_C
for more details.
*/
wchar_t PortName[] = L"\\\\.\\COM14";
/*
Open a handle to the COM port. We need the handle to send commands and
receive results.
*/
hComPort = CreateFile(PortName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (hComPort == INVALID_HANDLE_VALUE)
{
printf("Error: Unable to open the specified port\n");
return 1;
}
/* EXAMPLE 1 - MANIPULATE GPIO0 BY SENDING COMMAND */
/**************************************************************************
Send a command to output a logic high at GPIO 0. The command that is
used to accomplish this acton is "gpio set 0". It is important to send
a Carriage Return character (ASCII value 0x0D) to emulate the ENTER
key. The command will be executed only when the GPIO module detects
Carriage Return character.
**************************************************************************/
/* Write a Carriage Return to make sure that any partial commands or junk
data left in the command buffer is cleared. This step is optional.
*/
cmdBuffer[0] = 0x0D;
if(!WriteFile(hComPort, cmdBuffer, 1, &numBytesWritten, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
/* Copy the command to the command buffer */
strcpy(cmdBuffer, "gpio set 0");
/* Append 0x0D to emulate ENTER key */
cmdBuffer[10] = 0x0D;
/* Write the command to the GPIO module. Total 11 bytes including 0x0D */
printf("Info: Writing command <gpio set 0> to the GPIO module\n");
if(!WriteFile(hComPort, cmdBuffer, 11, &numBytesWritten, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
printf("Info: <gpio set 0> Command sent successfuly\n");
/* EXAMPLE 2 - MANIPULATE GPIO10 BY SENDING COMMAND */
/**************************************************************************
Send a command to output a logic high at GPIO 0. The command that is
used to accomplish this acton is "gpio set 0". It is important to send
a Carriage Return character (ASCII value 0x0D) to emulate the ENTER
key. The command will be executed only when the GPIO module detects
Carriage Return character.
**************************************************************************/
/* Write a Carriage Return to make sure that any partial commands or junk
data left in the command buffer is cleared. This step is optional.
*/
cmdBuffer[0] = 0x0D;
if(!WriteFile(hComPort, cmdBuffer, 1, &numBytesWritten, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
/*
Copy the command to the command buffer. GPIO number 10 and beyond are
referenced in the command by using alphabets starting A. For example
GPIO10 willbe A, GPIO11 will be B and so on. Please note that this is
not intended to be hexadecimal notation so the the alphabets can go
beyond F.
*/
strcpy(cmdBuffer, "gpio set A");
/* Append 0x0D to emulate ENTER key */
cmdBuffer[10] = 0x0D;
/* Write the command to the GPIO module. Total 11 bytes including 0x0D */
printf("Info: Writing command <gpio set A> to the GPIO module\n");
if(!WriteFile(hComPort, cmdBuffer, 11, &numBytesWritten, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
printf("Info: <gpio set A> Command sent successfuly\n");
/* EXAMPLE 3 - READ ADC 1 */
/**************************************************************************
Write "adc read 1" comamnd to the device and read back response. It is
important to note that the device echoes every single character sent to
it and so when you read the response, the data that is read will
include the command itself, a carriage return, the response which you
are interested in, a '>' character and another carriage return. You
will need to extract the response from this bunch of data.
/*************************************************************************/
/* Write a Carriage Return to make sure that any partial commands or junk
data left in the command buffer is cleared. This step is optional.
*/
cmdBuffer[0] = 0x0D;
if(!WriteFile(hComPort, cmdBuffer, 1, &numBytesWritten, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
/* Flush the Serial port's RX buffer. This is a very important step*/
Sleep(10);
PurgeComm(hComPort, PURGE_RXCLEAR|PURGE_RXABORT);
/* Copy the command to the command buffer */
strcpy(cmdBuffer, "adc read 1");
/* Append 0x0D to emulate ENTER key */
cmdBuffer[10] = 0x0D;
/* Write the command to the GPIO module. Total 11 bytes including 0x0D */
printf("Info: Writing command <adc read 1> to the GPIO module\n");
if(!WriteFile(hComPort, cmdBuffer, 11, &numBytesWritten, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
printf("Info: <adc read 1> Command sent successfuly\n");
/*Read back the response*/
if(!ReadFile(hComPort, responseBuffer, 16, &numBytesRead, NULL))
{
CloseHandle(hComPort);
printf("Error: Unable to write to the specified port\n");
return 1;
}
/* Add a null character at the end of the response so we can use the buffer
with string manipulation functions.
*/
responseBuffer[numBytesRead] = '\0';
printf("Info: ADC value read from the device = %.*s", 4, responseBuffer + 12);
/* Close the comm port handle */
CloseHandle(hComPort);
return 0;
}
My code for Ubuntu
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#define MAXBUFFER 32
int main(int argc, char* argv[])
{
struct termios serialSettings;
char *deviceName = "/dev/ttyACM0";
char bufferRecv[MAXBUFFER], bufferSend[MAXBUFFER];
int readInt, sendInt;
int fd = open(deviceName, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1) {
printf("\n %s\n", deviceName);
perror("unable to open port");
}
else {
printf("port is opened!\n");
bufferSend[0] = 0x0D; /* clear buffer */
strcpy(bufferSend, "gpio set 0"); /* */
sendInt = write(fd, bufferSend, strlen(bufferSend));
if(sendInt <= 0){
printf("Unable to write to the port\n");
return -1;
}
printf("<gpio set 0> : Command sent successfuly\n");
strcpy(bufferSend, "gpio read 0"); /* */
sendInt = write(fd, bufferSend, strlen(bufferSend));
if(sendInt <= 0){
printf("Unable to write to the port\n");
return -1;
}
printf("<gpio read 0> : Command sent successfuly\n");
readInt = read(fd, bufferRecv, sizeof(bufferRecv));
if(readInt < 0){
printf("Unable to read to the port\n");
return -1;
}
bufferRecv[strlen(bufferRecv)] = '\0';
printf("read=%c-\n", bufferRecv[0]);
}
close(fd);
return 0;
}
The output
port is opened!
<gpio set 0> : Command sent successfuly
<gpio read 0> : Command sent successfuly
Unable to read to the port
The "successful" write() s are false positives. The data was output, but not properly received by the device.
Your program has not properly configured the serial port using termios after the open() and before any write() or read(). You should configure the port for canonical rather than raw mode. You also need to configure the baud rate, character length, parity and number of stop bits.
Use the Serial Programming Guide for POSIX Operating Systems.
Workable(?) configuration code might look like (for 115200 baud rate, 8N1 and canonical input, i.e. read terminates on NL character):
#include <termios.h>
struct termios serialSettings;
speed_t spd;
int fd;
int rc;
fd = open(deviceName, O_RDWR | O_NOCTTY);
if (fd == -1) {
printf("\n %s\n", deviceName);
perror("unable to open port");
return -1;
}
rc = tcgetattr(fd, &serialSettings);
if (rc < 0) {
perror("unable to get attributes");
return -2;
}
spd = B115200;
cfsetospeed(&serialSettings, spd);
cfsetispeed(&serialSettings, spd);
serialSettings.c_cflag &= ~CSIZE;
serialSettings.c_cflag |= CS8;
serialSettings.c_cflag &= ~PARENB;
serialSettings.c_cflag &= ~CSTOPB;
serialSettings.c_cflag &= ~CRTSCTS; /* no HW flow control? */
serialSettings.c_cflag |= CLOCAL | CREAD;
serialSettings.c_iflag &= ~(PARMRK | ISTRIP | IXON | IXOFF | INLCR);
serialSettings.c_iflag |= ICRNL;
serialSettings.c_oflag &= ~OPOST;
serialSettings.c_lflag &= ~(ECHO | ECHONL | ISIG | IEXTEN);
serialSettings.c_lflag |= ICANON;
rc = tcsetattr(fd, TCSANOW, &serialSettings);
if (rc < 0) {
perror("unable to set attributes");
return -2;
}
/* serial port is now configured and ready */
...
Additional comments on your code:
bufferRecv[strlen(bufferRecv)] = '\0';
This is illogical code. If you could actually determine the string length of what's in bufferRecv, then that text would already be null terminated, and this assignment would not be necessary.
Secondly and much worse, the read() does not terminate the receive data with a null byte, so the strlen() could scan past the buffer end.
The read() does return the number of bytes stored in the buffer, and that value can be used to locate where a null byte should be written.
Workable code would look like (note the reduction in requested read length):
readInt = read(fd, bufferRecv, sizeof(bufferRecv) - 1);
if (readInt < 0){
perror("Unable to read from the port\n");
return -3;
}
bufferRecv[readInt] = '\0';
printf("ADC value read = %s\n", bufferRecv);
The strings that you send to the USB device should be preceded and terminated with a carriage return just like the Win examples:
strcpy(bufferSend, "\rgpio set 0\r");
...
strcpy(bufferSend, "\rgpio read 0\r");
New to C programming here so bear with me.. I wrote a program to write/read data to and from the serial port. Everything appears to be working except when I try to read the number of bytes available at the port that have been received. Here's my code (see the read_port function):
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <sys/ioctl.h> /* Serial Port IO Controls */
int fd; /* File descriptor for the port */
struct termios options_original; /* Original Serial Port Options */
int main()
{
fd = open_port();
flush_port();
write_port();
printf("FIONBIO value %d\n", FIONBIO);
usleep(2);
printf("FIONREAD value %d\n", FIONREAD);
read_port();
close_port();
}
/*
* open_port() - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error
*/
int open_port(void)
{
struct termios options;
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if (fd != -1)
{
printf("Serial Port Open\n");
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options_original);
tcgetattr(fd, &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag |= (CLOCAL | CREAD); /* Enable the receiver and set local mode */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* Raw Input Mode */
tcsetattr(fd, TCSANOW, &options); /* Set the new options for the port */
}
else
{
/* Could not open the port */
perror("open_port: Unable to open /dev/ttyUSB0 - ");
}
return (fd);
}
int close_port(void)
{
tcsetattr(fd, TCSANOW, &options_original);
printf("Serial Port Closed\n");
close(fd);
}
int flush_port(void)
{
usleep(2); // required to make flush work, for some reason
printf("Flushing IO Buffers\n");
tcflush(fd, TCIOFLUSH);
}
int write_port(void)
{
int n = write(fd, "DSC", 3);
if (n < 0)
fputs("write() of 1 byte failed!\n", stderr);
else
printf("Wrote %0d bytes to serial port\n", n);
}
int read_port(void)
{
int chars_read = 3;
int bytes;
char read_buffer[3] = {0};
int i;
fcntl(fd, F_SETFL, 0);
ioctl(fd, FIONBIO, &bytes);
printf("Number of bytes = %d\n", bytes);
int n = read(fd, read_buffer, chars_read);
printf("Character at Port: %s\n", read_buffer);
printf("Number of chars read = %0d\n", n);
}
Here's the output:
Serial Port Open
Flushing IO Buffers
Wrote 3 bytes to serial port
FIONBIO value 21537
FIONREAD value 21531
Number of bytes = 0
Character at Port: DSC
Number of chars read = 3
Serial Port Closed
For some reason 'Number of bytes' always equals 0. I have no idea why. Is there something wrong with doing this?
int bytes;
ioctl(fd, FIONBIO, &bytes);
printf("Number of bytes = %d\n", bytes);
It's pretty much verbatim from this site:
http://www.cmrr.umn.edu/~strupp/serial.html#config
Am I missing or not understanding something?
BTW I'm just doing a simple loopback test here.
ioctl(fd, FIONBIO, ...) sets a file descriptor (e.g. a socket) into blocking or
non-blocking mode. What you probably meant is
ioctl(fd, FIONREAD, &bytes);
to get the number of bytes available in the input buffer (i.e. the number of bytes
that can be read without blocking.)
I am trying to fetch data from the USB device (say pendrive) connected to the USB port of a system. Here, I am able to open the device file and read some random raw data. But I want to fetch data like minicom/teraterm.
Please let me know what methods and libraries I can use to do it successfully and how can it be done.
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <signal.h>
#include <sys/time.h>
int main()
{
short portfd=-1;
int n,f,len;
char buf[256],*s;
alarm(2);
#if defined(O_NDELAY) && defined(F_SETFL)
portfd = open("/dev/ttyUSB0", O_RDWR|O_NDELAY);
if (portfd >= 0){
/* Cancel the O_NDELAY flag. */
printf("port openend\n");
n = fcntl(portfd, F_GETFL, 0);
(void) fcntl(portfd, F_SETFL, n & ~O_NDELAY);
}
#else
portfd = open("/dev/ttyUSB0", O_RDWR);
#endif
if (portfd >= 0)
{
if (len == 0) len = strlen(s);
for(f = 0; f < len && f <100; f++)
buf[f] = *s++ | 0x80;
write(portfd, buf, f);
printf("Do write\n");
while(portfd>=0){
printf("%s\n",buf);
}
}
alarm(0);
signal(SIGALRM, SIG_IGN);
if (portfd < 0) {
printf("cannot open %s. Sorry.\n", "/dev/ttyUSB0");
}
}
Log of the output:
���������鉀�������������������鍀���������������������������������������������������������������2
����������鉀�������������������鍀���������������������������������������������������������������2
you will need to set the correct port configuration...
struct termios oldtio,newtio;
// open port...
// save existing attributes
tcgetattr(fd,&oldtio);
// set attributes - these flags may change for your device
#define BAUDRATE B9600
memset(&newtio, 0x00, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
//reset attributes
tcsetattr(fd,TCSANOW,&oldtio);
I have a rough working example here... http://file-hub.com/cmd:thread/142300