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

Postby midnightrambler905 » Aug 11, 2009 16:07

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

Postby midnightrambler905 » May 28, 2010 2:11

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: 7165
Joined: May 28, 2005 3:28
Location: Germany

Re: Tip: Event driven RS232 class (windows)

Postby D.J.Peters » Feb 27, 2016 15:27

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: 7165
Joined: May 28, 2005 3:28
Location: Germany

Re: Tip: Event driven RS232 class (windows)

Postby D.J.Peters » Mar 03, 2016 18:28

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: 331
Joined: Dec 28, 2006 10:34

Re: Tip: Event driven RS232 class (windows)

Postby Kot » Dec 31, 2016 16:08

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: 7165
Joined: May 28, 2005 3:28
Location: Germany

Re: Tip: Event driven RS232 class (windows)

Postby D.J.Peters » Dec 31, 2016 18:50

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: 331
Joined: Dec 28, 2006 10:34

Re: Tip: Event driven RS232 class (windows)

Postby Kot » Dec 31, 2016 20:14

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

Re: Tip: Event driven RS232 class (windows)

Postby ike » Apr 08, 2017 18:13

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

Return to “Hardware Interfaces / Communication”

Who is online

Users browsing this forum: No registered users and 1 guest