jokka
Posts: 17
Joined: Mon Apr 29, 2013 10:36 am

Cannot write data to serial port (reading works fine)

Sat May 11, 2013 6:39 pm

I interfaced an Arduino Leonardo via USB with the Pi, which creates /dev/ttyACM0, rights 660 or 666 doesn't matter, user Pi is in "dialout" group.

Now I copied over some C code for writing data to the Arduino, that worked on my notebook, but it doesn't work on the Pi anymore:

Code: Select all

FILE *fp;
fp = fopen("/dev/ttyACM0", "wb");
..error handling..
fprintf(fp, "%c", 'B'); /* write the character 'B' to the serial port)
fflush(fp); /* optional, if more write operations follow, in an actual program */
fclose(fp);
On my notebook, the fflush(fp) command would instantly cause the character to be transmitted to the Arduino, so it can react accordingly.
On the Pi, fflush(fp) just freezes the program and never returns.
I removed the fflush(fp) to check if the write buffers are at least flushed normally when the program eventually terminates, but lo and behold, in that case the program will freeze at fclose(fp) instead!

Any ideas? :/

(Reading Bytes sent by the Arduino from the serial port via fopen("/dev/ttyACM0", "rw") and then fgetc() works fine by the way.
I have also tried wiringPi library, but it has exactly the same results, just freezing.)
Last edited by jokka on Mon Jul 15, 2013 6:25 pm, edited 1 time in total.

User avatar
gordon@drogon.net
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: Cannot write data to serial port (reading works fine)

Sun May 12, 2013 4:32 pm

jokka wrote: I have also tried wiringPi library, but it has exactly the same results, just freezing.)
wiringPi doesn't open the serial port in any sort of blocking or handshaking mode. You should be able to write bytes to it regardless of anything listening or not.

If the exact same code runs on your (Linux) laptop, but not on the Pi then you might be looking at some other issue - kernel driver, etc. who knows. Is there anything in the output of dmesg when it freezes? (Assuming it's just your program that's stopped and not the whole Pi!)

-Gordon
--
Gordons projects: https://projects.drogon.net/

jokka
Posts: 17
Joined: Mon Apr 29, 2013 10:36 am

Re: Cannot write data to serial port (reading works fine)

Tue May 14, 2013 12:26 pm

Just the program hangs, the Pi doesn't freeze and there is nothing in dmesg.

User avatar
gordon@drogon.net
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: Cannot write data to serial port (reading works fine)

Tue May 14, 2013 1:49 pm

jokka wrote:Just the program hangs, the Pi doesn't freeze and there is nothing in dmesg.
Oddly enough, last night I had something similar - the issue turned out to be a power supply one. I was power 2 Pi's off the same PSU - which I've done in the past, but one had a big backlit LCD display on it, the other an Arduino on the serial port, and when I tried to access the Arduino, the program would hang the first time, segfault the next, then the Pi would sort of sulk for a while - I'm guessing the USB hub was a bit low of power. When I unplugged the other Pi it was fine, so check your PSU...

-Gordon
--
Gordons projects: https://projects.drogon.net/

jokka
Posts: 17
Joined: Mon Apr 29, 2013 10:36 am

Re: Cannot write data to serial port (reading works fine)

Tue May 14, 2013 2:50 pm

Interesting. I'm using a 5V/1000mA micro-USB psu, Pi type 2, it has connected:
-Arduino Leonardo
-USB webcam
-USB keyboard

and it is connected
-via LAN to my router, and
-via HDMI-DVI adaptor to my monitor.

I have now unplugged the webcam, keyboard and HDMI port and operate it through ssh.
This did not help though, it still hung.
Then I tried rebooting the Pi, although you didn't mention you had to do that, but it didn't help either.
Then I shutdown the Pi, unplugged power, and restarted it, but no go. :(

Unlike your case I never got segfaults though.
And I can actually read the serial port fine, and get valid data from Arduino, just the write command hangs indefinitely.

jokka
Posts: 17
Joined: Mon Apr 29, 2013 10:36 am

Re: Cannot write data to serial port (reading works fine)

Mon May 20, 2013 7:35 am

Don't tell me I'm the only one with this problem? :geek:

User avatar
joan
Posts: 15971
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Cannot write data to serial port (reading works fine)

Mon May 20, 2013 7:46 am

Try using a program like minicom.

Check the transmit wire is connected.

I can't think of any reason why the program should hang unless you have enabled flow control.

jokka
Posts: 17
Joined: Mon Apr 29, 2013 10:36 am

Re: Cannot write data to serial port (reading works fine)

Mon May 20, 2013 2:07 pm

Thanks, I will try minicom.

Questions though:
-I don't think I have any "transmit wires", the Leonardo connects through USB simply, right?
-How do I check whether flow control is enabled? And wouldn't that also prevent me receiving data from the Arduino? (Which works fine!)

User avatar
joan
Posts: 15971
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Cannot write data to serial port (reading works fine)

Mon May 20, 2013 3:02 pm

jokka wrote:Thanks, I will try minicom.

Questions though:
-I don't think I have any "transmit wires", the Leonardo connects through USB simply, right?
-How do I check whether flow control is enabled? And wouldn't that also prevent me receiving data from the Arduino? (Which works fine!)
I misread your post. I was assuming you were using the serial link on the Pi (the gpio TXD, RXD pins).

jokka
Posts: 17
Joined: Mon Apr 29, 2013 10:36 am

Re: Cannot write data to serial port (reading works fine)

Mon Jul 15, 2013 3:07 am

Ok, it has been MONTHS and I still cannot flush writes or any app that tries to do so will just FREEZE.
Can anyone please tell me how to fix this? It works just fine on every other machine.
I tried rpi-update. Is there any other magical update/fix script I should know about?

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia

Re: Cannot write data to serial port (reading works fine)

Mon Jul 15, 2013 3:39 am

To quote Gordon from another thread:

Have you turned off the GETTY process that normally runs on the line?

PiHeich
Posts: 25
Joined: Sun Jul 14, 2013 8:48 pm

Re: Cannot write data to serial port (reading works fine)

Mon Jul 15, 2013 9:52 am

Is possible to wiev your reading file? I have a problem, for read some data i need to recive about 30/100 charater...

Thanks

jokka
Posts: 17
Joined: Mon Apr 29, 2013 10:36 am

Re: Cannot write data to serial port (reading works fine)

Mon Jul 15, 2013 1:51 pm

Code: Select all

$ ps -ef | grep getty
root      3315     1  1 15:48 tty1     00:00:00 /sbin/getty --noclear 38400 tty1
root      3316     1  1 15:48 tty2     00:00:00 /sbin/getty 38400 tty2
root      3317     1  1 15:48 tty3     00:00:00 /sbin/getty 38400 tty3
root      3318     1  1 15:48 tty4     00:00:00 /sbin/getty 38400 tty4
root      3319     1  1 15:48 tty5     00:00:00 /sbin/getty 38400 tty5
root      3320     1  1 15:48 tty6     00:00:00 /sbin/getty 38400 tty6
root      3321     1  1 15:48 ?        00:00:00 /sbin/getty -L ttyAMA0 115200 vt100
getty is on ttyAMA0, but Arduino is on ttyACM0.

Yes I can read data just fine, but when I write data, as soon as the data actually gets flushed for transmission (or the connection is closed, which will also try to transmit the pending data) the program that tries to do so will hang until CTRL+C'ed.

I turned off getty on ttyAMA0 anyway, and also issued

Code: Select all

stty -icanon -F /dev/ttyACM0
but to no avail. It still freezes on writing.

I tried minicom, and it reads data just fine but as soon as I press a key, it hangs too :-p
Is it possible that my Pi simply has faulty hardware?

(for the record: My Pi runs latest raspberrian wheezy, everything up2date)
Last edited by jokka on Mon Jul 15, 2013 3:08 pm, edited 2 times in total.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia

Re: Cannot write data to serial port (reading works fine)

Mon Jul 15, 2013 2:42 pm

jokka wrote:getty is on ttyAMA0, but Arduino is on ttyACM0.
Yes, sorry, missed that!

PiHeich
Posts: 25
Joined: Sun Jul 14, 2013 8:48 pm

Re: Cannot write data to serial port (reading works fine)

Mon Jul 15, 2013 3:45 pm

Can i have your .c read file pls?

jokka
Posts: 17
Joined: Mon Apr 29, 2013 10:36 am

Re: Cannot write data to serial port (reading works fine)

Mon Jul 15, 2013 6:25 pm

PiHeich wrote:Can i have your .c read file pls?
It's in my opening posting.

jokka
Posts: 17
Joined: Mon Apr 29, 2013 10:36 am

Re: Cannot write data to serial port (reading works fine)

Mon Jul 15, 2013 8:06 pm

I backed up the Pi, wiped it and installed the Archlinux image.
Same result: Reading is fine, writing hangs.

As I already wrote, this reading/writing works in Arch on my PC out of the box, so this points towards a hardware failure or a problem with ARM architecture, doesn't it?

jokka
Posts: 17
Joined: Mon Apr 29, 2013 10:36 am

Re: Cannot write data to serial port (reading works fine)

Mon Jan 27, 2014 2:39 pm

Until today the problem of write->freeze persists. Does anyone have any news regarding this?

trbennett48
Posts: 8
Joined: Mon Feb 10, 2014 8:46 pm

Re: Cannot write data to serial port (reading works fine)

Mon Feb 10, 2014 9:53 pm

Have you reviewed the "rpi" specific info contained in this link:
http://elinux.org/RPi_Serial_Connection

-tony

IanH
Posts: 68
Joined: Mon Dec 19, 2011 5:53 pm

Re: Cannot write data to serial port (reading works fine)

Wed Feb 19, 2014 8:28 am

Firstly - I've used a Leonardo serial port from Python (using the python-serial module) on a Pi running raspbian with no problems. There is some Python code at http://elinux.org/Serial_port_programming which should get you started.

If the Python works but C doesn't, maybe there is a termios setting http://linux.die.net/man/3/termios which is wrong. It may be worth calling tcgetattr() and looking through the results for anything odd.

One thing - on the Leonardo, are you doing this in setup():

Code: Select all

  Serial.begin(115200); 
  while (!Serial)
    ;
The while loop is necessary on Leonardo - it pauses until the serial port has connected properly - and isn't needed on FTDI-based Arduinos.

The only other things I can think of are:
a) putting NOOBS or Rasbian on a spare SD card and trying that.
b) making sure all your Arduino firmware is up-to-date in case its a USB device-end problem.
https://github.com/IanHarvey

Hugh-Roger
Posts: 27
Joined: Sat Jan 18, 2014 12:31 am

Re: Cannot write data to serial port (reading works fine)

Sun Mar 02, 2014 11:05 pm

I wrote a multi MCU/Raspberry Pi SCADA application recently and I based my serial comms code for Raspberry Pi on this

http://stackoverflow.com/questions/6947 ... -port-in-c

It works well, although later down the line needed some finer tuning for my purposes.

Note that the port name for RPi is "/dev/ttyAMA0"

Also note that before doing any serial comms you need to

Nobble serial logging in /boot/cmdline.txt
Nobble serial getty in /etc/inittab

GuyLol
Posts: 1
Joined: Sun Dec 18, 2016 3:16 pm

Re: Cannot write data to serial port (reading works fine)

Sun Dec 18, 2016 3:20 pm

Hey . Just try ls /dev/tty* before and after you plugin the arduino to the raspberry pi .
The one that appears in the list after the plugging in operation is what you need to be using (which is not AMA0 occasionally ) .

In my case ttyUSB0 was the one not ttyAMA0.
Hope it helped .

Return to “C/C++”