请教一下具体代码实现。我的代码如下,每触发一次onComm事件就写入打开的文件,但写入文件后总是出现字符串被截断现象,如果在前面加了延时则字符串不被截断,但是字符串后面会出现多余的一行空行。请看一下我的代码有什么问题。
Dim InBuff As String
Private Sub cmdNewLog1_Click() Open CommonDialog4.FileName For Append As #SaveLogID DisplayFlag1 = True
End Sub
Private Sub MSComm1_OnComm() Select Case Case comEvReceive InBuff = MSComm1.Input If DisplayFlag1 = True Then Print #SaveLogID, InBuff Close #SaveLogID End If End Select
End Sub
Dim InBuff As String
Private Sub cmdNewLog1_Click() Open CommonDialog4.FileName For Append As #SaveLogID单击后打开日志文件 DisplayFlag1 = True
End Sub
Private Sub MSComm1_OnComm() Select Case Case comEvReceive InBuff = MSComm1.Input If DisplayFlag1 = True Then Print #SaveLogID, InBuff保存内容 Close #SaveLogID关闭日志 End If End Select
End Sub
Dim InBuff As String
Private Sub cmdNewLog1_Click() DisplayFlag1 = True
End Sub
Private Sub MSComm1_OnComm() Open CommonDialog4.FileName For Append As #SaveLogID Select Case Case comEvReceive InBuff = MSComm1.Input If DisplayFlag1 = True Then Print #SaveLogID, InBuff End If End Select Close #SaveLogID
End Sub
多谢回复!但是, 我在点击这个按钮事件中 cmdNewLog1_Click() 要打开一个文件并指定路径,串口数据来了就自动写入这个文件, 而不是串口事件出现才打开一个文件并保存。点击后这个文件可以一直打开,串口数据就可以一直写入,直到关闭程序。
Dim InBuff As String
Private Sub cmdNewLog1_Click() DisplayFlag1 = True
End Sub
Private Sub MSComm1_OnComm() Open CommonDialog4.FileName For Append As #SaveLogID Select Case Case comEvReceive InBuff = MSComm1.Input If DisplayFlag1 = True Then Print #SaveLogID, InBuff End If End Select Close #SaveLogID
End Sub
sorry,我第一帖代码有问题,应该是下面这个,OnComm事件中的关闭文件这一行是注掉的。
Private Sub cmdNewLog1_Click() Open CommonDialog4.FileName For Append As #SaveLogID DisplayFlag1 = True
End Sub
Private Sub MSComm1_OnComm() Select Case Case comEvReceive InBuff = MSComm1.Input If DisplayFlag1 = True Then Print #SaveLogID, InBuff 'Close #SaveLogID End If End Select