Serial Port usage Linux versus Win

Linux specific questions.
KenHorse
Posts: 36
Joined: Jan 27, 2012 0:08

Serial Port usage Linux versus Win

Postby KenHorse » Aug 12, 2017 17:57

I have a program that works perfectly when compiled and run under Win10 but no so much when compiled and ran under Linux (specifically Debian 3.16.0-4-amd64).

First of all, I'm running the program as root so /dev/ttyS0 permissions isn't the issue.

My program first opens the port at 9600 baud in order to send a command to the external device connected
So I open the port and send the command:

Code: Select all

Open Com Command(1) + ":9600,n,8,1,CS0,DS0,CD0,RS" For Binary As #2
If Err > 0 Then
   Print "Can't Open Serial Port 1st time. Press Any Key To Exit"
   Sleep
   Stop
End If

'send reset command
sleep 10
Print #2, "1*219999" ; Chr(13);
Sleep 1000

'close port and reopen at 57600 baud
Close #2

Open Com Command(1) + ":57600,n,8,1,CS0,DS0,CD0,RS," For Binary As #2
If Err > 0 Then
   Print "Can't Open Serial Port 2nd time. Press Any Key To Exit"
   Sleep
   Stop
End If
Sleep 2000

'Wait for device to send "%"
Bretries = 20                                          'we try 20 times

TESTFORSTART:

Get #2, , InByte
sleep 50  'need delay for some reason

Print "First InByte " ; InByte

If InByte = 37 Then                                      'did we receive a % or 37 ?
  print "We received a start from device"
  Print #2 , "{";                                       'send byte 123 to bootloader
  Sleep 10 
  Goto PortLoader
Else                                                        'If InpByte = 0 Then
  Bretries = Bretries - 1
  If Bretries <> 0 Then
    Goto TestforStart                  'we test again
  End If
End If                                                      'we received a %
Print "Timeout waiting for start character. Press Any Key To Exit"
   Sleep
   Stop


Portloader is a label that starts an xmodem transfer to the external device.

It appears that the problem is that when compiled under Linux, #2 doesn't close and reopen under the new baud rate but rather stays open at 9600 no matter what. Is this a bug or am I doing something wrong?
fxm
Posts: 7272
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: Serial Port usage Linux versus Win

Postby fxm » Aug 12, 2017 18:36

Have you tried calling "Freefile" just before each "Open Com" block immediately chained, rather than always forcing the same file number # 2 (see below your provided code adapted for this) ?

Code: Select all

Dim f As Integer

f = Freefile
Open Com Command(1) + ":9600,n,8,1,CS0,DS0,CD0,RS" For Binary As #f
If Err > 0 Then
   Print "Can't Open Serial Port 1st time. Press Any Key To Exit"
   Sleep
   Stop
End If

'send reset command
sleep 10
Print #f, "1*219999" ; Chr(13);
Sleep 1000

'close port and reopen at 57600 baud
Close #f

f = Freefile
Open Com Command(1) + ":57600,n,8,1,CS0,DS0,CD0,RS," For Binary As #f
If Err > 0 Then
   Print "Can't Open Serial Port 2nd time. Press Any Key To Exit"
   Sleep
   Stop
End If
Sleep 2000

'Wait for device to send "%"
Bretries = 20                                          'we try 20 times

TESTFORSTART:

Get #f, , InByte
sleep 50  'need delay for some reason

Print "First InByte " ; InByte

If InByte = 37 Then                                      'did we receive a % or 37 ?
  print "We received a start from device"
  Print #f , "{";                                       'send byte 123 to bootloader
  Sleep 10
  Goto PortLoader
Else                                                        'If InpByte = 0 Then
  Bretries = Bretries - 1
  If Bretries <> 0 Then
    Goto TestforStart                  'we test again
  End If
End If                                                      'we received a %
Print "Timeout waiting for start character. Press Any Key To Exit"
   Sleep
   Stop
KenHorse
Posts: 36
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Postby KenHorse » Aug 12, 2017 18:50

I just made the change you suggested and still no go
KenHorse
Posts: 36
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Postby KenHorse » Aug 12, 2017 19:42

It appears that FB is not properly setting up the comports under Linux (at least the version I'm running).

As I mentioned in my earlier post, it is obvious that FB is not properly configuring ttyS0's baud rate. In fact, once it is set to a baud rate (via stty), FB doesn't change that no matter what.
KenHorse
Posts: 36
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Postby KenHorse » Aug 12, 2017 19:56

Well, here's the fix (in my case at least)

After each Open command, I call stty to set the baud rate:

Code: Select all

Open Com Command(1) + ":9600,n,8,1,CS0,DS0,CD0,RS" For Binary As #2
shell "stty -F /dev/ttyS0 9600"

Close #2

Open Com Command(1) + ":57600,n,8,1,CS0,DS0,CD0,RS" For Binary As #2
shell "stty -F /dev/ttyS0 57600"


Works every time
fxm
Posts: 7272
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: Serial Port usage Linux versus Win

Postby fxm » Aug 13, 2017 6:21

I do not know anything about serial ports or Linux, but in the documentation on "OPEN COM", there is written about the differences between platforms:
    Platform Differences:
    • On the Windows platform "COM:" maps to "COM1:"
    • On the Linux platform
      "COM:" maps to "/dev/modem"
      "COM1:" maps to "/dev/ttyS0"
      "COM2:" maps to "/dev/ttyS1", etc
      "/dev/xyz:" maps to "/dev/xyz", etc

Does this mean that in your case it is better to use "COM1" instead of "COM" ?
KenHorse
Posts: 36
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Postby KenHorse » Aug 13, 2017 15:38

fxm wrote:I do not know anything about serial ports or Linux, but in the documentation on "OPEN COM", there is written about the differences between platforms:
    Platform Differences:
    • On the Windows platform "COM:" maps to "COM1:"
    • On the Linux platform
      "COM:" maps to "/dev/modem"
      "COM1:" maps to "/dev/ttyS0"
      "COM2:" maps to "/dev/ttyS1", etc
      "/dev/xyz:" maps to "/dev/xyz", etc

Does this mean that in your case it is better to use "COM1" instead of "COM" ?


No, it means for one reason or another, FreeBasic does not properly set the baud rate of the comport under Linux (well, my particular version of Linux at least)
St_W
Posts: 1056
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Serial Port usage Linux versus Win

Postby St_W » Aug 13, 2017 15:42

KenHorse wrote:No, it means for one reason or another, FreeBasic does not properly set the baud rate of the comport under Linux (well, my particular version of Linux at least)
And only when opening the same port for the second time, if I understood that correctly?

Probably a bug report with a description and a short code sample to reproduce the error should be created.
KenHorse
Posts: 36
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Postby KenHorse » Aug 13, 2017 15:46

St_W wrote:
KenHorse wrote:No, it means for one reason or another, FreeBasic does not properly set the baud rate of the comport under Linux (well, my particular version of Linux at least)
And only when opening the same port for the second time, if I understood that correctly?

Probably a bug report with a description and a short code sample to reproduce the error should be created.


Actually no. It is hit or miss if the baud rate is correctly set at all (at least for me). For instance, if I set the baud rate manually via stty, Freebasic may or may not change it on the try. However it appears it will NEVER set it correctly if closed and then reopened with a new baud rate.
St_W
Posts: 1056
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Serial Port usage Linux versus Win

Postby St_W » Aug 13, 2017 16:08

KenHorse wrote:Actually no. It is hit or miss if the baud rate is correctly set at all (at least for me). For instance, if I set the baud rate manually via stty, Freebasic may or may not change it on the try.
So it works occasionally? Or are those just the cases where the baud rate already was configured to the appropriate value?
KenHorse
Posts: 36
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Postby KenHorse » Aug 13, 2017 16:15

St_W wrote:
KenHorse wrote:Actually no. It is hit or miss if the baud rate is correctly set at all (at least for me). For instance, if I set the baud rate manually via stty, Freebasic may or may not change it on the try.
So it works occasionally? Or are those just the cases where the baud rate already was configured to the appropriate value?


Only if the baud rate was previously set (by means other than FB).

Where can I report this apparent bug?
St_W
Posts: 1056
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Serial Port usage Linux versus Win

Postby St_W » Aug 13, 2017 16:29

KenHorse wrote:Where can I report this apparent bug?
see http://freebasic.net/wiki/wikka.php?wak ... ortingBugs
I you don't have a sourceforge account and do not want to create one, but already have a github account you can also submit an issue here: https://github.com/freebasic/fbc/issues (the primary bugtracker is on sourceforge, though)

Putting a link to this forum thread in the bug report might be a good idea.
KenHorse
Posts: 36
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Postby KenHorse » Aug 13, 2017 16:46

St_W wrote:
KenHorse wrote:Where can I report this apparent bug?
see http://freebasic.net/wiki/wikka.php?wak ... ortingBugs
I you don't have a sourceforge account and do not want to create one, but already have a github account you can also submit an issue here: https://github.com/freebasic/fbc/issues (the primary bugtracker is on sourceforge, though)

Putting a link to this forum thread in the bug report might be a good idea.


Thanks and done!

Return to “Linux”

Who is online

Users browsing this forum: No registered users and 1 guest