User Tools

Site Tools


Sidebar

Network and communication

k24:k24.1:k24.1.5:k24.1.5.3:start

24.1.5.3 Project Temperature measurement

In this project a temperature value is determined and displayed. The computer and the board are connected via their serial interfaces. On the inserted board is a sensor of the type TS-NTC-103, which forms a voltage divider with a resistor. The temperature-dependent voltage is picked up there and is an analog value at the ADC input of an IC Picaxe 08M2, which outputs this value as a digital value (0… 255) to an RS232 interface. The RS232M program accesses the serial interface and reads the data stream from pin 2 (RxD).

B1
Figure 24.1.5.3.1: Circuit diagram

The circuit was designed by Stephan Mischnick (www.strippenstrolch.de) for the gambas book. Afterwards he built and tested the boards in a small series.

24.1.5.3.1 Notes

For all projects in connection with the class SerialPort (gb.net) the following procedure has proven itself:

  • Determine whether the computer has serial interfaces. If this is the case, a list of serial ports is generated with the path names such as /dev/ttyUSB0 for a USB-RS232 adapter or /dev/ttyS0 for a real serial port.
  • Determine if the user has the right to access serial ports.
  • The initialization of the selected serial interface comprises three steps: (1) Set port names, (2) set transmission parameters and (3) set data flow control.
  • Open serial port: After successful opening, SerialPort.status goes to Net.Active.
  • Read data: The SerialPort_Read() event is triggered when data can be read from the serial port. Option 1: Temporarily store data in a variable, Option 2: Display data in a suitable format and Option 3: Save data permanently (file).
  • Send data - if required.
  • Close serial port.

In the presented project, the outlined procedure is consistently implemented. Option 3 is not used, as this option is used in another project. Data is not sent.

The source code of FMain.class is completely specified and commented afterwards:

[1] ' Gambas class file
[2]
[3] Private $bAllowedClose As Boolean
[4] Private $aAdapterList As String[]
[5] Private $sTemperatureDigit As String
[6] Public TTimer As Timer
[7]
[8] Public Sub Form_Open()
[9]
[10]   Dim sLine As String
[11]
[12]   FMain.Center()
[13]   FMain.Resizable = False
[14]   SetLEDColor("gray")
[15]   btnStart.Enabled = False
[16]
[17]   If MRS232.CheckPermission() = False Then
[18]      $bAllowedClose = True
[19]      FMain.Close()
[20]   Endif
[21]
[22]   $aAdapterList = MRS232.GetRS232Devices()
[23]
[24]   If $aAdapterList.Count = 0 Then
[25]      cmbRS232PortName.Add(("No RS232 device detected!"))
[26]      SetLEDColor("red")
[27]      cmbRS232PortName.Enabled = False
[28]      lblRS232Parameters.Visible = False
[29]      $bAllowedClose = True
[30]      MRS232.SendSound("error.ogg")
[31]   Else
[32]      For Each sLine In $aAdapterList
[33]        cmbRS232PortName.Add(sLine)
[34]      Next
[35]      btnStart.Enabled = True
[36]      lblRS232Parameters.Visible = True
[37]      lblRS232Parameters.Enabled = False
[38]      SetLEDColor("red")
[39]   Endif
[40]
[41] End
[42]
[43] Public Sub btnStart_Click()
[44]
[45] ' SetRS232PortName
[46]   If hRS232.Status = Net.Active Then hRS232.Close()
[47]   hRS232.PortName = cmbRS232PortName.Text
[48] ' SetRS232TransmissionParameters
[49]   hRS232.Speed = 4800
[50]   hRS232.DataBits = SerialPort.Bits8
[51]   hRS232.Parity = SerialPort.None
[52]   hRS232.StopBits = SerialPort.Bits1
[53] ' SetDataFlowControl
[54]   hRS232.FlowControl = SerialPort.None
[55]
[56]   MRS232.ShowRS232Parameters(hRS232, lblRS232Parameters)
[57]
[58]   OpenRS232Port()
[59]
[60]   cmbRS232PortName.Enabled = False
[61]   btnStart.Enabled = False
[62]
[63]   TTimer = New Timer As "TTimer"
[64]   TTimer.Delay = 500 * 1 ' The temperature is read out every 500ms
[65]   TTimer.Start()
[66]
[67]   MRS232.SendSound("on.wav")
[68]
[69] End
[70]
[71] Public Sub hRS232_Read()
[72]
[73]   Read #hRS232, $sTemperatureDigit, Lof(hRS232)
[74]
[75] End
[76]
[77] Public Sub TTimer_Timer()
[78]
[79]   ShowData($sTemperatureDigit)
[80]
[81] End
[82]
[83] Private Sub ShowData(sTemperature As String)
[84]
[85]   If $sTemperatureDigit <> "" Then
[86]      lblShowTemperature.Text = Asc(sTemperature) & " °C"
[87]      SetLEDColor("green")
[88]   Else
[89]      lblShowTemperature.Text = "--- °C"
[90]      SetLEDColor("red")
[91]   Endif
[92]
[93]   $sTemperatureDigit = ""
[94]
[95] End
[96]
[97] Private Sub OpenRS232Port()
[98]
[99]   Repeat
[100]     Try hRS232.Open(1)
[101]     If Error Then
[102]        Message.Error(("Error when opening the RS232 port!"))
[103]        Return
[104]     Endif
[105]   Until hRS232.Status = Net.Active
[106]
[107] End
[108]
[109] Private Sub SetLEDColor(sLEDColor As String)
[110]
[111]   pboxStatus.Picture = Picture["LED/led_" & sLEDColor & ".svg"]
[112]
[113] End
[114]
[115] Public Sub Form_Close()
[116]
[117]   If $bAllowedClose = False Then
[118]      If Message.Question(("Finish this experiment?"), ("Yes"), ("No")) = 1 Then
[119]         If hRS232 And If hRS232.Status = Net.Active Then hRS232.Close()
[120]      Else
[121]         Stop Event
[122]      Endif
[123]   Endif
[124]
[125] End

Comment:

  • Lines 17 to 20 determine whether the current user has access to the serial interfaces. The function CheckPermission() is located like the function GetRS232Devices() in a module MRS232.module. Its source code can be found in the project folder of the project used. The project archive can be found in the download area.
  • The function value of the function GetRS232Devices() is evaluated in lines 24 to 39.
  • If the user has access to serial interfaces, but the computer has no serial interfaces, then this is displayed and signaled visually and acoustically. You can then close the program window:

B2
Figure 24.1.5.3.2: Faulty start …

  • The port name, the transfer parameters and the type of data flow control are defined in lines 45 to 54.
  • If the user has access to serial ports and the computer has at least one serial port, you can start the measurement:

B3
Figure 24.1.5.3.3: Ready

  • After the start the serial port (lines 97 to 107) is opened. If the ADC supplies temperature values, these are read out and stored in the global variable $sTemperatureDigit.
  • The formatted display is realized by the procedure ShowData(…) in the lines 83 to 95:

B4
Figure 24.1.5.3.4: Display of temperature and unit

  • In lines 56 the set baud rate, the set transmission parameters and the type of data flow control used are recorded and displayed transparently in a label in a common format. The type of data flow control becomes visible via a tool tip of the label. These displays are not necessary.
  • The display goes to “- - - - °C”
  1. if the power supply is switched off or
  2. the connection of the serial interfaces of computer and board is disconnected or
  3. if the USB-RS232 adapter is disconnected from the computer during the measurement. The display only goes to “- - - - °C” after a few seconds if RTS has probably been set to “Not Ready To Send” by the driver.

B5
Figure 24.1.5.3.5: No data are read out

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k24/k24.1/k24.1.5/k24.1.5.3/start.txt · Last modified: 18.06.2022 (external edit)

Page Tools