Tip: Event driven RS232 class (windows)

For issues with communication ports, protocols, etc.
midnightrambler905
Posts: 9
Joined: Aug 07, 2009 20:57
Location: Toronto GTA, ON, Canada
Contact:

Success

Post by midnightrambler905 »

Thanks for your help phishguy. I stripped everything out that I didnt need and made the code as compact aspossible and have a VERY simple little module to import scanned barcodes into the old app.

Really kind of embarassing at how long it took me (I think i was WAY too cautious and apprehensive when I started). My first attempts failed miserably, but once I got it partially working, and then set about determining exactly what I didnt need anymore, the task looked very small and simple.

Thankfully these forums & the freeBASIC help files saved me from asking some (what look like dumb questions now). And the final step to completion was to add a wait step to allow the input buffer to fill (was losing characters when scanning larger barcodes).

Once again, thank-you very much. Hope to be asking you & other forum participants more questions in the not too distant future when I begin the re-write of this old app (lots of planning & reading to do before I begin LOL).

.... Tony
midnightrambler905
Posts: 9
Joined: Aug 07, 2009 20:57
Location: Toronto GTA, ON, Canada
Contact:

another question regarding this thread

Post by midnightrambler905 »

Hi Guys:

I know its been almost a year since my last post, but I have a question.

I have been reveiwing all the past posts, and some of my email, but the answer eludes me. When i went thru the FAQs, it says that FB does not support USB ports, but I have foundreferences to USB HID devices (not truly sure what the HID is other than human interface device - assuming that means mice and keyboards). But I was sure in last years correspondence someone told me that I could easily modify the program that was written to work with a USB port instead of the RS232 COM ports (I wish I could remember who said it, but that is probably why i cant find the answer in previous correspondence). I came into possession of a inexpensive USB barcode scanner (how much cheaper than free can you get LOL), and was wondering what to modify to access the USB port the same way as the COM port?

Your help is greatly appreciated
D.J.Peters
Posts: 8616
Joined: May 28, 2005 3:28
Contact:

Re: Tip: Event driven RS232 class (windows)

Post by D.J.Peters »

new version:
changes:
Some sub's are functions as boolean now.

added:
Boolean as params instead of integer.
Byval for pendantic compile.

Joshy
D.J.Peters
Posts: 8616
Joined: May 28, 2005 3:28
Contact:

Re: Tip: Event driven RS232 class (windows)

Post by D.J.Peters »

new version:
added:
optional ErrorEvent(msg as string)

changed:
before the device are used a new config the default config from device mannger are used as a base.
the param baudrate are checked for legal settings
If something goes wrong (device is busy or what ever) a optional user defined error callback are triggered.

Joshy
Last edited by D.J.Peters on Dec 31, 2016 20:50, edited 1 time in total.
Kot
Posts: 336
Joined: Dec 28, 2006 10:34

Re: Tip: Event driven RS232 class (windows)

Post by Kot »

When I try to communicate with an Arduino board, I don't receive a string as a string, but a series of separate characters. The arduino code (one of the sample programs) is supposed to sent back "I received: " and then incoming byte, here it's 4. Every character received fires separate ReceiveCB event and it looks like this:

? 4
4...
?
device send event 1 bytes.

1
I
1

1
r
1
e
1
c
1
e
1
i
1
v
1
e
1
d
1
:
1

1
5
1
2
1

1

How can I make it receive strings like EZTerm does? Do I have to add my own procedure putting characters into a string together or I'm doing something wrong?
D.J.Peters
Posts: 8616
Joined: May 28, 2005 3:28
Contact:

Re: Tip: Event driven RS232 class (windows)

Post by D.J.Peters »

Code: Select all

' NOTE: On all Windows OS's the serial device works in binary mode only.
' So if you waiting for a complete string you have to collect all
' incomming data until end of string chr(0) or chr(10) !"\n" are received.
sub ReceiveStringCB(byval buffer as ubyte ptr, byval size as integer)
  static as string msg
  dim as boolean StringComplete
  dim as integer n
  while n<size
    ' it's not a printable char
    if buffer[n]<31 then
      StringComplete=true 
      exit while
    else
     ' copy chars from buffer to string
      msg &= chr(buffer[n])
    end if
    n+=1
  wend
  if StringComplete then
    print ">" & msg
    msg=""
  end if
end sub
Kot
Posts: 336
Joined: Dec 28, 2006 10:34

Re: Tip: Event driven RS232 class (windows)

Post by Kot »

Thanks, works great!
ike
Posts: 387
Joined: Jan 17, 2011 18:59

Re: Tip: Event driven RS232 class (windows)

Post by ike »

I am using this class to send Gcode to CNC machine, machine can store 50 commands into buffer. After executig commad machine send me code
info "I am done", or "I am still working my position is x= y= z=" In the mean time program is sending new commands to buffer, but if machine is bussy some commands are jammed and I get 30% of gcode not executed

I've try many things and then I have add in your code this 3 lines:

function RS232.Send(byval pBuffer as any ptr,byval BufferSize as integer) as boolean
if IsTXActive = 1 then
return false
end if


and everything is working well now. I try a lot gcode - always OK
softfoot
Posts: 54
Joined: Aug 31, 2020 3:45

Re: Tip: Event driven RS232 class (windows)

Post by softfoot »

Whew - rather an old post to re-awaken ... but it looks like it could be exactly what I need ;-)

How would I add handling an event of RI (Ring Indication) changing state ???

Best regards,
Dave
D.J.Peters
Posts: 8616
Joined: May 28, 2005 3:28
Contact:

Re: Tip: Event driven RS232 class (windows)

Post by D.J.Peters »

softfoot wrote: Oct 10, 2024 16:42How would I add handling an event of RI (Ring Indication) changing state ???
The RS232 class is primary the data layer, receive and transmit data over the serial cables (TX/RX).
The Ring idicator is a part of the MODEM control layer and not a part of this event driven RS232 class SORRY :-(

BUT :-) you can use the the WIN32 API function "GetCommModemStatus()" https://learn.microsoft.com/en-us/windo ... odemstatus
If MS_RING_ON is set (&H0040) then you know the RI line is active and start a RS232 session if needed.

I can't test it because I don't have a modem this days !

Joshy
Last edited by D.J.Peters on Oct 11, 2024 19:53, edited 2 times in total.
D.J.Peters
Posts: 8616
Joined: May 28, 2005 3:28
Contact:

Re: Tip: Event driven RS232 class (windows)

Post by D.J.Peters »

@softfoot
in file "RS232.bi" after the line "declare function EnableRTS(byval state as boolean) as boolean"
add:

Code: Select all

declare function IsRinging() as boolean
in file "RS232.bas" before the line "sub RS232.Run()"
add:

Code: Select all

function RS232.IsRinging() as boolean
  if hDevice=INVALID_HANDLE_VALUE then
    if (EventError<>0) then EventError("IsRinging() failed hDevice=INVALID_HANDLE_VALUE !") 
    return false
  endif
  dim as DWORD flags
  if GetCommModemStatus(hDevice,@flags)=0 then 
    if (EventError<>0) then EventError("IsRinging() failed GetCommModemStatus()=0 !") 
    return false
  endif  
  return iif((flags and &H40)=&H40,true,false)
end function
Test it and let me know does it works for you.

Joshy

Code: Select all

' test02.bas
#include "RS232.bas"

sub OpenCB()
  ? : ? "device open event" : ?
  print
end sub

sub CloseCB()
  ? : ? "device close event" : ?
end sub

sub ErrorCB(msg as string)
  ? : ? "ups: an error " & msg : ?
end sub

sub SendCB(byval Size as integer)
  ? : ? "device send event " & size & " bytes." : ?
end sub

sub ReceiveCB(byval buffer as ubyte ptr, byval size as integer)
  static as string msg
  dim as integer n
  while n<size
    if buffer[n]=10 then
      print "received: " & msg : msg=""
      exit while
    else
      msg &=chr(buffer[n])
    end if
    n+=1
  wend
end sub


dim as RS232 Com
' optional set your callback's
Com.EventOpen    = @OpenCB
Com.EventClose   = @CloseCB
Com.EventError   = @ErrorCB
Com.EventSend    = @SendCB'
Com.EventReceive = @ReceiveCB

' !!! SET THE RIGHT ARGUMENTS !!!
' open COM1 with 9600 bps
if Com.Open(1,9600)=false then
  print "error: can't open RS232 !"
  beep : sleep : end 
end if

dim as string txt="hello RS232"
Com.Send(strptr(txt),len(txt))

while inkey=""
  print "IsRinging() = " & Com.IsRinging()
  sleep 1000  
wend
Com.Close
print "ok ..."
sleep
softfoot
Posts: 54
Joined: Aug 31, 2020 3:45

Re: Tip: Event driven RS232 class (windows)

Post by softfoot »

Many thanks for that!
Looks simpler than I thought ;-)
Dave
softfoot
Posts: 54
Joined: Aug 31, 2020 3:45

Re: Tip: Event driven RS232 class (windows)

Post by softfoot »

That worked niceley !!! Thank you very much.
Dave
Post Reply