Page 1 of 1

(+ newLISP Windows RS232)

Posted: Sun Mar 07, 2010 6:47 pm
by Fritz
I need to communicate with standart rs-232 serial port on PC.

Is it possible to write on newLISP something like (read-port "COM1" 20)?

Re: (+ newLISP Windows RS232)

Posted: Tue Mar 09, 2010 1:40 am
by itistoday
Couldn't you use the "import" function to import the necessary C functions from some windows DLL?

Re: (+ newLISP Windows RS232)

Posted: Tue Mar 09, 2010 4:27 am
by Fritz
itistoday wrote:Couldn't you use the "import" function to import the necessary C functions from some windows DLL?
Well, I’ll try. I’m not too familiar with C, but it is a good reason to learn it closer.

Re: (+ newLISP Windows RS232)

Posted: Fri May 28, 2010 8:53 am
by Robert Gorenc
Hi Fritz,
do you have same success in writing to serial port?
I also would like to talk to uC with forth on board via serial port from Linux.
I didn't jet try, maybe this weekend....

Re: (+ newLISP Windows RS232)

Posted: Fri May 28, 2010 9:42 am
by Fritz
Robert Gorenc wrote:do you have same success in writing to serial port?
Yep. Here is a part of working code (Windows PC):

Code: Select all

; Initialization
(define (scales-init)
  (exec "mode COM1 BAUD=9600 PARITY=N DATA=8 STOP=1"))

; Getting data from COM-port
(define (scales-weight-up)
  (local (hndl buff)
    (set 'hndl (open "COM1" "r"))
    (read hndl buff 40 (char 0x0D))
    (close hndl)
    (int (-12 5 buff))))

(scales-init)

; Measuring
(if (catch (scales-weight-up) 'result)
  (println "Weight: " result)
  (println "No link."))

Re: (+ newLISP Windows RS232)

Posted: Fri May 28, 2010 10:47 am
by Robert Gorenc
thanx,
something similar was my idea, but I need read and write access, so my doubt is open two pipes, one for reading and one for writing, or one with r/w......
something like:
(set 'hin (open "/dev/ttyUSB0" "r"))
(read hin buff 40)
(close hin)
and
(set 'hout (open "/dev/ttyUSB0" "w"))
(write-buffer hin buff )
(close hout)

or
(set 'hndl (open "/dev/ttyUSB0" "u"))
(write-buffer hndl buff)
(read hndl buff 40)
(close hndl))

I will try what is better working for me.
(exit)

Re: (+ newLISP Windows RS232)

Posted: Fri Dec 24, 2010 4:49 am
by electrifiedspam
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?

Re: (+ newLISP Windows RS232)

Posted: Fri Dec 24, 2010 5:34 pm
by electrifiedspam
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.