基于VB6开发OMR SR-410答题卡程序:设备数据转可读格式技术问询
Great start on setting up the serial communication with your OMR device! Let’s break down what’s working in your current code and walk through the critical next steps to turn raw OMR data into that human-readable Q1-1/Q2-2 format you need.
Current Serial Port Setup Breakdown
Your Form_Load code correctly initializes the MSComm control for talking to the OMR:
- You’ve set the correct baud rate (19200), parity (even), data bits (7), and stop bits (2) — make sure these match exactly what your OMR device specifies in its documentation (this is non-negotiable for reliable communication).
- Enabling RTS handshaking and clearing input/output buffers is smart practice to avoid leftover data causing issues.
The missing piece here is handling the actual data coming from the device and parsing it into your desired format. Let’s fix that.
Next Steps to Implement
1. Capture Incoming Serial Data with the OnComm Event
Serial data doesn’t arrive all at once — you need to listen for when the device sends data using the MSComm1_OnComm event. This is the only reliable way to capture data as it comes in:
Private Sub MSComm1_OnComm() Dim rawData As Variant Dim formattedOutput As String Dim questionNum As Integer ' Check if the event is a data receive event If MSComm1.CommEvent = comEvReceive Then ' Read the incoming data (adjust input mode if your OMR uses binary) rawData = MSComm1.Input ' First, debug the raw data to understand its structure ' Paste this into a text box named txtDebug to see what the OMR sends txtDebug.Text = "Raw OMR Data: " & rawData & vbCrLf ' Now parse the data (example below — adjust based on your OMR's protocol) formattedOutput = "" questionNum = 1 ' Assume rawData is a string of answer values (e.g., "123412..." for Q1-Q6) For questionNum = 1 To Len(rawData) formattedOutput = formattedOutput & "Q" & questionNum & "-" & Mid(rawData, questionNum, 1) & vbCrLf Next questionNum ' Display the final readable format txtResults.Text = formattedOutput End If End Sub
2. Understand Your OMR’s Data Protocol
This is the most important part! Every OMR device outputs data in a specific structure:
- Some send ASCII strings (e.g.,
Q1:1,Q2:2or just a raw sequence like1234). - Others send binary data where each byte represents a question’s selected option.
- Many include a termination character (like a newline
vbCrLf) to signal the end of a card’s data.
Action: Grab your OMR device’s manual and find its data output specification. Use the txtDebug text box in the code above to see exactly what raw data it sends — this will tell you how to map each piece of data to a question number and answer.
3. Add Robustness & Error Handling
- Wrap serial port operations in error handling to catch issues like the port being in use:
Private Sub Form_Load() Screen.MousePointer = 11 On Error GoTo PortError MSComm1.Handshaking = comRTS MSComm1.RTSEnable = True MSComm1.CommPort = 1 MSComm1.Settings = "19200,E,7,2" MSComm1.PortOpen = True MSComm1.InBufferCount = 0 MSComm1.OutBufferCount = 0 Screen.MousePointer = 0 Exit Sub PortError: MsgBox "Failed to open serial port: " & Err.Description, vbCritical Screen.MousePointer = 0 End Sub - If your OMR sends data in chunks, buffer incoming data until you see the termination character (e.g., wait for
vbCrLfbefore parsing). - Validate parsed answers to ensure they’re valid (e.g., only 1-4 for multiple choice) and match the number of questions on your test.
4. Test & Validate
- Run the program with a test card you’ve manually filled out. Compare the formatted output to your manual answers to confirm the parsing works.
- Adjust the parsing logic if the question numbering or answer mapping is off (e.g., if the OMR starts counting at 0 instead of 1).
Bonus Tips
- If your OMR uses binary data, set
MSComm1.InputMode = comInputModeBinaryand handle the data as a byte array instead of a string. - Add a configuration option to set the number of questions, so your program works for different test papers.
- Save the formatted results to a text file or database for record-keeping.
内容的提问来源于stack exchange,提问作者ChikulotTechsp

