I want to know if there is a way to increase the speed of PureBasic serial port (RS-232) communications. Although my application is pretty specific there may well be other uses involving computer to microprocessor serial communications where speed is important.
My project is to construct what I call the 'software defined antenna analyzer' for amateur radio. I am still assembling and testing the components which will include:
- microcomputer running a PureBasic program for sweeping frequencies and reporting in various ways on voltage measurements taken at an antenna; in this case the microcomputer is a netbook
- direct digital synthesizer oscillator and low-power RF amplifier (a DDS-60 module using an AD9851 DDS chip) to transmit to an antenna
- SWR bridge (resistors, capacitors and diodes) to provide forward and reflected antenna voltages at given frequencies
- PICAXE 14M2 microprocessor to:
. receive frequency control data from a microcomputer and set up the DDS chip to produce the frequency
. measure voltages (there are three of them) for the frequency at the SWR bridge and send the measurements back to the microcomputer.
I am at the stage of this project where the microprocessor is receiving frequency control data from the microcomputer. The microprocessor is telling the DDS to produce the correct frequency, and is reporting back voltages to the microcomputer (just dummy test voltages for now, the SWR bridge has yet to be added). No more than 32 characters are exchanged each way. Communication between the microprocessor and the microcomputer is by serial port via a RS232 to USB adaptor plugged into one of the the microcomputer's USB ports. To test this arrangement I've kludged together a console program to sweep 30 frequencies (1 mHz to 30 mHz) and display the corresponding voltages in the console window.
My problem now is that the turn-around time for communication between the microcomputer and microprocessor is slow. I am hoping to achieve a sweep rate of at least 100 frequencies per second (in theory the DDS can handle the rapid frequency updates), but the time between the microcomputer sending frequency control data to the microprocessor and receiving the voltage measurements back is too long. In order to make the program work reliably (i.e. receive voltages, if at all, corresponding to the frequency) I have to put in a delay of about 90 milliseconds between sending frequency control data request and receiving voltage measurements; this implies a sweep rate of about 10 frequencies per second. The program delay statement can be seen in the accompanying snippet the PureBasic test code, showing serial communication and timer elements.
According to the "ElapsedMilliseconds()" timer it appears that the microprocessor uses only a few milliseconds of the total time between the microcomputer sending the frequency control data and receiving the voltage values in return. The rest of the time for the transaction is consumed by the delay statement. (I doubt the exact accuracy of the "ElapsedMilliseconds()" timer, however, because sometimes the timer duration is less than that of the delay statement.)
I've tried a number of things to speed up communication turnaround time, consistent with reliable measurement reporting. These include:
- cranking up the microprocessor speed to 32 MHz and serial port speed to 38,400 baud; this helped. Speeding up the microprocessor and serial port usually required shorter delay times. (At 8 mHz and 9600 baud the delay is approx. 300 ms, at 38,400 baud it is 90 ms.)
- playing with values of *Buffer and the Open statement buffers; this made no difference
- using shorter delay times in the PureBasic program; there is a fairly critical point where making the delay time shorter screws around with properly receiving voltage measurements
- change the program to poll AvailableSerialPortInput() until some characters are received then transferring them to the *buffer ( ReadSerialPortData(0,*Buffer,port_data) ), but for some reason this did not work very well (I may not have known what I was doing)
If you have experience with PureBasic serial port functioning can you advise me on how to speed it up? Or can it be speeded up?
I will be writing a windows-based PureBasic user control and reporting program. Does serial port communications work faster or slower when not in console mode?
I appreciate any help.
--- Andy
Code: Select all
Program Snippet Below:
;Using Purebasic 4.61 (x86) on a netbook computer, communicating with a PICAXE 14m2 over RS232 port via a
;serial to USB adapter.
; Dots ('.') mean code segments have been removed to highlight serial com. and timer elements.
OpenConsole()
.
.
*Buffer=AllocateMemory(512)
.
.
.
;Open the RS232 Serial Port
If OpenSerialPort(0,"com"+comport$,38400,#PB_SerialPort_NoParity,8,1,#PB_SerialPort_NoHandshake,256,256)
Debug "Com"+comport$ + " is open"
Else
Debug "Com"+comport$ + " Failed"
End
EndIf
.
.
.
;Send frequency to PICAXE
for n=1 to 30
.
.
StartTime = ElapsedMilliseconds()
WriteSerialPortString(0, output_string$ ,PB_Ascii)
Delay (90)
volts$=""
port_data=AvailableSerialPortInput(0)
ReadSerialPortData(0,*Buffer,port_data)
text_in$=PeekS(*Buffer,port_data)
volts$=text_in$
ElapsedTime = ElapsedMilliseconds()-StartTime
PrintN (Str(n)+" volts "+volts$ +"Timer ms "+Str(ElapsedTime))
Next n