[Gambas-user] Serial Port issues and events.
Alexie
ualex73 at ...626...
Thu Jun 30 18:22:18 CEST 2016
To read into an array, do something like:
Public Sub SerialPort_Read()
Dim aData As New Byte[]
Dim iLen As Integer
iLen = Lof(Last)
aData.Resize(iLen)
Try aData.Read(Last, 0, iLen)
End
2016-06-30 17:44 GMT+02:00 ML <d4t4full at ...626...>:
> Fernando,
>
> I know what you mean. In my case I just wanted to show in the examples
> which bytes come and go through the port.
> I do know 0x02 (&H2 for Gambas) is a single byte with value 2. I'm not
> transmitting "0-x-0-2" over the wire as 4 bytes, I'm just transmitting a
> string whose first character has ASCII value 0x02.
>
> I have also been coding for years (started with QB45 in the nineties,
> and before that with ZX-81 and ZXSpectrum in BASIC and even Assembler).
> I have coded several countries' Fiscal Printer interfaces for my company
> (all of them RS232C). But all this I did on Mr Gates' in-operating
> systems and IDEs.
>
> I do not know, though, the basics of RS232 I/O on Gambas3, methinks:
> I do not know -for example- what is wrong in my code that makes serial
> xxx_READ events not trigger.
> I also do not know how to write/read proper binary-as-string (or even
> bynary-as-byte[]) to/from RS232.
>
> My particular issue with the program:
> When the .InputBufferSize is not zero, it shows 3, which is correct (I'm
> expecting three bytes or chars, with values 0x02-0x15-0x03).
> Immediately afterwards, when I READ these bytes into a string (the only
> working thing so far), the first byte (0x02) is chopped off and the LEN
> of the resulting string is 2.
> I wonder: How come? There were three bytes in the buffer and I get a two
> byte string!
> I think Gambas is treating the 0x02 as a string-lenght indicator, and
> fills the string with the rest of the buffer, therefore fetching 2
> characters (this seems backed because the string gets the 0x15 and 0x03
> bytes as its contents). But I need all three bytes.
> My not-so-wild-guess (which agrees with your advise) is that I must use
> Byte[] and not String to read these blocks. Problem is twofold:
> 1- (BAD) Cannot get the _READ event to trigger
> 2- (WORSE) Cannot figure out how to make Gambas return a Byte[] with the
> buffer contents.
>
> Any pointers or working examples in those directions WILL help. Hope I
> explained myself better now.
>
> Regards,
> zxMarce.
>
> On 2016-06-30 11:34, nando_f at ...951... wrote:
> > 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 -------
> >
> >
> >
> ------------------------------------------------------------------------------
> > 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
>
>
>
>
> ------------------------------------------------------------------------------
> 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
>
More information about the User
mailing list