Serial Data Acquisition Program

Directly on the reference program:

Dim?av?As?Variant

Dim?datacount?As?Long

Private?Sub?cmdClear_Click()

txtData.Text?=?""

End?Sub

Private?Sub?cmdStop_Click()

'Close port

If?MSComm.PortOpen?=?True?Then

MSComm.InBufferCount?=?0' Empty Buffer

MSComm.PortOpen?=?False

End?If

cmdReceive.Enabled?=?True

lblStatus.Caption?=?" Stop receiving, idle"

End?Sub

Private?Sub?cmdReceive_Click()

'Serial port settings

With?MSComm

.CommPort?=?1

.Settings?=?" 9600,N,8,1"

.RThreshold?=?1?'Receive 1 byte to trigger oncomm event

.InputMode?=?comInputModeBinary

.InputLen?=?1'Input length is 1

. InBufferCount?=?0?'Clear receive buffer

End?With

'Open port

If?MSComm.PortOpen?=?False?Then

MSComm. Err?Then

MsgBox?(Err.Description)

Exit?Sub

End?If

End?If

lblStatus.Caption?=?" Open port, waiting to receive"

datacount?=?0

cmdReceive.Enabled?=?False

End?Sub

Private?Sub?cmdSave_Click()

Dim?outfn?As ?String

MsgBox?("Received"? +?CStr(datacount)? +?" Group data")

lblStatus.Caption?=?" Receive complete, please select output file"

cmdReceive.Enabled?=?True

'Select output file

CommonDialog1.FileName?=?CStr(Date)? +?" .txt"

CommonDialog1.Filter?=?" Text?Files|*.txt"

CommonDialog1.Flags?=?CommonDialog1.Flags?Or?cdlOFNOverwritePrompt

CommonDialog1.CancelError?=?True< /p>

On?Error?GoTo?errhandler

CommonDialog1.ShowSave

outfn?=?CommonDialog1.FileName

Open?outfn?For?Output?As?#1

Print?#1,?txtData.Text

Close?#1

'txtData.SaveFile?outfn

lblStatus.Caption?=?" Output complete, idle"

errhandler:

Exit?Sub

End?Sub

Private?Sub?Form_Load()

lblStatus.Caption?=?" Idle"

End?Sub

Private?Sub?Form_Unload(Cancel?As?Integer)

'Close port

If?MSComm.PortOpen?=?True?Then

MSComm. InBufferCount?=?0'Empty buffer

MSComm.PortOpen?=?False

End?If

End?Sub

Private?Sub?MSComm_OnComm()

Dim?T1,? T2?As?Long

Select?Case?MSComm.CommEvent

Case?comEvReceive 'Receive event generated by Rthreshold bytes received

MSComm. Event Receive

lblStatus.Caption?=?" Receive"

av?=?MSComm.Input'Read a receive byte

dataframe(1)? =?av(0) 'Convert to bytes

If?dataframe(1)? =? &HA?Then'Receive T1

Do

DoEvents

Loop?Until?MSComm.InBufferCount?>=?2?'Loop and wait for the receive buffer to >=2 bytes

av?=?MSComm. Input

dataframe(2)? =?av(0)

av?=?MSComm.Input

dataframe(3)? =?av(0)'Receive T1

T1?=?dataframe(2)? +?CLng(dataframe(3))? *?256'Calculate T1

End?If

Do

DoEvents

Loop?Until?MSComm.InBufferCount?>=?1?'Loop waits for the receive buffer to be >=1 byte

av?=? MSComm.Input 'Read a receive byte

dataframe(4)? =?av(0) 'Convert to byte

'Receive to T2

If?dataframe(4)? =? &HA0?Then

'MSComm.RThreshold?=?0?'Turn off OnComm event reception

'Loop waiting for receive buffer >=2 bytes

Do

DoEvents

Loop?Until?MSComm .InBufferCount?>=?2

av?=?MSComm.Input

dataframe(5)? =?av(0)

av?=?MSComm.Input

dataframe(6)? =?av(0)'Receive T2

T2?=?dataframe(5)? +?CLng(dataframe(6))? *?256'Calculate T2

'Display T1?T2?enter

txtData.Text?=?txtData.Text?+?CStr(T1)? +?" ?" ? +?CStr(T2)? +?Chr(&HD)? +?Chr(&HA)

datacount?=?datacount?+?1'Number of data sets +1

End?If

MSComm.RThreshold?=?1'Turn on OnComm event reception

Case?Else

End?Select

End?Sub

The baud rate for RS232 serial communication is set to 9600, 8 data bits, one stop bit, and no parity bit.

2. Each set of data contains T1 (16 bits) and T2 (16 bits), dividing each data into two 8-bit data, first the lower 8 bits, then the higher 8 bits. The data is unsigned integer.

T1 is sent first, then T2, then the next set of T1 and T2.

T1 has the header data 0x0A (hex, decimal bit 10) as the header byte, then the low 8 bits of T1, and the high 8 bits of T1.

T2 takes header data 0xA0 (hex, decimal bit 160) as its header byte, then the low 8 bits of T2, and the high 8 bits of T2.

Sending timing example: 0x0A, t1 low 8 bits, t1 high 8 bits, 0xA0, t2 low 8 bits, t2 high 8 bits ......

Combine the high and low 8 bits of the received data T1, T2, convert them to a decimal number, and store them in a txt text file in the form of T1 T2 per line <

If you need to do other 'file format' processing, the data has been obtained, how do you want to get on the change.