Skip to content

Commit

Permalink
auto scroll received data
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhex53 committed Dec 17, 2024
1 parent f430948 commit 108ddfd
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
Binary file added .github/20241217095029.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
.externalNativeBuild
.cxx
local.properties
/app/release
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

Another simple serial tester for Android, use `jSerialComm`, support HEX send and receive mode, very easy to use and convinient for Modbus debugging.

![Phone](.github/20241216211119.png)
![Phone](.github/20241216211119.png)

![Tablet RK3288](.github/20241217095029.png)
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ android {

dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.activity:activity-compose:1.7.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7")
implementation("androidx.activity:activity-compose:1.9.3")
implementation(platform("androidx.compose:compose-bom:2023.10.01"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
Expand Down
19 changes: 15 additions & 4 deletions app/src/main/java/com/zhaoxinsoft/serialtester/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
Expand All @@ -40,6 +41,7 @@ import androidx.compose.material3.TopAppBarDefaults.topAppBarColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
Expand All @@ -53,6 +55,7 @@ import com.fazecast.jSerialComm.SerialPort
import com.fazecast.jSerialComm.SerialPortDataListener
import com.fazecast.jSerialComm.SerialPortEvent
import com.zhaoxinsoft.serialtester.ui.theme.SerialTesterTheme
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
Expand Down Expand Up @@ -100,7 +103,7 @@ class MainActivity : ComponentActivity() {
val str = if (viewModel.hexMode.value) {
newData.joinToString(" ") { String.format("%02X", it) }
} else {
newData.toString(Charsets.UTF_8)
newData.toString(Charsets.UTF_8).trimEnd()
}
val timestamp = SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault()).format(Date())
viewModel.receivedData.add("[$timestamp] RX: $str")
Expand Down Expand Up @@ -139,12 +142,10 @@ class MainActivity : ComponentActivity() {
private fun disconnect() {
serialPort?.closePort()
serialPort?.removeDataListener()
viewModel.receivedData.clear()
viewModel.isConnected.value = false
}

private fun clear() {
// viewModel.data.value = null
viewModel.receivedData.clear()
}

Expand Down Expand Up @@ -285,16 +286,26 @@ class MainActivity : ComponentActivity() {
Text("Send")
}
}
} else if (viewModel.receivedData.isNotEmpty()) {
OutlinedButton(onClick = { clear() }) {
Text("Clear")
}
}
val lazyGridState = rememberLazyGridState()
val coroutineScope = rememberCoroutineScope()
LazyVerticalGrid(
columns = GridCells.Fixed(1), modifier = Modifier.fillMaxWidth(),
columns = GridCells.Fixed(1), modifier = Modifier.fillMaxWidth(), state = lazyGridState
) {
item {
// put static item here
}
items(viewModel.receivedData) { data ->
Text(data)
}
coroutineScope.launch {
if (viewModel.receivedData.isNotEmpty())
lazyGridState.scrollToItem(viewModel.receivedData.size - 1)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.lifecycle.ViewModel

class MainViewModel : ViewModel() {
val availablePorts = mutableListOf<String?>(null)
val isConnected = mutableStateOf(true)
val isConnected = mutableStateOf(false)
val selectedDevice = mutableStateOf<String?>(null)
val baudRate = mutableStateOf<Number?>(9600)
val dataBits = mutableStateOf<Number?>(8)
Expand Down

0 comments on commit 108ddfd

Please sign in to comment.