[Solved] Sending Data to the Serial Port

Post your Gambas programming questions here.
AndyGable
Posts: 52
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

[Solved] Sending Data to the Serial Port

Post by AndyGable » Sunday 14th February 2021 12:26pm

Hi Everyone

I was hoping someone could advice me how I can send text to the Serial port with Gambas 3.

I have according to my code the port open but I can not work out how to send data to the device (it is a small LCD screen)

I want to first get it to show "Hello World"

any advice would be most welcomed
Last edited by AndyGable on Wednesday 17th February 2021 5:08pm, edited 1 time in total.

User avatar
stevedee
Posts: 378
Joined: Monday 20th March 2017 6:06pm

Re: Sending Data to the Serial Port

Post by stevedee » Sunday 14th February 2021 1:10pm

Sounds like an interesting challenge Andy.

If you haven't already done so, I'd suggest doing a test first to establish that all is OK with your hardware.

Are you connecting an RS232 port from a computer to your mini-LCD display?

You will need protocol information for your display (e.g. how to set baud rates, flow control & so on).

User avatar
stevedee
Posts: 378
Joined: Monday 20th March 2017 6:06pm

Re: Sending Data to the Serial Port

Post by stevedee » Sunday 14th February 2021 1:48pm

...once you have proved that your computer can communicate with your display using a program like minicom, you could try a bit of Gambas like this:-
Public Sub Form_Open()

  With SerialPort1
    .PortName = "/dev/ttyS0"    'needs to be appropriate for your computer
    .Speed = 1200                             'must match printer
    .FlowControl = SerialPort.Hardware        'must match printer
    .DataBits = SerialPort.Bits7              'must match printer
    .StopBits = SerialPort.Bits1              'must match printer
    .Parity = SerialPort.Even                  'must match printer
    .Send = "Hello World!"
  End With

End
...I wish I could rig up a serial comms test-bed and play along with you!

progger
Posts: 4
Joined: Monday 15th February 2021 8:16am

Re: Sending Data to the Serial Port

Post by progger » Monday 15th February 2021 8:26am

You also have to make sure that you add yourself to the "dialout" group in your Linux system. (for example open a terminal and type: sudo adduser your_name dialout).

As mentioned above, first try it with a standard serial communication software to see if the hardware works. Personally, I prefer either Moserial or Cutecom for simple communication.

AndyGable
Posts: 52
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: Sending Data to the Serial Port

Post by AndyGable » Monday 15th February 2021 3:39pm

stevedee wrote:
Sunday 14th February 2021 1:48pm
...once you have proved that your computer can communicate with your display using a program like minicom, you could try a bit of Gambas like this:-
Public Sub Form_Open()

  With SerialPort1
    .PortName = "/dev/ttyS0"    'needs to be appropriate for your computer
    .Speed = 1200                             'must match printer
    .FlowControl = SerialPort.Hardware        'must match printer
    .DataBits = SerialPort.Bits7              'must match printer
    .StopBits = SerialPort.Bits1              'must match printer
    .Parity = SerialPort.Even                  'must match printer
    .Send = "Hello World!"
  End With

End
...I wish I could rig up a serial comms test-bed and play along with you!
I have tried your example and I get the following error on .send

Code: Select all

'SerialPort.Send is not a property  in FMain:14
Any idea what this means?

User avatar
stevedee
Posts: 378
Joined: Monday 20th March 2017 6:06pm

Re: Sending Data to the Serial Port

Post by stevedee » Monday 15th February 2021 3:56pm

Sorry Andy, I'm an idiot. Send is a method.

Also you may need to use the Begin method, so try:-
Public Sub Form_Open()
 
  With SerialPort1
    .PortName = "/dev/ttyS0"    'needs to be appropriate for your computer
    .Speed = 1200                             'must match printer
    .FlowControl = SerialPort.Hardware        'must match printer
    .DataBits = SerialPort.Bits7              'must match printer
    .StopBits = SerialPort.Bits1              'must match printer
    .Parity = SerialPort.Even                  'must match printer
    .Begin()
    .Send("Hello World!")
  End With
 
End

AndyGable
Posts: 52
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: Sending Data to the Serial Port

Post by AndyGable » Tuesday 16th February 2021 3:51pm

stevedee wrote:
Monday 15th February 2021 3:56pm
Sorry Andy, I'm an idiot. Send is a method.

Also you may need to use the Begin method, so try:-
Public Sub Form_Open()
 
  With SerialPort1
    .PortName = "/dev/ttyS0"    'needs to be appropriate for your computer
    .Speed = 1200                             'must match printer
    .FlowControl = SerialPort.Hardware        'must match printer
    .DataBits = SerialPort.Bits7              'must match printer
    .StopBits = SerialPort.Bits1              'must match printer
    .Parity = SerialPort.Even                  'must match printer
    .Begin()
    .Send("Hello World!")
  End With
 
End
Hey Thanks for the advice Stevedee but now I am getting "Too many arguments in FMain:15 on the .Send("Hello World!") line any advice?

User avatar
stevedee
Posts: 378
Joined: Monday 20th March 2017 6:06pm

Re: Sending Data to the Serial Port

Post by stevedee » Tuesday 16th February 2021 4:48pm

Sorry, I'm not doing very well with this one, am I.

Looking more closely, the Send method does not take any arguments, so should be replaced by .Send()

I was hoping that there was an easy way to send some text, but the documentation says you need to establish a Stream and then use Stream methods to send & receive data.

I'll try to look at this again when I have more time.

progger
Posts: 4
Joined: Monday 15th February 2021 8:16am

Re: Sending Data to the Serial Port

Post by progger » Tuesday 16th February 2021 7:23pm

I am doing a lot of serial communications using Gambas.

Here is how I set it up:

Private Comm1 As SerialPort

' Com port initialization
Comm1.PortName = cbCom1.Text
Comm1.Speed = 115200
Comm1.Parity = Comm1.None
Comm1.DataBits = 8
Comm1.StopBits = 1
Comm1.FlowControl = Comm1.None

' Start by using a serial port buffer
Comm1.Begin

' Now write many bytes to that port in a for-loop or whatever way, using the command:
Write #Comm1, data As Byte

' Finally, the buffered data can be send using:
Comm1.Send

AndyGable
Posts: 52
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: Sending Data to the Serial Port

Post by AndyGable » Wednesday 17th February 2021 12:56pm

progger wrote:
Tuesday 16th February 2021 7:23pm
I am doing a lot of serial communications using Gambas.

Here is how I set it up:

Private Comm1 As SerialPort

' Com port initialization
Comm1.PortName = cbCom1.Text
Comm1.Speed = 115200
Comm1.Parity = Comm1.None
Comm1.DataBits = 8
Comm1.StopBits = 1
Comm1.FlowControl = Comm1.None

' Start by using a serial port buffer
Comm1.Begin

' Now write many bytes to that port in a for-loop or whatever way, using the command:
Write #Comm1, data As Byte

' Finally, the buffered data can be send using:
Comm1.Send
so how do I send the text "Hello World"?

Post Reply