LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2018, 08:15 AM   #1
crabbit
LQ Newbie
 
Registered: Feb 2018
Posts: 13

Rep: Reputation: Disabled
Problem testing serial program I am writing in C


I have a small program that opens and listens to serial ports. I developed it on a virtual machine and to test it I opened terminal windows using \\.\pipe\com1 (/dev/ttyS0) etc. All this works flawlessly in my VM but when I moved my code over to raspian (Stretch) I am unable to send data using
Code:
screen /dev/ttyUSB1 19200.
My program sets /dev/ttyUSB1 to 19200 but stty reports it as 9600 even when my program is running.

I am not getting any errors when calling

Code:
infd = open (intty, O_RDWR | O_NOCTTY);
or
Code:
tcsetattr (infd, TCSANOW, &newtio)
with
Code:
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
(BAUDRATE = B19200)

Am I missing something that has to do with USB serial ports?

Last edited by crabbit; 03-03-2018 at 08:42 AM.
 
Old 03-03-2018, 01:24 PM   #2
crabbit
LQ Newbie
 
Registered: Feb 2018
Posts: 13

Original Poster
Rep: Reputation: Disabled
One more piece of information. I am able to write but unable to read from the port,

Code:
int main() {
  char byte;
  char buf[100];
  buf[0] = 0;
  int fd = open("/dev/ttyUSB1", O_RDWR);
  if (!fd) printf("Error: %d\n", errno);
  int ret = write(fd, "Test", 4);
  printf("%d\n", ret);
  ret = read(fd, buf, 4);
  printf("error %d\n", errno);
  printf("%d\n", ret);
  buf[5] = 0;
  printf("%s\n", buf);
  return 0;
}
Code:
4
error 0
0
Here is my stty
Code:
pi@raspberrypidev:~/src $ sudo stty -F /dev/ttyUSB1 -a
speed 19200 baud; rows 0; columns 0; line = 0;
intr = <undef>; quit = <undef>; erase = <undef>; kill = <undef>; eof = <undef>;
eol = <undef>; eol2 = <undef>; swtch = <undef>; start = <undef>; stop = <undef>;
susp = <undef>; rprnt = <undef>; werase = <undef>; lnext = <undef>;
discard = <undef>; min = 0; time = 5;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread clocal crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff
-iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt
-echoctl -echoke -flusho -extproc

Last edited by crabbit; 03-03-2018 at 01:26 PM.
 
Old 03-03-2018, 11:34 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Your error-checks are broken: open(2) returns -1 on error; read(2) returns -1 on error, 0 on end-of-file, positive value means length of the data read in.
Also you left out the switch to raw mode https://www.systutorials.com/docs/li...n/3-cfmakeraw/
Nonethess, you still read 4 bytes, but they might be non-printable characters. Use printf's %x in a loop.

Last edited by NevemTeve; 03-03-2018 at 11:39 PM.
 
Old 03-04-2018, 12:14 PM   #4
crabbit
LQ Newbie
 
Registered: Feb 2018
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Your error-checks are broken: open(2) returns -1 on error; read(2) returns -1 on error, 0 on end-of-file, positive value means length of the data read in.
Also you left out the switch to raw mode https://www.systutorials.com/docs/li...n/3-cfmakeraw/
Nonethess, you still read 4 bytes, but they might be non-printable characters. Use printf's %x in a loop.
I corrected the error check and there is no error opening /dev/ttyUSB1.
the write returned that 4 bytes where written.
the read returned 0 bytes where read.
no error with either write or read.

the stty is for /dev/ttyUSB1 after cfmakeraw()

I am getting some more serial cables and null modem adapters to test this using a seperate system rather than screen() or a test program running on the same system.
 
1 members found this post helpful.
Old 03-06-2018, 12:36 PM   #5
crabbit
LQ Newbie
 
Registered: Feb 2018
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by crabbit View Post
I corrected the error check and there is no error opening /dev/ttyUSB1.
the write returned that 4 bytes where written.
the read returned 0 bytes where read.
no error with either write or read.

the stty is for /dev/ttyUSB1 after cfmakeraw()

I am getting some more serial cables and null modem adapters to test this using a seperate system rather than screen() or a test program running on the same system.
Once I connected my laptop to the PI I was able to sucessfully test my program. I suspect that however you write to /dev/ttyUSBx you cannot read "that" data back unless the port is set up for loop back because all programs are running on the host side of the UART.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Python/PYGTK program and writing to external flash memory alsaf Programming 2 10-25-2012 01:11 PM
Problem in writing to serial port. srinathduraisamy Linux - General 2 03-25-2010 12:29 AM
Problem writing on serial device maus Programming 6 10-14-2009 04:46 PM
Serial Port Testing problem mta Linux - Software 1 06-05-2006 06:16 AM
C++ serial port program problem doar Programming 3 10-01-2004 10:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration