(+ newLISP Windows RS232)

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?
Locked
Fritz
Posts: 66
Joined: Sun Sep 27, 2009 12:08 am
Location: Russia

(+ newLISP Windows RS232)

Post 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)?

itistoday
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Re: (+ newLISP Windows RS232)

Post by itistoday »

Couldn't you use the "import" function to import the necessary C functions from some windows DLL?
Get your Objective newLISP groove on.

Fritz
Posts: 66
Joined: Sun Sep 27, 2009 12:08 am
Location: Russia

Re: (+ newLISP Windows RS232)

Post 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.

Robert Gorenc
Posts: 12
Joined: Tue Oct 27, 2009 9:01 pm
Location: Croatia

Re: (+ newLISP Windows RS232)

Post 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....

Fritz
Posts: 66
Joined: Sun Sep 27, 2009 12:08 am
Location: Russia

Re: (+ newLISP Windows RS232)

Post 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."))

Robert Gorenc
Posts: 12
Joined: Tue Oct 27, 2009 9:01 pm
Location: Croatia

Re: (+ newLISP Windows RS232)

Post 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)

electrifiedspam
Posts: 27
Joined: Sat Nov 06, 2010 11:39 pm

Re: (+ newLISP Windows RS232)

Post 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?

electrifiedspam
Posts: 27
Joined: Sat Nov 06, 2010 11:39 pm

Re: (+ newLISP Windows RS232)

Post 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.

Locked