Re: new libraries to access serial ports
- Posted by Fernando Oct 19, 2010
- 2581 views
Your library works well for me, except for one thing: If I try to read the input stream when there are no bytes in the buffer, the whole computer locks up waiting for data.
How can I test to see if the input buffer is empty or not?
There must be a way to do it, as every terminal program waits for input and displays it whenever it comes without locking up. Can I hook into the serial port interrupt chain?
Hi! lpuster,
Both libraries, the windows version and the linux version, have provision for timing out. I didn't used that code for over 1 year so I don't remember the details, but I'm sure you can read the source code as well as me.
regards, Jacques d.
Hi,
Thanks Jacques for your library. I have been using it with success to test some devices.
Maybe this could help (tested in Windows XP):
include serial.ew include get.e include misc.e object val integer port, a atom hCom1 constant MAXDWORD=#FFFFFFFF constant ESC=27 port = 1 -- ADJUST TO YOUR PORT hCom1 = serial_open(port) if hCom1 < 0 then printf(1,"Error opening port %d.\n\n",port) abort(1) end if -- http://msdn.microsoft.com/en-us/library/aa363190%28v=VS.85%29.aspx -- A value of MAXDWORD, combined with zero values for both the ReadTotalTimeoutConstant and ReadTotalTimeoutMultiplier members, -- specifies that the read operation is to return immediately with the bytes that have already been received, -- even if no bytes have been received. SetCommTimeouts(hCom1,{MAXDWORD,0,0,0,0}) SetCommState(hCom1, "baud:9600;parity:none;bits:8;stop:1;control:none") puts(1,"1) Connect TX and RX (loopback) (pins 2 and 3 in the DB9 connector).\n" & "2) Every key you hit will be send and received by serial port and echoed to the screen.\n" & "3) ESC abort.\n") PurgeComm(hCom1,PURGE_RXCLEAR) while 1 do a = get_key() if a != -1 then if a = ESC then puts(1,"\nAborted!\n") abort(0) end if serial_puts(hCom1, a) -- SEND KEY CODE TO SERIAL PORT end if val = serial_getc(hCom1) if val != -1 then puts(1,val) end if end while
P.S.: I had to define MAXDWORD since it's not defined as global in serial.ew.
Regards,
Fernando