[Gambas-user] Serial Port issues and events.

nando_f at ...951... nando_f at ...951...
Thu Jun 30 16:34:37 CEST 2016


I've done extensive work with the serial port for a dozen years
with 100% success 24 hour operation.

Sometimes people get mixed up with "0x02" thinking it's binary.
That is 4 characters ascii.
I prefer to use chr(2) to create a 1 length string binary 2.
If you're going to use byte[] array
then byte[0] = 2 is the equivalent.
Using escape (like HTML encoding) is the incorrect thinking
-Fernando

--
Open WebMail Project (http://openwebmail.org)


---------- Original Message -----------
From: ML <d4t4full at ...626...>
To: gambas-user at lists.sourceforge.net
Sent: Thu, 30 Jun 2016 10:51:41 -0300
Subject: Re: [Gambas-user] Serial Port issues and events.

> Alexie,
> 
> Not really. This is my first attempt at a binary-exchange over serial in
> Gambas. Can't also find out the old G3 examples that might have existed
> about this and my web searches were unsuccessful.
> So, if you could point me to some example code to read a Byte[], I'll be
> grateful. My attempts to read to a Byte[] failed with SEGFAULT, or
> errors with no text.
> 
> On the other hand, I just realized -again, happened to me in the past
> and I forgot it did- that the list does not send text inside RAW tags...
> The code can be seen only on Nabble's site, so I'm reinserting it here.
> Please excuse my notation; I know it's not Gambas standard. It's what I
> used for eons on QB45, VB3, VB4, VB5 VB6 and even .Net for my day to day
> work; (really) old habits die hard.
> 
> Declarations:
> 
>   Private Const TEST_TX As String = "ø1" '0x02-0xF8-0x31-0x03 -
> Nabble's RAW TEXT tags removed some data from the string
>   Private Const TEST_RX As String = "" '0x02-0x15-0x03 - Nabble's RAW
> TEXT tags removed some data from the string
>   Private m_tty As SerialPort
>   Private m_rxBuff As String = Null
> 
> Instancing and setting the port up:
> 
>     'Instance serial port and set properties
>     m_tty = New SerialPort As "myTty"  'This should call myTty_Read()
> when there is data to read from the port, but it does not seem to
> trigger. Wonder why.
>     m_tty.PortName = "/dev/" & m_port
>     m_tty.Speed = m_speed
>     Select Case m_parity
>       Case "No/Space"
>         m_tty.Parity = SerialPort.None
>       Case "Even"
>         m_tty.Parity = SerialPort.Even
>       Case "Odd"
>         m_tty.Parity = SerialPort.Odd
>     End Select
>     m_tty.DataBits = m_bits
>     m_tty.StopBits = m_stop
> 
>     'Encoder use hardware handshake (RTS/CTS)
>     m_tty.FlowControl = SerialPort.Hardware
> 
>     'Try opening the port
>     m_tty.Open()
>     m_tty.Blocking = False
>     m_tty.DTR = True
> 
> Then, I send TEST_TX and expect for either TEST_RX or a timeout:
> 
>   Public Function Test() As Boolean
> 
>     Dim tOut As Date = DateAdd(Now, gb.Second, 2)
> 
>     'Reset RX Buffer, send test command and check for timeout/response
>     m_tty.Drop              'Kill remaining send data
>     m_tty.Begin()           'Start buffering
>     Write #m_tty, TEST_TX   'Add to send buffer
>     m_rxBuff = Null         'Kill RX buffer
>     m_tty.Send()            'Send buffer
> 
>     While (Now < tOut) And (m_rxBuff <> TEST_RX)
>       Sleep 0.1
>       'This IF below should not be needed, but myTty_Read() never fires
> otherwise!
>       If m_tty.InputBufferSize > 0 Then
>         myTty_Read()
>       Endif
>     Wend
> 
>     Finally
>       Return (m_rxBuff = TEST_RX)
> 
>     Catch
>       Message.Error("Error '" & Error.Text & "' occurred in " & Error.Where)
> 
>   End
> 
> Finally, this next is the problem code. It does not fire as expected
> when data arrives, and when I call it by hand in the IF above, if I try
> to READ #m_tty As String it chops off the leading 0x02 byte, and I could
> not figure out how to READ #m_tty As Byte[] or As Object (I get a null
> error; nothing on the message.error text, and no OK button on it either):
> 
>   Public Sub myTty_Read()
> 
>     Dim dLen As Integer = m_tty.InputBufferSize
>     Dim sgmnt As Byte[] 'String = Null  'This here is my desperate
> attempt to read As String or As Object...
> 
>     'Data should be available in the serial port buffer
>     'sgmnt = Read #m_tty As String
>     'm_rxBuff &= sgmnt
>     sgmnt = Read #m_tty As Object
>     m_rxBuff &= sgmnt.ToString
>     Debug "Rx: (" & CStr(dLen) & ") " & sgmnt
> 
>     Catch
>       'This is triggered when reading As Object or As Byte[]. But the
> text is NULL and has no OK button!
>       Message.Error("Error '" & Error.Text & "' occurred in " & Error.Where)
> 
>   End
> 
> A breakpoint in the "sgmnt = Read #m_tty As ..." line show that the
> value of "dLen" is three, which is OK. But when I try to READ As String,
> the leading 0x02 is missing from the string, leaving only 0x15-0x03
> (quick tooltip shows the string to be "\x15\x03"). When this is later
> compared to TEST_RX in Test(), the comparison obviously fails.
> 
> TIA,
> zxMarce.
> 
> ------------------------------------------------------------------------------
> Attend Shape: An AT&T Tech Expo July 15-16. Meet us at AT&T Park in San
> Francisco, CA to explore cutting-edge tech and listen to tech luminaries
> present their vision of the future. This family event has something for
> everyone, including kids. Get more information and register today.
> http://sdm.link/attshape
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------





More information about the User mailing list