Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sat Sep 21, 2019 2:11 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Serial Port on Linux / USB adapters / CR LF issues
PostPosted: Tue Jan 13, 2009 11:12 am 
Offline

Joined: Tue Jan 13, 2009 10:49 am
Posts: 5
Hi All,

First I believe in giving before receiving, so here is some info not found (or not easily found!) in the manuals/forum that may be of use to others in the future trying to use USB<>serial adapters with RB under Linux.

System.SerialPortCount does not find USB adapters like /dev/ttyUSB0 (tested with devices using prolific and fdti chipsets). It only finds /dev/ttyS0 etc.

However you can still open the USB devices by using this syntax
Serial1.SerialPort = System.SerialPort("/dev/ttyUSB0")

Also you may need to 'modprobe' fdti devices to get Linux to recognise the correct VID / PID. This process is well explained in this link http://www.rev-ed.co.uk/docs/axe027.pdf. The prolific devices worked straight away.

Now the issue:

I am building a cross platform program (RB2008 5.1 on Windows to target Windows/Mac Intel/Linux) to receive data from a microcontroller that is outputiing raw data bytes (not ASCII) such as temperature.

Everything works fine on Windows and Mac - I can Serial1.ReadAll the data in and get rising temperatures displayed such as 9-10-11-12-13-14

However on Linux it insists on displaying 9-10-11-12-10-14

Clearly CR (13, 0x0D) is being 'automatically' changed to LF (10, 0x0A), but i don't want this as I am using binary data not text strings. How do I stop this?

I have tried every combination of Encodings.* in

Dim s as String = Serial1.ReadAll(Encodings.ASCII)

and even

Dim mb as Memory Block = Serial1.ReadAll

I also tried a GNU-C test program on the same machine and 13 is displayed fine, so the issue is definitely in RB, and in Linux only (Mac and Windows are ok). Tested on both Ubunto and eeePC.

Any help anyone?

Many thanks


Top
 Profile  
Reply with quote  
 Post subject: Re: Serial Port on Linux / USB adapters / CR LF issues
PostPosted: Tue Jan 13, 2009 2:15 pm 
Offline
User avatar

Joined: Tue Oct 25, 2005 1:57 pm
Posts: 332
I'm afraid I don't an answer for you with regard to getting the Linux version of you app to behave properly. This may be a bug in RB that nothing can be done about. Do see the same behavior with LookAhead as opposed to ReadAll?
Do you have control over the code running on the microcontroller? could you change the format it uses to transmit data as ASCII hex strings, i.e "09"-"0A"-"0B"-"0C"-"0D"-"0E"? It would require twice as may characters for each reading, but it would circumvent your problem.

_________________
http://freeware.the-meiers.org
http://opensource.the-meiers.org


Top
 Profile  
Reply with quote  
 Post subject: Re: Serial Port on Linux / USB adapters / CR LF issues
PostPosted: Thu Jan 15, 2009 6:54 am 
Offline

Joined: Tue Jan 13, 2009 10:49 am
Posts: 5
Lookahead works the same, and unfortunately I can't change the microcontroller output. Looks to be a Linux/RB issue.


Top
 Profile  
Reply with quote  
 Post subject: Re: Serial Port on Linux / USB adapters / CR LF issues
PostPosted: Thu Jan 15, 2009 10:24 am 
Offline

Joined: Sun Oct 09, 2005 4:41 pm
Posts: 57
Location: Rheinbach, Germany
I know it's not helpful, but I have the same issue on Linux with RB since years. I filed a bug report but it was closed with no result.
In my Thread ( viewtopic.php?f=8&t=249 ) you can read the answers of my post. Unfortunately I could not solve my problem. Maybe you can?

Dierk

_________________
Dierk Walter

http://www.vman.cc
Show Control and more


Top
 Profile  
Reply with quote  
 Post subject: Re: Serial Port on Linux / USB adapters / CR LF issues
PostPosted: Thu Jan 15, 2009 11:03 am 
Offline

Joined: Tue Jan 13, 2009 10:49 am
Posts: 5
Many thanks for your reply, it pointed me in the right direction. I have done some C tests and identified the issue.

The RB issue is that it opens the serial port, by default or accident, with the inputs_option, c_iflag, with the ICRNL flag enabled. This is not a specific driver issue (tried with default serial, and USB prolific and ftdi drivers - all work the same), it is the way the port is opened. It may just 'open that way by default' on Linux.

See the Input Options at
http://dce.felk.cvut.cz/pos/cv5/doc/serial.html
for more on the c_iflag settings


Now here is some C code to change the settings when the port is open. if someone can tell me how to translate this from C into RB speak, we can resolve the issue. fd is the handle to the serial port (Aaran suggests Serial1.Handle might work).

#include <termios.h>

&nbsp; &nbsp;struct termios newIo;
&nbsp; &nbsp;newIo.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
&nbsp; &nbsp;newIo.c_iflag = 0;
&nbsp; &nbsp;newIo.c_oflag = 0;
&nbsp; &nbsp;newIo.c_lflag = 0;
&nbsp; &nbsp;newIo.c_cc[VMIN] = 0;
&nbsp; &nbsp;newIo.c_cc[VTIME] = 0;

&nbsp; &nbsp;if (tcflush(fd, TCIFLUSH)) {
&nbsp; &nbsp;&nbsp; &nbsp;//err("tcflush() failed.\n");
&nbsp; &nbsp;&nbsp; &nbsp;return -1;
&nbsp; &nbsp;}
&nbsp; &nbsp;if (tcsetattr(fd, TCSANOW, &newIo)) {
&nbsp; &nbsp;&nbsp; &nbsp;//err("tcsetattr() failed.\n");
&nbsp; &nbsp;&nbsp; &nbsp;return -1;
&nbsp; &nbsp;}


Top
 Profile  
Reply with quote  
 Post subject: Re: Serial Port on Linux / USB adapters / CR LF issues
PostPosted: Wed Jan 21, 2009 5:27 am 
Offline

Joined: Tue Jan 13, 2009 10:49 am
Posts: 5
Issue
Linux builds do not operate in the same way as Mac/Windows when binary data is received via the SerialPort control. Specifically the data value 13 is corrupted to 10. This means third party instrumentation sending binary data cannot be used correctly.

Bug
Although RB uses the same code for Linux/Mac, the default operating system behaviour of the c_iflag, ICRNL differs (clear on Mac, set on Linux). As RB does not seem to explicitly set/clear this flag but instead accepts the operating system default, different behaviour of the Linux build is experienced.

Resolution
RB should explictly clear the ICRNL flag when opening a port for Mac/Linux. This will ensure consistant behaviour with all builds (Windows/Mac/Linux). It is trivial exercise for someone who actually really wants the 13 converted to 10 to do so once the data has been .readall

Workaround
dim portname as New String
portname = "/dev/ttyUSB0"

#if targetLinux
dim sh as New Shell
sh.Execute "stty -F "+ portname +" -icrnl"

while sh.IsRunning
app.doevents
wend
#endif

dim sp as New SerialPort
sp.SerialPort = System.SerialPort(portname)
sp.open
&nbsp; &nbsp;
etc...


Top
 Profile  
Reply with quote  
 Post subject: Re: Serial Port on Linux / USB adapters / CR LF issues
PostPosted: Wed Jan 21, 2009 8:59 am 
Offline

Joined: Sun Oct 09, 2005 4:41 pm
Posts: 57
Location: Rheinbach, Germany
Wow, thank you for your solution. It works. I cant believe that after years living with this bug someone find a workaround. Great Job.

_________________
Dierk Walter

http://www.vman.cc
Show Control and more


Top
 Profile  
Reply with quote  
 Post subject: Re: Serial Port on Linux / USB adapters / CR LF issues
PostPosted: Wed Jan 21, 2009 1:50 pm 
Offline
User avatar

Joined: Tue Oct 25, 2005 1:57 pm
Posts: 332
CPS wrote:
Bug
Although RB uses the same code for Linux/Mac, the default operating system behaviour of the c_iflag, ICRNL differs (clear on Mac, set on Linux). As RB does not seem to explicitly set/clear this flag but instead accepts the operating system default, different behaviour of the Linux build is experienced.


Great job in figuring this out. Have you filed a bug report?

_________________
http://freeware.the-meiers.org
http://opensource.the-meiers.org


Top
 Profile  
Reply with quote  
 Post subject: Re: Serial Port on Linux / USB adapters / CR LF issues
PostPosted: Wed Jan 21, 2009 2:56 pm 
Offline

Joined: Sun Oct 09, 2005 4:41 pm
Posts: 57
Location: Rheinbach, Germany
roger wrote:

Great job in figuring this out. Have you filed a bug report?


I have filed a Bug report in 2005. But RB said it is no bug and they closed the report as fixed.

_________________
Dierk Walter

http://www.vman.cc
Show Control and more


Top
 Profile  
Reply with quote  
 Post subject: Re: Serial Port on Linux / USB adapters / CR LF issues
PostPosted: Thu Jan 22, 2009 10:22 am 
Offline

Joined: Tue Jan 13, 2009 10:49 am
Posts: 5
v-man wrote:
Wow, thank you for your solution. It works. I cant believe that after years living with this bug someone find a workaround. Great Job.


No problem, I had to find a workaround or else my software would just not work under Linux!
In my opinion it is a RB bug because different platform builds behave in different ways.
I have already filed it again as a bug (case 5862).


Top
 Profile  
Reply with quote  
 Post subject: Re: Serial Port on Linux / USB adapters / CR LF issues
PostPosted: Thu Jan 22, 2009 6:49 pm 
Offline
User avatar

Joined: Tue Oct 25, 2005 1:57 pm
Posts: 332
v-man wrote:
roger wrote:

Great job in figuring this out. Have you filed a bug report?


I have filed a Bug report in 2005. But RB said it is no bug and they closed the report as fixed.


Might be worthwile to file it again, with an explanation with your findings. Knowing how to fix the bug they may be more inclined to actually fixing it.

_________________
http://freeware.the-meiers.org
http://opensource.the-meiers.org


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group