Help with RS-232 comms

Started by electrifiedspam, December 27, 2010, 10:02:39 PM

Previous topic - Next topic

electrifiedspam

When running the following code:



; Initialization

(define (com-init)

  (exec "mode COM1 BAUD=9600 PARITY=O DATA=8 STOP=1"))



; Getting data from COM-port

(define (start-listening)

  (local (hndl buff)

    (set 'hndl (open "COM1" "r"))

    (read hndl buff 40 (char 0x0D))

    (close hndl)

    (int (-12 5 buff))))



(com-init)



(if (catch (start-listening) 'result)

  (println "Message: " result)

   (println "no link"))

--------------------------------------------------

My message returns nil.



I assume that it is looking for printable characters, but how can I capture the hex of the message.



My ultimate goal is to write a Modbus module for newlisp. That would allow me to use it at work as most of the equipment we use supports Modbus.

electrifiedspam

#1
Please forgive me, I am so new and constantly over my head.



I see that the result symbol is only capturing true and not the message.

Lutz

#2
no, no, the 'result variable actually is receivng the return value of the expression and the whole 'catch' expression returns true.



> (catch (+ 2 3) 'result)
true
> result
5
>


what probably is happening in your case is, that (int (-12 5 buff)) returning nil because the contents of (-12 5 buff) is not a number. If buff contains 0 characters it will stop printing at that place. You could insert a (print (-12 5 buff)) to see the contents. Or do a (print (map char (explode (-12 5 buff)))) to see what's in that piece of buff. Or inspect the entire buffer with (println (map char (explode buff)))