Page 1 of 1

SerialPort

Posted: Mon Sep 17, 2012 7:40 pm
by hth
Hi, how can I read the serial port as the GW-Basic program?
(Multimeter "METEX M-3650CR")

Image


hth

Re: SerialPort

Posted: Tue Sep 18, 2012 1:55 am
by IdeasVacuum

Re: SerialPort

Posted: Tue Sep 18, 2012 7:31 am
by infratec
Hi,

try this:

Code: Select all

OpenConsole()

Com = OpenSerialPort(#PB_Any, "COM1", 1200, #PB_SerialPort_NoParity, 7, 2, #PB_SerialPort_NoHandshake, 64, 64)
If Com
  Buffer$ = Space(14)
  WriteSerialPortString(Com, "D", #PB_Ascii)
  ReadSerialPortData(Com, @Buffer$, 14)
  PrintN(Buffer$)
  CloseSerialPort(Com)
EndIf
But it is without any checking and without a timeout!
(like the QB-Program)

And you have to compile it as console program (like the QB program)

Bernd

P.S.: for a timeout use AvailableSerialPortInput() to check if something is arrived.

Re: SerialPort

Posted: Tue Sep 18, 2012 9:57 am
by infratec
Hi,

here a solution which is more modern:

Code: Select all

#Version = "1.00"


Procedure Logging(Text$)
  
  Protected File
  
  File = OpenFile(#PB_Any, ReplaceString(ProgramFilename(), ".exe", ".csv"))
  If File
    FileSeek(File, Lof(File))
    WriteStringN(File, #DQUOTE$ + FormatDate("%yyyy%mm%dd %yy%ii%ss", Date()) + #DQUOTE$ + ";" + #DQUOTE$ + Text$ + #DQUOTE$)
  EndIf
  
EndProcedure


Com = OpenSerialPort(#PB_Any, "COM1", 1200, #PB_SerialPort_NoParity, 7, 2, #PB_SerialPort_NoHandshake, 64, 64)
If Not Com
  MessageRequester("Error", "Can not open COM port!")
  End
EndIf

OpenWindow(0, 0, 0, 200, 80, "MeterReader V" + #Version, #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TextGadget(1, 10, 10, 100, 20, "", #PB_Text_Border)
ButtonGadget(0, 10, 50, 60, 20, "Start")
CheckBoxGadget(2, 120, 10, 60, 20, "Logging")

Buffer$ = Space(14)

Exit = #False
Repeat
  Event = WaitWindowEvent(100)
  
  Select Event
    Case #PB_Event_Timer
      If EventTimer() = 1
        WriteSerialPortString(Com, "D")
      EndIf
    Case #PB_Event_Gadget
      If EventGadget() = 0
        If GetGadgetText(0) = "Start"
          SetGadgetText(0, "Stop")
          AddWindowTimer(0, 1, 1000)
        Else
          RemoveWindowTimer(0, 1)
          SetGadgetText(0, "Start")
        EndIf
      EndIf
    Case #PB_Event_CloseWindow
      Exit = #True
  EndSelect
  
  If AvailableSerialPortInput(Com) >= 14
    ReadSerialPortData(Com, @Buffer$, 14)
    SetGadgetText(1, Buffer$)
    If GetGadgetState(2)
      Logging(Buffer$)
    EndIf
  EndIf
  
Until Exit

CloseSerialPort(Com)
CloseWindow(0)
To have the log file at the same place, you need to enable 'create temporare executable in the source directory'.
The log file can be opened with calc or excel to make a diagramm out of it.

Bernd

P.S.: It's not tested, since I have no serial Meter.

Re: SerialPort

Posted: Tue Sep 18, 2012 3:53 pm
by infratec
Version 1.01

Code: Select all

#Version = "1.01"

Enumeration
  #Output1
  #Output2
  #Output3
  #Logging
  #Button
  #Time
EndEnumeration

Procedure Logging(T1$, T2$, T3$)
  
  Protected File.i, Line$
  
  File = OpenFile(#PB_Any, ReplaceString(ProgramFilename(), ".exe", ".csv"))
  If File
    FileSeek(File, Lof(File))
    Line$ = #DQUOTE$ + FormatDate("%yyyy%mm%dd %yy%ii%ss", Date()) + #DQUOTE$ + ";"
    Line$ + #DQUOTE$ + T1$ + #DQUOTE$ + ";"
    Line$ + #DQUOTE$ + T2$ + #DQUOTE$ + ";"
    Line$ + #DQUOTE$ + T3$ + #DQUOTE$
    WriteStringN(File, Line$)
  EndIf
  
EndProcedure


Com = OpenSerialPort(#PB_Any, "COM5", 1200, #PB_SerialPort_NoParity, 7, 2, #PB_SerialPort_NoHandshake, 64, 64)
If Not Com
  MessageRequester("Error", "Can not open COM port!")
  End
EndIf

OpenWindow(0, 0, 0, 190, 100, "MeterReader V" + #Version, #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TextGadget(#Output1, 10, 10, 40, 20, "", #PB_Text_Border|#PB_Text_Center)
TextGadget(#Output2, 60, 10, 60, 20, "", #PB_Text_Border|#PB_Text_Center)
TextGadget(#Output3, 130, 10, 40, 20, "", #PB_Text_Border|#PB_Text_Center)
CheckBoxGadget(#Logging, 120, 50, 60, 20, "Logging")
ButtonGadget(#Button, 10, 50, 60, 20, "Start")
SpinGadget(#Time, 80, 50, 30, 20, 1, 60, #PB_Spin_Numeric)
SetGadgetState(#Time, 1)

CreateStatusBar(0, WindowID(0))
AddStatusBarField(50)

Buffer$ = Space(14)

Exit = #False
Repeat
  Event = WaitWindowEvent(100)
  
  Select Event
    Case #PB_Event_Timer
      If EventTimer() = 1
        WriteSerialPortString(Com, "D")
        StatusBarText(0, 0, "read", #PB_StatusBar_Center)
        If FirstTime
          RemoveWindowTimer(0, 1)
          AddWindowTimer(0, 1, GetGadgetState(#Time) * 1000)
          FirstTime = #False
        EndIf
      EndIf
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Button
          If GetGadgetText(#Button) = "Start"
            SetGadgetText(#Button, "Stop")
            DisableGadget(#Time, #True)
            SetGadgetText(#Output1, "")
            SetGadgetText(#Output2, "")
            SetGadgetText(#Output3, "")
            AddWindowTimer(0, 1, 1)
            FirstTime = #True
          Else
            RemoveWindowTimer(0, 1)
            SetGadgetText(#Button, "Start")
            DisableGadget(#Time, #False)
            StatusBarText(0, 0, "")
          EndIf
        Case #Time
      EndSelect
    Case #PB_Event_CloseWindow
      Exit = #True
  EndSelect
  
  If AvailableSerialPortInput(Com) >= 14
    ReadSerialPortData(Com, @Buffer$, 14)
    SetGadgetText(#Output1, Trim(Mid(Buffer$, 1, 3)))
    SetGadgetText(#Output2, Trim(Mid(Buffer$, 4, 6)))
    SetGadgetText(#Output3, Trim(Mid(Buffer$, 10, 4)))
    If GetGadgetState(#Logging)
      Logging(GetGadgetText(#Output1), GetGadgetText(#Output2), GetGadgetText(#Output3))
    EndIf
    StatusBarText(0, 0, "")
  EndIf
  
Until Exit

CloseSerialPort(Com)
CloseWindow(0)
Still not tested :oops:

Bernd