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).
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.
For all projects in connection with the class SerialPort (gb.net) the following procedure has proven itself:
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:
Figure 24.1.5.3.2: Faulty start …
Figure 24.1.5.3.4: Display of temperature and unit