Announcement

Collapse
No announcement yet.

RS232 Interfacing

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • RS232 Interfacing

    Hi Guys
    I have just built an RFID reader/writer that interfaces to a PC via a serial port, either UART based or USB based.
    The customers requirements included a pushbutton on the device that the application could read in order to start the RFID Tag reading process.
    This is necessary in this case because the RFID reader is serially driven and is too dumb to read tags all by itself, so the PC has to tell it what to do.
    Anyway the button is tied to the CTS line and consequently I had to work out how to make the application respond to it seemingly instantly.
    After searching through POFFS and the net a solution has presented itself that works quite well.
    I know that many PB users have been chasing solutions to these specific problems, so I have posted code fragments below.

    I can also provide circuit diagrams and details of wiring stuff up to the serial port in order to test it out.

    To flesh it out a little more, the USB device that I used for this application is an FTDI TTL232 interface and cable. The FTDI device has TX,RX,CTS and RTS lines available and is dead easy to use at a cost of AU$25.00 approximately. The drivers supplied implement a Virtual Serial port that behaves in the same way as a hardware UART.

    These techniques also apply to Bluetooth serial and anything else that can successfully pretend to be a standard com port.


    ' The functions below require that the file number of the open com port and the host dialog box handle are global.
    ' hcomm is the windows file number found by passing the com port file number to the file attribute function.

    ' Function to return current status of CTS line
    FUNCTION CTSTRUE() AS LONG
    LOCAL mstat AS LONG
    IF GetCommModemStatus(hcomm,mstat) THEN
    IF (mstat AND %MS_CTS_ON) THEN FUNCTION = %TRUE
    END IF
    END FUNCTION

    ' Thread created in initialisation routine to update the status of the Pushbutton pressed flag.

    FUNCTION BPressed(BYVAL x AS DWORD) AS LONG
    DO
    IF ISFALSE ctsstat THEN ' If the flag is not set then
    IF ISTRUE ctstrue THEN ' If CTS is asserted
    ctsstat = %TRUE ' Set Flag
    PostMessage mdlg, %WM_USER + 2, 0, 0 ' In this case I am posting a message into the queue for processing in the main dialog loop.
    END IF
    END IF
    SLEEP 50 ' Checking the button 20 times per second seems to be fast enough to make the response seem instant.
    LOOP
    END FUNCTION


    ' This thread should be started after the COM port has been successfully opened.
    THREAD CREATE bpressed(0) TO response

    ' The message is processed in the dialog loop, in this case two buttons are pressed. I am using CONTROL SEND in this case as interrogating the RFID reader takes a long enough time to avoid bothering to check that the user has released the pushbutton and SEND forces the application to wait until the message has been processed.

    CASE %WM_USER+2
    CONTROL SEND CBHNDL,%IDC_CLEARDATA, %BM_CLICK,0,0
    CONTROL SEND CBHNDL,%IDC_READ, %BM_CLICK,0,0
    CTSSTAT = %FALSE

    ' Basically the thread continually updates the "Button Pressed" flag every fifty milliseconds. You can do what you need to do with the flag, or modify it to suit your application.

    Thanks to the other forum users whose posts pointed me in the right direction.

    Of course, it goes without saying that any improvements of this code will be gratefully received.

    Best Regards
    Gary Barnes
    The Control Key

    If you are not part of the solution
    then you are either a gas, solid, plasma or some other form of matter.
Working...
X