[Solved] Sending Data to the Serial Port

Post

Posted
Rating:
#1 (In Topic #581)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 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
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘Regular’
 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).
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
stevedee is in the usergroup ‘Regular’
…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:-

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   With SerialPort1
  4.     .PortName = "/dev/ttyS0"    'needs to be appropriate for your computer
  5.     .Speed = 1200                             'must match printer
  6.     .FlowControl = SerialPort.Hardware        'must match printer
  7.     .DataBits = SerialPort.Bits7              'must match printer
  8.     .StopBits = SerialPort.Bits1              'must match printer
  9.     .Parity = SerialPort.Even                  'must match printer
  10.     .Send = "Hello World!"
  11.  

…I wish I could rig up a serial comms test-bed and play along with you!
Online now: No Back to the top

Post

Posted
Rating:
#4
Trainee
 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.
Online now: No Back to the top

Post

Posted
Rating:
#5
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

stevedee said

…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:-

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   With SerialPort1
  4.     .PortName = "/dev/ttyS0"    'needs to be appropriate for your computer
  5.     .Speed = 1200                             'must match printer
  6.     .FlowControl = SerialPort.Hardware        'must match printer
  7.     .DataBits = SerialPort.Bits7              'must match printer
  8.     .StopBits = SerialPort.Bits1              'must match printer
  9.     .Parity = SerialPort.Even                  'must match printer
  10.     .Send = "Hello World!"
  11.  

…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

'SerialPort.Send is not a property  in FMain:14

Any idea what this means?
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
stevedee is in the usergroup ‘Regular’
Sorry Andy, I'm an idiot. Send is a method.

Also you may need to use the Begin method, so try:-

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   With SerialPort1
  4.     .PortName = "/dev/ttyS0"    'needs to be appropriate for your computer
  5.     .Speed = 1200                             'must match printer
  6.     .FlowControl = SerialPort.Hardware        'must match printer
  7.     .DataBits = SerialPort.Bits7              'must match printer
  8.     .StopBits = SerialPort.Bits1              'must match printer
  9.     .Parity = SerialPort.Even                  'must match printer
  10.     .Begin()
  11.     .Send("Hello World!")
  12.  
Online now: No Back to the top

Post

Posted
Rating:
#7
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

stevedee said

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

Also you may need to use the Begin method, so try:-

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   With SerialPort1
  4.     .PortName = "/dev/ttyS0"    'needs to be appropriate for your computer
  5.     .Speed = 1200                             'must match printer
  6.     .FlowControl = SerialPort.Hardware        'must match printer
  7.     .DataBits = SerialPort.Bits7              'must match printer
  8.     .StopBits = SerialPort.Bits1              'must match printer
  9.     .Parity = SerialPort.Even                  'must match printer
  10.     .Begin()
  11.     .Send("Hello World!")
  12.  

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?
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Regular
stevedee is in the usergroup ‘Regular’
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.
Online now: No Back to the top

Post

Posted
Rating:
#9
Trainee
 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
Online now: No Back to the top

Post

Posted
Rating:
#10
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

progger said

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"?
Online now: No Back to the top

Post

Posted
Rating:
#11
Trainee
 Write #Comm1, "Hello World!"
Online now: No Back to the top

Post

Posted
Rating:
#12
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 To all who have helped

Thank-you I have it working now

the next Serial challange I will have is getting the status data from the PoS Printer (but that can wait for another day)
Online now: No Back to the top
1 guest and 0 members have just viewed this.