LINUX.ORG.RU

Подскажите, как завершить передачу для последовательного порта.

 


0

1

Привет, продолжаю играться с этой штукой: Не могу заставить работать USB <--> RS232 converter

Написал 2 простые программки:


#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

#define OUTPUT "/dev/ttyS0"

int 
main (int argc, char *argv[]) 
{
  char buf[5] = {'a', 'b', 'c', 'd', 'e'};
  int fd_output;
  
   
  fd_output = open(OUTPUT, O_RDWR | O_NOCTTY);
  
  if (fd_output < 0) {
    perror(OUTPUT);
    exit(-1);
  }
  
  write(fd_output, buf, 5);
  fsync(fd_output);
  
   close(fd_output);
  
  return 0;
}


#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

#define INPUT "/dev/ttyUSB0"

int 
main (int argc, char *argv[]) 
{
  char buf[6];
  int fd_input;
  
  fd_input = open(INPUT, O_RDWR | O_NOCTTY);
  
  
 
  if (fd_input < 0) {
    perror(INPUT);
    exit(-1);
  }

  
  read(fd_input, buf, 5);
  close(fd_input);
  
  buf[5] = '\0';
  printf("Buffer: %s", buf);
  
  return 0;
}

Запускаю чтение (2ая программка), затем пишу. Но! чтение не заканчивается и висит, пока я не сделаю echo 'anything' > /dev/ttyS0 (т.е. в выход).

После этого, мой буфер печатается и все ок. Подскажите, что нужно сделать: или какой-то символ конца передачи добавить. Не пойму куда копать.

★★

Последнее исправление: cetjs2 (всего исправлений: 3)

man 2 read

read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.

тебе non-blocking read нужен, видимо.

anonymous
()

printf(«Buffer: %s», buf); и сам по себе может не печататься

вот printf(«Buffer: %s\n», buf); напечатается

ilovewindows ★★★★★
()
Ответ на: комментарий от Boy_from_Jungle

Спасибо. Скорее всего таки в самой программе и буду использовать send/receive. Это было так, для теста.

GreenBag ★★
() автор топика
Ответ на: комментарий от anonymous

read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.

It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal)

AptGet ★★★
()
Ответ на: комментарий от sergijoo

нет, FILE - это совершенно другая вещь, и используется в stdio. Тут используются низкоуровневые небуферизованные файловые операции, соответственно файловый дескриптор здесь int.

slapin ★★★★★
()
1 марта 2014 г.
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.