Serial Port usage Linux versus Win

Linux specific questions.
Post Reply
KenHorse
Posts: 56
Joined: Jan 27, 2012 0:08

Serial Port usage Linux versus Win

Post by KenHorse »

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
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Serial Port usage Linux versus Win

Post by fxm »

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: 56
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Post by KenHorse »

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

Re: Serial Port usage Linux versus Win

Post by KenHorse »

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: 56
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Post by KenHorse »

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
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Serial Port usage Linux versus Win

Post by fxm »

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: 56
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Post by KenHorse »

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: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Serial Port usage Linux versus Win

Post by St_W »

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: 56
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Post by KenHorse »

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: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Serial Port usage Linux versus Win

Post by St_W »

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: 56
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Post by KenHorse »

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: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Serial Port usage Linux versus Win

Post by St_W »

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: 56
Joined: Jan 27, 2012 0:08

Re: Serial Port usage Linux versus Win

Post by KenHorse »

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!
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Serial Port usage Linux versus Win

Post by coderJeff »

@KenHorse, what version of fbc are you running? Does second OPEN COM ever succeed?

This is my test program just to cycle through opening the COM multiple times at each speed.

Code: Select all

dim speed(0 to ...) as integer = { _
	50, 150, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, _
	115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000 _
}

const settings = ",n,8,1"

if command(1) = "" then
	print "Usage: " & command(0) & " COMPORT"
	end 1
end if 

dim c as string = command(1) & ":SPEED" & settings
print c

for i as integer = lbound(speed) to ubound(speed)
	c = command(1) & ":" & speed(i) & settings
	print speed(i) & " = ";
	Open COM c For Binary As #2
	if err = 0 then 
		print "Yes"
		close #2
	else
		print "No"
	end if
next
Post Reply