RS232 communication

Just starting out? Need help? Post your questions and find answers here.
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

RS232 communication

Post by SP »

Dears purebasicmaniacs, I need yours suggestions (sorry for my english)

The question is this:

I have to perform a serial communications with a device.
This device sends data without previous interrogation and without predetermined time i.e. in any moment.

I would prefer not to have to do a cyclic routine based on a timer that to intervals looks the input buffer.
I would like a callback function or similar, to perform this.

Can anybody helps me? Send an example please.

Thanks in advanced.
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post by SP »

Thanks Droopy, but I think that this library not allow to make this.(Perhaps I'm in error, but not found a function that perform it).

Greetings
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

It works here: PB 3.94.
bye,
Daniel
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post by SP »

Hi Daniel,

Do you mean that I have to work with 3.94 version to be able to do this?
Marc
User
User
Posts: 28
Joined: Mon Jun 16, 2003 9:02 pm
Location: Paris - Villemer
Contact:

Post by Marc »

Please send your sample code...

May be you can test the inputbuffer in the event loop and if data are available, process the job...

Marc
Marc from PARIS - MVCOM Library
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post by SP »

Bonjour Marc,

I have not written code. I am considering the form to perform the serial communications. I take note of your suggestion.

Thanks by your time.
A greeting from Barcelona
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

SP wrote:I have not written code.
Huh? Where do you know then that there is a bug in MVCom? Marc would try to fix it.
bye,
Daniel
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post by SP »

Hi Daniel,

My english is very poor. Perhaps not explains me well.

I try to say that I'm thinking about how perform this. I don't believe that the MVCOM have a bug. I wanted the way to receive an event reporting the arrival of characters to the input buffer.

Thanks.
Greetings
Santi
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Post by Baldrick »

Here is a snippet of a small recieve procedure I use in an app I have written in PB 3.94.
It is using MVCOM lib in PB 3.94.
This procedure would need to be updated to suit the new MVCOM lib Marc Vitry has made for PB 4.0, but it will work without problem.

Code: Select all

; PB 3.94
 ;{ Rx Start
 Procedure.s Rx(Hcom)
 If Car$=Chr(0) ;MVCOM lib will cause a crash if string result is null
 Car$=""    ; so deliberately force result string to have some value
 EndIf    ; prior to attempting to read data 
 If Hcom
 While ComInputBufferCount(Hcom)>0
 If ComInput(Hcom,Car$)
 CarVal=Asc(Car$)
 Rx$+Chr(CarVal)
 EndIf
 Wend
 EndIf
 ProcedureReturn Rx$ 
 EndProcedure
 ;} Rx End
Maybe this will help you a little.
Regards,
Baldrick
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post by SP »

Thanks for your code Baldrick,
Greetings
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Baldrick wrote:Here is a snippet of a small recieve procedure I use in an app I have written in PB 3.94.
It is using MVCOM lib in PB 3.94.
This procedure would need to be updated to suit the new MVCOM lib Marc Vitry has made for PB 4.0, but it will work without problem.

Code: Select all

; PB 3.94
 ;{ Rx Start
 Procedure.s Rx(Hcom)
 If Car$=Chr(0) ;MVCOM lib will cause a crash if string result is null
 Car$=""    ; so deliberately force result string to have some value
 EndIf    ; prior to attempting to read data 
 If Hcom
 While ComInputBufferCount(Hcom)>0
 If ComInput(Hcom,Car$)
 CarVal=Asc(Car$)
 Rx$+Chr(CarVal)
 EndIf
 Wend
 EndIf
 ProcedureReturn Rx$ 
 EndProcedure
 ;} Rx End
Maybe this will help you a little.
Regards,
Baldrick
So there is a 4.0 version of MVCOM?

- np
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Post by Baldrick »

NoahPhense,
Yes there is a version of the MVCOM lib for PB4.0.
You can download from PureArea.
Here is a shortcut to it for you.
http://www.purearea.net/pb/download/use ... ARYV12.zip

I have not used it with Pb4 but I do know there are a few changes in the lib such as ComInput(Hcom,..,..) has been renamed as ComRead(Hcom,.) and uses a slightly different memory access to the 3.9x version.
The snippet I posted earlier is 1 I use in a small app I have written to read Rs232 printer output data from an alarm product I install, service, etc here in Aus. It was originally written using around 3.92 or earlier and updated up to 3.94 & I have not really had any need to update to Pb4.0.
Here is a pdf of the alarm panel specs for your interest.
http://www.mcmelectronics.com.au/PDF%20 ... cation.pdf
I am at the moment very busy with a couple of large sites I am setting up, so when I am finished those & hopefully have a little spare time on my hands I will see if I can do a little update on my Rs232 printer read program to PB4

Regards,
Baldrick
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Post by Baldrick »

@NoahPhense,
Don't know if this will be of any use for you, but over the weekend I have made up a little program which may help you a little.
It is just simply a blind receiver for ASCII. I have run it connected to that alarm panel i pasted the link to previously & it runs quite nicely.
Also connected to a 2nd computer running Hyperterminal & used hyperterminal to paste some ascii text copied from a text file which ran quite nicely also.
Cable used for PC to PC link I made up as 3 wire only crossover.
i.e. Db9A pin2 to Db9B pin3
Db9A pin3 to Db9B pin2
Db9A pin5 to Db9B pin5

This little proggy requires MVCOM lib as the PB4.0 update Marc Vitry has done.

Loop code:

Code: Select all

; PureBasic Visual Designer v3.95 build 1485 (PB4Code)
; Coded by Baldrick 22/10/2006

IncludeFile "ASCII_Rx_1_Procs.pb"
;XIncludeFile #PB_Compiler_Home+"My Projects\ErrorHandle.pbi"

Open_Window_Main()
Connect$=Read_PortPref()
StatusBarText(#StatusBar_0,0,"Not Connected ---> "+Connect$)

Repeat
  
  Event = WaitWindowEvent(1):WindowID = EventWindow():GadgetID = EventGadget()
  EventType = EventType():MenuID = EventMenu()
  If Hcom
   If ComInputBufferCount(Hcom)
    MyRx$=Rx(Hcom)
    InsertEditorText(#Editor_DataIn,MyRx$)
   EndIf 
  EndIf 
  
  If Event = #PB_Event_Menu
    Select MenuID
     Case #MENU_2
      Open_Window_Com_Setup()
     Case #MENU_3
      If Hcom=0
       Hcom=Connect(Connect$)
        If Hcom
         StatusBarText(#StatusBar_0,0,"Connected ---> "+Connect$)
        EndIf
      EndIf 
     Case #MENU_4
      Dis=Disconnect(Hcom)
       If Dis
        Hcom=0
        StatusBarText(#StatusBar_0,0,"Not Connected ---> "+Connect$)
       EndIf
     Case #MENU_8
      Quit=1
     Case #MENU_13
      Open_Window_About(About)
    EndSelect 
  EndIf
  
  If Event = #PB_Event_Gadget
    Select GadgetID
     Case #Button_Connect
      If Hcom=0
       Hcom=Connect(Connect$)
        If Hcom
         StatusBarText(#StatusBar_0,0,"Connected ---> "+Connect$)
        EndIf 
       EndIf 
     Case #Button_Disconnect
      Dis=Disconnect(Hcom)
       If Dis
        Hcom=0
        StatusBarText(#StatusBar_0,0,"Not Connected ---> "+Connect$)
       EndIf 
     Case #Button_Setup_Save
      If Hcom=0
       Write_PortPref(#Window_Com_Setup)
       Connect$=Read_PortPref()
       StatusBarText(#StatusBar_0,0,"Not Connected ---> "+Connect$)
        Else 
       MessageRequester(#Appname,"Please disconnect before attempting to modify connection")
      EndIf 
     Case #Button_About_Ok
      CloseWindow(#Window_About)
    EndSelect 
  EndIf
  
  If Event = #PB_Event_CloseWindow
    Select WindowID
     Case #Window_Main
      Quit=1
     Case #Window_Com_Setup
      CloseWindow(#Window_Com_Setup)
    EndSelect 
  EndIf 
  
Until Quit
  If Hcom
   Dis=Disconnect(Hcom)
   Hcom=0
   EndIf 
End
;
Include file as ASCII_Rx_1_Procs.pb

Code: Select all

; PureBasic Visual Designer v3.95 build 1485 (PB4Code)
; Coded by Baldrick 22/10/2006

;- Window Constants
;
Enumeration
  #Window_Main
  #Window_Com_Setup
  #Window_About
EndEnumeration

;- MenuBar Constants
;
Enumeration
  #MenuBar_0
EndEnumeration

Enumeration
  #MENU_2
  #MENU_3
  #MENU_4
  #MENU_8
  #MENU_13
  #MENU_15
EndEnumeration

;- Gadget Constants
;
Enumeration
  #Button_Connect
  #Button_Disconnect
  #Editor_DataIn
  #Text_LabelPort
  #Text_Label_Speed
  #Text_Label_Data
  #Text_Label_Stop
  #Text_Label_Parity
  #Combo_Com
  #Combo_Baud
  #Combo_Dbits
  #Combo_Sbits
  #Combo_Parity
  #Button_Setup_Save
  #Text_About
  #Button_About_Ok
EndEnumeration
  #Appname="ASCII Serial Receiver"

;- StatusBar Constants
;
Enumeration
  #StatusBar_0
EndEnumeration

About.s="This program coded by Baldrick as a test example only"+#CRLF$
About+"By use of this program you agree to accept all liability for any damages "
About+"caused by use of this program whether direct or indirectly, blah, blah....."

Procedure InsertEditorText(editorgadget,results$) 
 ProcedureReturn SendMessage_(GadgetID(editorgadget),#EM_REPLACESEL,0,results$) 
EndProcedure

Procedure Connect(Connect$)
  Hcom=ComOpen(Connect$,#HandShakeModeNone,128,128) ; in tests memory buffers down to value 
   If Hcom=0                                        ; 1 byte work without fault
    MessageRequester(#Appname,"Connection fail, please check settings and try again")
    Else
     Beep_(1500,250)
     DisableGadget(#Button_Connect,1)
     DisableGadget(#Button_Disconnect,0)
   EndIf
 ProcedureReturn Hcom
EndProcedure 

Procedure DisConnect(Hcom)
  If Hcom
   Result=ComClose(Hcom)
  EndIf 
   If Result=#True
    Beep_(400,200)
    DisableGadget(#Button_Connect,0)
    DisableGadget(#Button_Disconnect,1)
   EndIf 
 ProcedureReturn Result
EndProcedure 

Procedure.s Rx(Hcom)
  Count=0
   Count=ComInputBufferCount(Hcom)
    If Count
     Rxd.s=Space(Count)
     ComRead(Hcom,@Rxd,Count)
      Else
     Rxd.s=""
    EndIf 
 ProcedureReturn Rxd
EndProcedure 
  
Procedure.s Read_PortPref()
  If OpenPreferences(#Appname+".ini")=0
   MessageRequester(#Appname,"Configuration file not found, defaults will be applied")
  EndIf
   PreferenceGroup("ComString")
   Port$=ReadPreferenceString("Port","Com1")+": "
   Speed$="baud="+ReadPreferenceString("Baud","9600")+" " 
   Parity$="parity="+ReadPreferenceString("Parity","N")+" "
   Dbits$="data="+ReadPreferenceString("Dbits","8")+" "
   Sbits$="stop="+ReadPreferenceString("Sbits","1")
   Connection$=Port$+Speed$+Parity$+Dbits$+Sbits$
   ClosePreferences()
 ProcedureReturn Connection$
EndProcedure 

Procedure Write_PortPref(WinID)
  If IsWindow(WinID)
    If CreatePreferences(#Appname+".ini")
    PreferenceGroup("ComString")
     WritePreferenceString("Port",GetGadgetText(#Combo_Com))
     WritePreferenceString("Baud",GetGadgetText(#Combo_Baud))
     WritePreferenceString("Parity",GetGadgetText(#Combo_Parity))
     WritePreferenceString("Dbits",GetGadgetText(#Combo_Dbits))
     WritePreferenceString("Sbits",GetGadgetText(#Combo_Sbits))
     PreferenceGroup("ComState")
     WritePreferenceLong("Port",GetGadgetState(#Combo_Com))
     WritePreferenceLong("Baud",GetGadgetState(#Combo_Baud))
     WritePreferenceLong("Parity",GetGadgetState(#Combo_Parity))
     WritePreferenceLong("Dbits",GetGadgetState(#Combo_Dbits))
     WritePreferenceLong("Sbits",GetGadgetState(#Combo_Sbits))
     ClosePreferences()
    EndIf 
  EndIf 
EndProcedure 

Procedure Open_Window_Main()
  If OpenWindow(#Window_Main, 254, 11, 600, 600, #Appname,  #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
   If CreateMenu(#MenuBar_0, WindowID(#Window_Main))
      MenuTitle("&File")
      MenuItem(#MENU_2, "&Setup")
      MenuBar()
      MenuItem(#MENU_3, "&Connect")
      MenuItem(#MENU_4, "&Disconnect")
      MenuBar()
      MenuItem(#MENU_8, "&Exit")
      AddKeyboardShortcut(#Window_Main,#PB_Shortcut_Escape,#Menu_8)
      MenuTitle("&Help")
      MenuItem(#MENU_13, "&About")
      MenuBar()
      MenuItem(#MENU_15, "Hel&p")
   EndIf
    If CreateStatusBar(#StatusBar_0, WindowID(#Window_Main))
    EndIf
   If CreateGadgetList(WindowID(#Window_Main))
          ButtonGadget(#Button_Connect, 20, 20, 60, 20, "Connect")
          ButtonGadget(#Button_Disconnect, 90, 20, 60, 20, "Disconnect")
          DisableGadget(#Button_Disconnect,1)
          EditorGadget(#Editor_DataIn, 20, 50, 560, 490)   
   EndIf
  EndIf
EndProcedure

Procedure Open_Window_Com_Setup()
  If OpenWindow(#Window_Com_Setup, 262, 20, 365, 126, #Appname+" Comms Set Up",  #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_WindowCentered,WindowID(#Window_Main) )
    If CreateGadgetList(WindowID(#Window_Com_Setup))
      TextGadget(#Text_LabelPort, 15, 15, 45, 20, "ComPort")
      TextGadget(#Text_Label_Speed, 105, 15, 50, 20, "Baud rate")
      TextGadget(#Text_Label_Data, 190, 15, 30, 20, "D bits")
      TextGadget(#Text_Label_Stop, 245, 15, 30, 20, "S bits")
      TextGadget(#Text_Label_Parity, 295, 15, 35, 20, "Parity")
      ComboBoxGadget(#Combo_Com, 15, 40, 80, 110)
      ComboBoxGadget(#Combo_Baud, 100, 40, 85, 110)
      ComboBoxGadget(#Combo_Dbits, 190, 40, 50, 110)
      ComboBoxGadget(#Combo_Sbits, 245, 40, 50, 105)
      ComboBoxGadget(#Combo_Parity, 300, 40, 50, 110)
      ButtonGadget(#Button_Setup_Save, 155, 90, 50, 20, "Save")
      OpenPreferences(#Appname+".ini")
      PreferenceGroup("ComState")
       For a=1 To 8
        AddGadgetItem(#Combo_Com,-1,"Com"+Str(a))
       Next 
      Comstate=ReadPreferenceLong("Port",0)
      SetGadgetState(#Combo_Com,ComState)
      Baud=300
       For a=1 To 7
        AddGadgetItem(#Combo_Baud,-1,Str(Baud))
        Baud*2
       Next
      BaudState=ReadPreferenceLong("Baud",5)
      SetGadgetState(#Combo_Baud,BaudState)
       For a=1 To 2
        AddGadgetItem(#Combo_Dbits,-1,Str(a+6))
        AddGadgetItem(#Combo_Sbits,-1,Str(a))
       Next
      DbitsState=ReadPreferenceLong("Dbits",1)
      SbitsState=ReadPreferenceLong("Sbits",0)
      SetGadgetState(#Combo_Dbits,DbitsState)
      SetGadgetState(#Combo_Sbits,SbitsState)
        AddGadgetItem(#Combo_Parity,-1,"N")
        AddGadgetItem(#Combo_Parity,-1,"E")
        AddGadgetItem(#Combo_Parity,-1,"O")
      ParityState=ReadPreferenceLong("Parity",0)
      SetGadgetState(#Combo_Parity,ParityState)
      ClosePreferences()
    EndIf
  EndIf
EndProcedure

Procedure Open_Window_About(About$)
  If OpenWindow(#Window_About,260,20,350,300,"About "+#Appname,#PB_Window_TitleBar|#PB_Window_WindowCentered,WindowID(#Window_Main))
   If CreateGadgetList(WindowID(#Window_About))
    TextGadget(#Text_About,10,10,330,240,About$,#PB_Text_Border)
    ButtonGadget(#Button_About_Ok,150,270,50,20,"OK")
   EndIf 
  EndIf 
EndProcedure 

Regards,
Baldrick.

Edited to fix small bug - ( option which was allowing "0" stop bits :oops: )
Post Reply