reading the serial port.

For issues with communication ports, protocols, etc.
Post Reply
BrianKudsk
Posts: 24
Joined: Sep 08, 2010 20:27
Location: Florida

reading the serial port.

Post by BrianKudsk »

In Qb when using the serial port I would do a line like this
ON COM(1)
then do a com handler subroutine to get the data.
FB does not really have this, any ideas?
I am new to FB, but have alot of equipment running using com ports using the method I described talking to Transducers and motors and such.
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

You can run your Com handler in a seperate thread, or if your program is running in a loop, you can look for serial data within the loop.
BrianKudsk
Posts: 24
Joined: Sep 08, 2010 20:27
Location: Florida

reading the serial port.

Post by BrianKudsk »

Thank you for the reply.
Now to show my heavy DOS , 68HC11 background, I will need to go research "Threads"
I knew eventually I would have to leave my well known (comfortable)routines.
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

Here is an example of a routine I use in one of my programs to get the temperature reading from a Fluke temperature meter. You should be able to adapt this to your needs.

Code: Select all

Dim Shared quit As Integer
Dim Shared meterval As String
Dim thread As Any Ptr

'temperature meter reading thread
Sub mythread(param As Any Pointer)
    While quit=0
        While Loc(5)=0
            If quit<>0 Then 
                Exit Sub
            End If
            Sleep 10,0
        Wend
        Input #5,meterval
        
    Wend
End Sub


Open com "com1:9600,n,8,1,cs0,ds0,cd0" As #5
Sleep 200

If Err <> 0 Then
    Cls
    Print "Error opening temp meter port " 
    Sleep
    End
End If

Sleep 800
Print #5,"T"' start meter
thread=Threadcreate(@mythread,0) ' create thread for temp meter

Do
    Print meterval
    Sleep 10,0
Loop Until Multikey(1)
quit = 1
Threadwait(thread)
BrianKudsk
Posts: 24
Joined: Sep 08, 2010 20:27
Location: Florida

reading a serial port

Post by BrianKudsk »

Thanks again. I copied your code and I will try it here in a little bit. After looking at it I think I have a better understanding. Thanks again.
I will post the result, Good or bad.
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

You would need to add some code to check if the serial port data has updated. Othererwise, the return value (meterval) will always be the same until new serial data arrives. In my application, this wasn't necessary.
BrianKudsk
Posts: 24
Joined: Sep 08, 2010 20:27
Location: Florida

reading the serial port.

Post by BrianKudsk »

AWESOME.
The code worked right out of the box. All I did was substitute 9600 with 38400.
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

Great! Here is an example that will only display if the value is updated.

Code: Select all

Dim Shared quit As Integer
Dim Shared meterval As String
Dim thread As Any Ptr

'temperature meter reading thread
Sub mythread(param As Any Pointer)
    While quit=0
        While Loc(5)=0
            If quit<>0 Then 
                Exit Sub
            End If
            Sleep 10,0
        Wend
        Input #5,meterval
        
    Wend
End Sub


Open com "com1:9600,n,8,1,cs0,ds0,cd0" As #5
Sleep 200

If Err <> 0 Then
    Cls
    Print "Error opening temp meter port " 
    Sleep
    End
End If

Sleep 800
Print #5,"T"' start meter
thread=Threadcreate(@mythread,0) ' create thread for temp meter
meterval = "invalid"
Do
    If meterval <> "invalid" Then
        Print meterval
        meterval = "invalid"
    End If
    
    Sleep 10,0
Loop Until Multikey(1)
quit = 1
Threadwait(thread)
BrianKudsk
Posts: 24
Joined: Sep 08, 2010 20:27
Location: Florida

reading the serial port.

Post by BrianKudsk »

The device I am reading is constantly transmitting. All I really need to do now is decode the information and perform some math to get my final usable information. By the way the second program also worked right out of the box!
Thanks again. When I get this part all the way done I will post the result. Hopefully today.
BrianKudsk
Posts: 24
Joined: Sep 08, 2010 20:27
Location: Florida

reading the serial port

Post by BrianKudsk »

Ok. I am full up and running. The calculations are complete. In this respect FB is like QB. I had to DIM some of the variables , but other than that I am displaying Altitude at a very fast rate,.
Next job is a nice looking screen. Most of my programs use screen 13 for the big fonts, I may do some oage flipping , because FB seems to be very fast.
One more thing that I will need to do is see how the final EXE reacts to some of the platforms I am using.
Thanks again
AMSA
Posts: 9
Joined: Jan 27, 2011 20:35

Post by AMSA »

Hello there people.

phishguy, could you explain a little bite each part of the code that you have written? I was thinking to do a project like your, but, instead of being a temperature equipment, I was thinking to do that on a Fluke 45.

And more, I thought doing some kind of data logger or something, GUI format.

Best regards,
Charles
12val12newakk
Posts: 35
Joined: Nov 14, 2019 17:04

Re: reading the serial port.

Post by 12val12newakk »

I want to connect the MCP3201 directly to the LPT
to get the code from the ADC requires 50 requests to the port.
one request( for both writing and reading )using this library takes 3+ μS.
I remember exactly that when it was under Windows xp
through the built-in access to ports, the request was 1.2 μS.
How can I reduce the request time ?

here is the working code STM32

Code: Select all

   unsigned short Read_MCP3201 (void)
{
   int i=0;    
	unsigned short  AdcResult=0;   //must 16 BIT 
        CS_MCP3201_High;  ///CS   
	      __disable_irq ();
       for(i=0;i<16;i++)
       {
             CLK_MCP3201_Low;  //ADC_CLK=0 
			  	   CLK_MCP3201_High;  ///clk
			      AdcResult<<=1;   
       if(HAL_GPIO_ReadPin(DATA_MCP3201_GPIO_Port, DATA_MCP3201_Pin))  
							{ AdcResult |= 1;}   
       }  
			   __enable_irq ();
      CS_MCP3201_High;  ///CS 
			    AdcResult<<=3;
           AdcResult>>=4;   
       return(AdcResult);                          
 }
12val12newakk
Posts: 35
Joined: Nov 14, 2019 17:04

Re: reading the serial port.

Post by 12val12newakk »

SORY // DOUBLE
Post Reply