This library allows RS232 from LUA scripts.
https://github.com/ynezz/librs232/downloads
It supports Compots COM1 to COM9. Version 1.0.1 supports upto COM255, but source needs to be built using VC express 2005.
It is possible to rebuild it.
Example.
http://www.dougrice.plus.com/dev/exampleSolarLogger.lua
using robot-electronics's kit:
http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm
http://www.dougrice.plus.com/dev/exampleIIC.lua http://www.dougrice.plus.com/dev/exampleIIC_atod.lua http://www.dougrice.plus.com/dev/exampleIICdiaplay.lua
++ Example for C
http://www.robot-electronics.co.uk/acatalog/examples.html has some code for Linux GCC serial ports, worthy of a read. http://www.robot-electronics.co.uk/htm/linux_examples.htm
rpi iic srf08.c is an example used on the raspberry pi - Ubuntu
C++ Example for Bloodshed C++ http://www.phanderson.com/tm128/dev-cpp_tm128_1.html
http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm
Robot sell some usefule modules see: http://www.robot-electronics.co.uk/acatalog/USB_I2C.html
I have combined the LINUX and Windows TCC versions to drive a MAX2719 behind an ISS. It complies using GCC on LINUX and TCC on Windows.
TCC is found at http://bellard.org/tcc/
My demo code, ( under construction ): http://www.dougrice.plus.com/dev/rpi_ser_tcc.c
Assuming buffered RS232, you send commands, wait for text to come back.
If you do a read with time out, you can wait for the text to dry up. You have to buffer the received text until you have enough to process.
It is not possible to supply a string to look for. You have to write this.
-- flush i/P buffer read( number of bytes wanted, timeout ) -- if timed out assume no new text write() wait() read()
repeat reads until enough text or timeout
process()
This page talks about the issues around using the serial port on the PC.
You open the serial port as a file.
Assuming you have a device that sends RS232 when asked.
You have to receive characters by periodically by reading the file attached to the COM port.
In an event driven application, you poll for events, queue them and when you get an event, process it.
This means that you need to poll and buffer. The buffered information needs to be analysed and converted into events.
The easiest is to send line by line and send an event at the end of the line.
It cand be summarised as:
wait for start of start bit delay 1.5 bits sample LSB loop delay 1 bit sample next bit repeat this until we get to enough bits
; process in stop bit.
In the stop bit we have time to process the sample and get ready for the next byte.
This works well enough.
However, could we do something in the bit delay.
Could we look for a 1 to 0 transition or 0 to 1 transition and phase lock loop this to adjust the delay timeout?
During the delay sample the input and totalise when it s 0 and when it it 1. We measure the PWM.
Decide if we have had a 1 to 0 and 0 to 1 transition.
count 0's
count 1's
now classify:
if all 1's ignore as no edge. if all 0's ignore as no edge.
if _- and 0' < 1's then adjust up. if _- and 0' > 1's then adjust down.
if -_ and 0' < 1's then adjust down. if -_ and 0' > 1's then adjust up.
if 1's !=0's then adjust timing.
---------|-----------|----------|-----------|--------- 0's 0's>1's 0's==1's 0's<1's 1's ignore adjust locked adjust ignore ---------|-----------|----------|-----------|---------
Where it says adjust, what do we do? Consider this:
-------------|------------- expected -------------|------------- early: ----------_________________ __________----------------- -------------|------------- late: -------------------________ ___________________-------- -------------|-------------This needs further work. We could toggle + or - ve adjustments until lock acheived.
delay look for edge if found goto very early look for edge if found goto early look for edge if found goto okay look for edge if found goto late look for edge if found goto very late
RS232in ; spin for a start bit ; CLRWDT ; BTFSS PORTB,RS232rx ; GOTO RS232in CALL Delay6 ; 1/2 bit delay for Start bit - check with MPLAB CALL Delay5 ; bit delay movlw 8 movwf RSloopCnt RS232inL0 MOVFW PORTB ANDLW 1<< RS232rx ; mask off bit ADDLW -1<< RS232rx ; use ripple carry to move into C RRF RSdata,F CALL Delay5 DECFSZ RSloopCnt,f goto RS232inL0 COMF RSdata,f MOVFW RSdata ADDLW -0x0D SKPNZ GOTO RSfoundCR ADDLW 0x0D CALL LCDwrtChar RETURN RSfoundCR MOVLW LCDCurLeftLower CALL LCDwrtCmd RETURN Delay5 MOVLW 0x1E MOVWF RSdell Delay51 DECFSZ RSdell ; 1 GOTO Delay51 ; 2 clk CLRWDT RETURN ;Delay 0x0F * 3 ticks of 4Mhz ~= 45us Delay6 ; RETURN ; MOVLW 0x0F MOVLW 0x08; shorten delay as edge detector takes about 22 us MOVWF RSdell Delay61 DECFSZ RSdell GOTO Delay61 CLRWDT RETURN