Page 1 of 2

[Solved] Sending Data to the Serial Port

Posted: Sunday 14th February 2021 12:26pm
by AndyGable
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

Re: Sending Data to the Serial Port

Posted: Sunday 14th February 2021 1:10pm
by stevedee
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).

Re: Sending Data to the Serial Port

Posted: Sunday 14th February 2021 1:48pm
by stevedee
...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!

Re: Sending Data to the Serial Port

Posted: Monday 15th February 2021 8:26am
by progger
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.

Re: Sending Data to the Serial Port

Posted: Monday 15th February 2021 3:39pm
by AndyGable
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?

Re: Sending Data to the Serial Port

Posted: Monday 15th February 2021 3:56pm
by stevedee
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

Re: Sending Data to the Serial Port

Posted: Tuesday 16th February 2021 3:51pm
by AndyGable
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?

Re: Sending Data to the Serial Port

Posted: Tuesday 16th February 2021 4:48pm
by stevedee
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.

Re: Sending Data to the Serial Port

Posted: Tuesday 16th February 2021 7:23pm
by progger
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

Re: Sending Data to the Serial Port

Posted: Wednesday 17th February 2021 12:56pm
by AndyGable
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"?