April 19, 2024, 07:09:50 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Parallel Port Programming

Started by Trevor, October 24, 2008, 04:45:13 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Trevor

Is it possible to read from and write to the parallel port with CBASIC? I have done it with FreeBASIC but I am not sure if it is possible with CBASIC. I guess a DLL would need to be used as CB has no inbuilt commands of the type. How would one go about doing this?

sapero

Hello, there is no problem:
declare "!msvcrt.dll", _inp(port:int),char
declare "!msvcrt.dll", _outp(port:int, byte:int)
declare "kernel32", Sleep(time:int)
declare "!crtdll", _system alias system(command:string)

openconsole()

' on NT systems we need a helper driver
' start the driver
_system("net start giveio")

' attach the driver to our process
def f:FILE
if (OpenFile(f, "\\.\giveio", "R") = 0) then CloseFile(f)

' play with the ports
for a=0 to 10

print "sending 11111111"
_outp(888, 255)
Sleep(500)

print "sending 00000000"
_outp(888, 0)
Sleep(500)
next a

aurelCB

Interesting but what is "giveio" ???

sapero

Giveio is a mini driver which tells to the kernel, that your process should be able to access I/O ports directly. You can google it, and even compile.

Trevor


sapero

Sure, on win98 you don't need drivers to access ports. I'm coding on XP, and this is why I added the code for driver.

Trevor


sapero

Inpout32 and other are just hidding the driver from end user. These dll's are created to make it easier, when you execute inp32() it does all (including driver instalation) to read the value from I/O port.
You can find in my code two lines where in the simplest way a driver is started and attached to current process. Your dll does the same, but you don't know about (unless you read the manual).

Trevor

Do you know if there is a manual for Inpout32.dll?

sapero


Techno

Hi

You can use the inpou32.dll for access all the kind of ports:

serial, parallel, timers

It works fine in Windows XP

Not tested under Vista but I hate Vista

Trevor

I've got Inpout32.dll working and I'm getting some wierd results. I have written to the output lines (port 888) and managed to then read that data. When I read the status lines (port 889) I am getting wierd results. The Inp32 function returns with 127, how can this be when there are only 5 bits available?

sapero

6 bits are connected to the LPT controller. 2 next bits can NOT be not-connected, so you have one connected to +5, one to ground.
The two unusedl bits are marked as Reserved, and you should ignore them.
value = int32(889) & 63

http://www.beyondlogic.org/spp/parallel.htm   Table 5 Status Port

Techno

November 01, 2008, 11:51:10 AM #13 Last Edit: November 01, 2008, 11:56:58 AM by Techno
Hello Trevor,

Here iis an example working in console mode. You have hardware need
It's the serial port RS232


$USE "inpout32.lib"
DECLARE IMPORT, Inp32(PortAddr : WORD), WORD
DECLARE IMPORT, Out32(PortAddr : WORD, PortData : WORD)
DECLARE EXTERN _sscanf(STRING buf, STRING format,...),INT
DECLARE EXTERN sprintf(POINTER p, POINTER p, ...), INT
DECLARE "kernel32", Sleep(time:int)

/* ======================================================================================*/
/* Base Port addresses                                     */
/* ======================================================================================*/
CONST BA_COM1  = 0x3F8
CONST BA_COM2 = 0x2F8
CONST BA_COM3        = 0x3E8
CONST BA_COM4        = 0x3E8
/*=======================================================================================*/

/*=======================================================================================*/
/* UART Registers        */
/*=======================================================================================*/
CONST IER = 0x01 /* Interrupt Enable Register */
CONST IIR = 0x02 /* Interrupt Identification Register */
CONST FCR = 0x02 /* FIFO Control Register */
CONST MCR = 0x03 /* Line Control Register */
CONST MCR = 0x04 /* Modem Control Register */
CONST LSR = 0x05 /* Line Status Register */
CONST MSR = 0x06 /* Modem Status Register */
/*=======================================================================================*/

/*=======================================================================================*/
/* UART Read inputs                           */
/*=======================================================================================*/
CONST DR = 0x01 /* Data Ready */
CONST OE = 0x02 /* Overrun Error */
CONST PE = 0x04 /* Parity Error */
CONST FE = 0x08 /* Framing Error */
CONST BI = 0x10 /* Break Interrupt */
CONST ET = 0x20 /* Empty Transmitter Holding Register */
CONST ED = 0x40 /* Empty Data Holding Registers */
CONST EF = 0x80 /* Error in Received FIFO */
CONST DCTS = 0x01 /* Delta Clear to Send */
CONST DDSR = 0x02 /* Delta Data Set Ready */
CONST TERI = 0x04 /* Trailing Edge Ring Indicator */
CONST DDCD = 0x08 /* Delta Data Carrier Detect */
CONST CTS = 0x10 /* Clear To Send */
CONST DSR = 0x20 /* Data Set Ready */
CONST RI = 0x40 /* Ring Indicator */
CONST DCD = 0x80 /* Carrier Detect */
/* ======================================================================================*/

/* ======================================================================================*/
/* UART Write Lines           */
/*=======================================================================================*/

CONST DTR                = 0x01 /* Data Terminal Ready */
CONST RTS                = 0x02 /* Request To Send */
CONST LPB = 0x10 /* Loopback mode */

/*========================================================================================*/

SUB Pulsgever()
Out32((0x43), 0x24) /* Timer 0, mode 2, lezen: highbyte */
Out32((0x40), 59659 &  0xFF) /* deelfactor */
Out32((0x40), 59659 >> 0x08) /* = 1193180 : 20 */
RETURN
ENDSUB

SUB Uur()
Out32((0x43), 0x36) /* Timer 0, mode 3 */
Out32((0x40), 0x00) /* deelfactor */
Out32((0x40), 0x00) /* = 65536 */
RETURN
ENDSUB

SUB Frequency(Channel AS CHAR), INT
DEF dummy    AS INT
DEF Timer_1 AS INT
DEF Timer_alt AS INT
DEF in AS CHAR
DEF in_alt AS CHAR
DEF tijd AS CHAR
DEF port_adr AS CHAR
DEF teller AS INT

port_adr = 0x10 /* CTS */
IF(Channel = 0x02) THEN in = 0x20 /* DSR */
IF(Channel = 0x03) THEN in = 0x40 /* RI  */
IF(Channel = 0x04) THEN in = 0x80 /* DCD */
teller = 0

'_ASM
' STI
'_ENDASM
Timer_1 = Inp32((0x40) & 0x80) /* hoogstwaardige bit */
DO
Timer_alt = Timer_1
Timer_1 = Inp32((0x40) & 0x80)
UNTIL(Timer_1 > Timer_alt)   /* 1e Timer overflow */
in_alt = Inp32((MSR) & port_adr)

DO
Timer_alt = Timer_1
in = Inp32((MSR) & port_adr) /* flanken tellen */
IF(in > in_alt) THEN teller ++
in_alt = in

Timer_1 = Inp32((0x40) & 0x80)   /* overflows tellen */
IF(Timer_1 > Timer_alt) THEN tijd++

UNTIL(tijd = 20)   /* 1s */
'_ASM
' CLI
'_ENDASM
RETURN(teller)
ENDSUB

OPENCONSOLE
CLS
Pulsgever()
DO
PRINT USING("Channel 1 : ###.##", Frequency(1), "Hz")
PRINT USING("Channel 2 : ###.##", Frequency(2), "Hz")
PRINT USING("Channel 3 : ###.##", Frequency(3), "Hz")
PRINT USING("Channel 4 : ###.##", Frequency(4), "Hz")
UNTIL INKEY$(1) <> ""
Uur()
END
CLOSECONSOLE


Sapero can you tested it on your PC and if there errors can you reported it to me

Thanks
Stephane

sapero

CONST MCR = 0x03
CONST MCR = 0x04


Have you even tried to compile this ;D ? I can't test it because I'm working on a laptop without old user ports.

Techno

The first line "CONST MCR = 0x03 is not correct !
The other line is correct

Stephane