Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - electrifiedspam

#1
Johu,



Maybe I am over thinking it but would:



(define (hexs n (w 4)) (format (string "%0" w "X") n))



be better?



Thank you for the example.
#2
Well,



That was more concise. I knew someone could do it. Thank you for sharing. The second part makes me feel better, because evidently there isn't a way to print hexadecimal.



Thank you for the response!

Chris
#3
(define (to-hex number)

(set 'answer '())

   (while (> number 0)

      (cond ((> number 0) (set 'answer (append (list (hex-let (mod number 16))) answer)) (set 'number (/ number 16)))

         ((= number 0) (set 'answer (append (list (hex-let (mod number 16))) answer)))

      )

   )

   (join (map string answer))

)



(define (hex-let num)

      

      (cond ((= num 10) (set 'num "A"))

         ((= num 11) (set 'num "B"))

         ((= num 12) (set 'num "C"))

         ((= num 13) (set 'num "D"))

         ((= num 14) (set 'num "E"))

         ((= num 15) (set 'num "F"))

      )

      num

)

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

I couldn't find a ready way to do this in the language, not that there isn't one. But there comes a time when it is just easier to roll your own rather than search. This returns a string that represents the hexadecimal value of the decimal number you put into it.



I was trying to read error messages coming back to me from the PLC. (map char (explode buff)) helpfully converted all of the values to decimal which while nice, made it difficult to compare to the hexadecimal message I had sent.



I don't think that you really NEED this for production code, but it sure was nice for trouble shooting.



Again, I am sure that there is a better way, but this was fun.



Chris
#4
newLISP in the real world / Re: Modbus CRC
January 02, 2011, 12:50:26 PM
I chose a string of 1's and 0's simply so I would know what was going on and it was easy to print out all of the shifts and register manipulations so I could debug easier.



granted that (^ 0xFFFF 0x11) turns out the same result as all of the cruft I had to go through but I went for a version that was readable.



I have not done any benchmarking on this so it may in fact BE unusable for higher baud rates.



Thank you all

Chris
#5
newLISP in the real world / Modbus CRC
January 02, 2011, 12:45:03 PM
I figured out how to do it. I thought I would share. Mind you, this is the gross version so there is much optimization left to do, my goal was to get something that did the trick. Now I need to make it cleaner, but I thought that I would at least share.

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

;note this spits out the decimal of the crc. It needs converted to hexadecimal.

;0x11 0x05 0x00 0xAC 0xFF 0x00 4E8B (crc appended to the message) Sample message to test against. 0xA001 is the crc constant for modbus.

(context 'CRC)



(constant 'crcConst (bits 0xA001))



(define (crcModbus message)

   (set 'crcReg "1111111111111111")

      (while (> (length message) 0)

         (set 'crcReg (XOR crcReg (bits (pop message))))

         (dotimes (x 8) (set 'crcReg (shiftXOR crcReg)))   

      )

      (println (int crcReg 0 2))

)



(define(lsb_is_1 num)

   (if (= (last num) "1") true)

)



(define (XOR numA numB)

   (if (< (length numA) 16) (dotimes (x (- 16 (length numA))) (push "0" numA)))

   (if (< (length numB) 16) (dotimes (x (- 16 (length numB))) (push "0" numB)))

   (set 'numA (reverse numA) 'numB (reverse numB))

   (join (map string (reverse (map (fn (y z) (^ (int y) (int z))) (explode numA) (explode numB)))))

)



(define (rshift numC)

   (set 'numc (chop numC 1))

   (set 'numc (join (push "0" (explode numc))))

   numc

)



(define (shiftXOR numD)

   (if (lsb_is_1 numD) (set 'res (XOR (rshift numD) (bits 0xA001))) (set 'res (rshift numD)))

   (println res)

   res

)



(context 'MAIN)
#6
newLISP in the real world / Re: Help with RS-232 comms
December 28, 2010, 09:11:43 AM
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.
#7
newLISP in the real world / Help with RS-232 comms
December 27, 2010, 10:02:39 PM
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.
#8
newLISP and the O.S. / Re: (+ newLISP Windows RS232)
December 24, 2010, 09:34:48 AM
I think I found the problem.



The 'sec' in the CreateFile attribute was allowed to be null so I packed it as a null character like this:



(constant 'sec (pack "nn" nill))



Below is the code that detected COM1.



(constant 'FILE_SHARE_READ 0x00000001)

(constant 'FILE_SHARE_WRITE 0x00000002)   

(constant 'OPEN_EXISTING 3)



(constant 'sec (pack "nn" nill))



(constant 'FILE_ATTRIBUTE_NORMAL 0x80)

(import "kernel32.dll" "CreateFileA")

(import "kernel32.dll" "CloseHandle")

(setq test1 (CreateFileA "COM1" 0 (+ FILE_SHARE_WRITE FILE_SHARE_READ) sec OPEN_EXISTING FILE_ATTRIBUTE_NORMAL 0))

(cond ((> test1 -1) (println "COM1 is present " test1) (CloseHandle test1))

         (true (println "sorry Charley"))

)



This could easily be modified to detect all available com ports. I hope this is of help to someone. Now I am on to actually reading from the comport.
#9
newLISP and the O.S. / Re: (+ newLISP Windows RS232)
December 23, 2010, 08:49:36 PM
My ultimate goal is to write a modbus module for newlisp.

My immeadiate goal is to find the comport. I know that I have com1, yet I can't find it.



I found "this" code:

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

Return TRUE if the COM exists, FALSE if the COM does not exist

Public Function COMAvailable(COMNum As Integer) As Boolean

    Dim hCOM As Long

    Dim ret As Long

    Dim sec As SECURITY_ATTRIBUTES



    'try to open the COM port

    hCOM = CreateFile("COM" & COMNum & "", 0, FILE_SHARE_READ + FILE_SHARE_WRITE, sec, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)

    If hCOM = -1 Then

        COMAvailable = False

    Else

        COMAvailable = True

        'close the COM port

        ret = CloseHandle(hCOM)

    End If

End Function

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

Which is C and I tried to convert it to this code:



(define FILE_SHARE_READ 0x00000001)

(define FILE_SHARE_WRITE 0x00000002)

(define OPEN_EXISTING 3)

(define FILE_ATTRIBUTE_NORMAL 0x80)

(import "kernel32.dll" "CreateFileA")

(import "kernel32.dll" "CloseHandle")

(setq test1 (CreateFileA "COM1" 0 (+ FILE_SHARE_READ FILE_SHARE_WRITE) sec OPEN_EXISTING FILE_ATTRIBUTE_NORMAL 0))

(if (> test1 -1) ((println "Who's you daddy " test1) (CloseHandle test1)))



Which always returns a -1 indicating that com1 does not exist.



This leads me to believe that I am not packing my variables right. Any pointers?
#10
newLISP and the O.S. / Re: Problems installing on Vista
December 07, 2010, 08:09:11 PM
OK



I read up on 'or'.



According to what I read, it returns the first thing that is not nil. Emacs was already using the home variable and the others were set to other things left over from projects past...



I edited the newlisp-edit.lsp and had it check the DOCUMENT_ROOT variable first. I set that variable to C:Program Filesnewlisp and it seems to work.



IF there are better fixes, please let me know, but this has me up and running for now.



Thank you!!



PS



I made a math program for my son that tests his multiplication skills for all numbers up to 13. My goal for him was to either learn is multiplication OR reprogram the computer to say that he got it all right.



Either way we both win.
#11
newLISP and the O.S. / Re: Problems installing on Vista
December 07, 2010, 06:59:53 PM
What should



(env "NEWLISPDIR") return?



I get



"C:\Program Files\newlisp" which is where my directory is installed.



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



(set 'userSettingsDir (string

         (or (env "APPDATA") (env "HOME") (env "USERPROFILE") (env "DOCUMENT_ROOT")) "/newLISP"))



I am assuming that these should return 'C:Program Filesnewlisp' I have installed Emacs on this machine and it is utilizing the 'HOME' variable. I guess the 'or' means that as long as one environment variable returns the newlisp directory, then it ignores the others?
#12
newLISP and the O.S. / Re: Problems installing on Vista
December 07, 2010, 06:53:44 PM
When I run 'newlisp-edit' from the command line I get:



"Cannot find newlisp. Make sure that you typed the name correctly and try again."



Furthermore when I type in 'newlisp' into the terminal I get the standard "Newlisp is not recognized as a batch command...."
#13
newLISP and the O.S. / Re: Problems installing on Vista
December 06, 2010, 08:19:22 PM
Rebooting didn't help.
#14
newLISP and the O.S. / Re: Problems installing on Vista
December 06, 2010, 08:19:21 PM
Rebooting didn't help.
#15
newLISP and the O.S. / Re: Problems installing on Vista
December 06, 2010, 07:23:59 PM
Well,

There was a C:temp



AND I have changed the TEMP system and user variable to C:temp. So far all to no avail. I will try a reboot but I am doubting that it will bring any changes.