我现在找到两台机器了,在上位机上运行一个vb程序,上面有4个通道,分别对应4个double类型的数据,然后将它们发到串口上面,我用一根串口线联到下位机上,然后再下位机运行接受数据的程序,就想实现这么一个功能,现在的问题是在下位机上面检测不到commevent.好像就没有数据发送出来似的。上位机vb代码如下 Option ExplicitDim PortIsOpen As Integer Dim NumberOfActiveChannels As IntegerDim ChannelValueArray(7) As String * 1Private Sub cboSetActiveChannels_Click() ' Dim Count As Integer Let Count% = 0 'Set up a channel counter loop Let NumberOfActiveChannels% = (Form01.cboSetActiveChannels.ListIndex + 1) 'and the No. of Active Chans.' Do If Count% < NumberOfActiveChannels% Then 'This loop runs 4 times and Let Form01.hsbChannelValue(Count%).Enabled = True 'for Active Channels enables Call hsbChannelValue_Scroll(Count%) 'the corresponding slider and Else 'updates the displayed value. Let Form01.hsbChannelValue(Count%).Enabled = False 'For Inactive Channels the Let Form01.lblChannelValue(Count%).Caption = "" 'slider bar is disabled and End If 'the displayed value cleared. Let Count% = (Count% + 1) Loop Until Count% = 8 End SubPrivate Sub cmdCommPortAction_Click()Dim Count As Integer Select Case PortIsOpen% Case False 'COM Port is currently not Open. On Error GoTo CommErrorHandler 'Set up an error handler. If Form01.optCommPortSelect(0).Value = True Then 'Depending on the users choice first Let Form01.Comm1.CommPort = 1 'set the appropriate COM Port, 1 or 2. Else Let Form01.Comm1.CommPort = 2 End If Let Form01.Comm1.Settings = "19200,N,8,1" 'The set the transmission parameters, Let Form01.Comm1.Handshaking = 2 'sets handshaking method to 'RTS/CTS' Let Form01.Comm1.PortOpen = True 'and attempts to open the port. Let Form01.Comm1.RTSEnable = True 'If Port Open = OK then power up the Let Form01.cmdCommPortAction.Caption = "Close Port" 'interface hardware, alter the caption Let Form01.cmdQuit.Enabled = False 'on the button, disable Quit button, Let PortIsOpen% = True 'and set the Port State variable. Let Form01.Comm1.InBufferCount = 0 'Because the slider values may have For Count% = 0 To (NumberOfActiveChannels% - 1) 'changed while the Port was closed an Let Form01.Comm1.Output = ChannelValueArray(Count%) 'update is performed to ensure the Next Count% 'hardware reflects the current values. Exit Sub 'Note the purge of the receive buffer.CommErrorHandler: 'Here if an error occured opening the Let Form01.cmdCommPortAction.Caption = "Open Port" 'COM Port. Ensure that the button Let Form01.cmdQuit.Enabled = True 'caption, the Quit button and the Let PortIsOpen% = False 'Port State variable are correctly Beep: Beep 'set. Sound to alert the user. Exit Sub Case True 'Here if the port is already Open so Let Form01.Comm1.RTSEnable = False 'power down the interface hardware Let Form01.Comm1.PortOpen = False 'and close it, change the caption on Let Form01.cmdCommPortAction.Caption = "Open Port" 'the button, re-enable Quit button Let Form01.cmdQuit.Enabled = True 'stop the Channel Value Write timer Let PortIsOpen% = False 'and set the Port State variable. End Select End SubPrivate Sub cmdQuit_Click() End End SubPrivate Sub Form_Load()Dim RetVal As Integer Dim Count As Integer Dim ReturnString As StringIf App.PrevInstance = True Then End 'Only permit a single instance.Let Form01.Left = ((Screen.Width - Form01.Width) / 2) 'Centralise the form on screen. Let Form01.Top = ((Screen.Height - Form01.Height) / 2)For Count% = 0 To 7 'Set all channel values to the mid Let Form01.hsbChannelValue(Count).Value = 125 'position ie 1.5ms. Next Count%Form01.cboSetActiveChannels.AddItem "Ch1 Only" 'Set up options in the Active Form01.cboSetActiveChannels.AddItem "Ch1 - Ch2" 'Channels selection and set to Form01.cboSetActiveChannels.AddItem "Ch1 - Ch3" 'default value of Ch1 to Ch4. Form01.cboSetActiveChannels.AddItem "Ch1 - Ch4"Let Form01.cboSetActiveChannels.ListIndex = 3Let PortIsOpen = False 'Initialise any variables.Form01.Show End SubPrivate Sub hsbChannelValue_Change(Index As Integer)Call hsbChannelValue_Scroll(Index%) End SubPrivate Sub hsbChannelValue_Scroll(Index As Integer)Dim Count As IntegerLet Form01.lblChannelValue(Index%).Caption = Format$((((Form01.hsbChannelValue(Index%).Value * 4) + 1000) / 1000), "0.000") Let ChannelValueArray(Index%) = Chr$(Form01.hsbChannelValue(Index%).Value)If PortIsOpen% = True Then 'If Port is Open then check for a While Form01.Comm1.CTSHolding = True: Wend 'transmission in progress and wait Let Form01.Comm1.OutBufferCount = 0 'if so. Purge the Transmit buffer Let Form01.Comm1.InBufferCount = 0 'and Receive buffer. For Count% = 0 To (NumberOfActiveChannels% - 1) 'Load the new set of values Let Form01.Comm1.Output = ChannelValueArray(Count%) 'for the Active Channels only Next Count% 'into the Transmit buffer. End If End Sub
请教高手,如果搞定,马上给分.并不胜感激! //initial com1
m_MSCommCom1.SetCommPort(1);
m_MSCommCom1.SetInputMode(1);
m_MSCommCom1.SetInBufferSize(1024);
m_MSCommCom1.SetOutBufferSize(512);
m_MSCommCom1.SetSettings("19200,n,8,1");
m_MSCommCom1.SetHandshaking(2);
m_MSCommCom1.SetEOFEnable(false);
//m_MSCommCom1.SetCTSHolding(false);
// m_MSCommCom1.SetDSRHolding(false);
// m_MSCommCom1.SetRTSEnable(false);// m_MSCommCom1.SetCTSHolding(true);// m_MSCommCom1.SetDTREnable(true); m_MSCommCom1.SetRThreshold(1);
m_MSCommCom1.SetSThreshold(1);
if( !m_MSCommCom1.GetPortOpen())
{
m_MSCommCom1.SetPortOpen(true);
}
else
{
m_MSCommCom1.SetOutBufferCount(0);
m_MSCommCom1.SetInBufferCount(0);
}
m_MSCommCom1.SetInputLen(0);
m_MSCommCom1.GetInput();void CTestDlg::OnCommMscomm1()
{
// TODO: Add your control notification handler code here
VARIANT varient_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048];
CString strtemp;
int test = m_MSCommCom1.GetCommEvent();
if(m_MSCommCom1.GetCTSHolding())
MessageBox("cts true",MB_OK);
if(m_MSCommCom1.GetDSRHolding())
MessageBox("dsr true",MB_OK);
if(test==1)
MessageBox("发送",MB_OK);
else if(test==2)
{
varient_inp = m_MSCommCom1.GetInput();
safearray_inp = varient_inp;
len = safearray_inp.GetOneDimSize();
for(k=0; k<len; k++)
{
safearray_inp.GetElement( &k,rxdata+k);
}
for(k=0; k<len; k++)
{
BYTE bt = *(char *)(rxdata+k);
strtemp.Format("%c",bt);
m_com1RxData += strtemp;
}
//MessageBox("接到数据",MB_OK);
UpdateData(false);
}
else
{
MessageBox("串口没有消息",MB_OK);
}
{
BYTE bt = *(char *)(rxdata+k);
strtemp.Format("%c",bt);
m_com1RxData += strtemp;
}
这段代码是不对的。
如果接收的是字符串,可以这样写:
strtemp = (LPTSTR)rxdata;
如果接收的是数值,例如double型数值,可以这样写:
strtemp.Format("%Lf", *(double*)rxdata);
的数据,另外commevent却依次出现为2,3,4,5,6,3,4,5,6,3,4,5,6,1
另外,很惭愧的是,我在使用断点调试的时候,经常走到一些类似汇编语言的地方,就走不下去了,请指点以下。感激不尽!
varient_inp = m_MSCommCom1.GetInput()
这个地方读到的数据已经是乱码了,该怎么处理它 呢?
BYTE *p = (BYTE*)&dbVal;
SerialPort.Sent(p, 8);
2、接收端代码:BYTE pRcv[20];
memset(pRcv, 0, sizeof(pRcv));
SerialPort.Recive(pRcv, 8);
double *pdbVal = (double*)pRcv;
ASSERT( fabs(*pdbVal - .1) < 1e-6 );
Option ExplicitDim PortIsOpen As Integer
Dim NumberOfActiveChannels As IntegerDim ChannelValueArray(7) As String * 1Private Sub cboSetActiveChannels_Click() '
Dim Count As Integer
Let Count% = 0 'Set up a channel counter loop
Let NumberOfActiveChannels% = (Form01.cboSetActiveChannels.ListIndex + 1) 'and the No. of Active Chans.'
Do
If Count% < NumberOfActiveChannels% Then 'This loop runs 4 times and
Let Form01.hsbChannelValue(Count%).Enabled = True 'for Active Channels enables
Call hsbChannelValue_Scroll(Count%) 'the corresponding slider and
Else 'updates the displayed value.
Let Form01.hsbChannelValue(Count%).Enabled = False 'For Inactive Channels the
Let Form01.lblChannelValue(Count%).Caption = "" 'slider bar is disabled and
End If 'the displayed value cleared.
Let Count% = (Count% + 1)
Loop Until Count% = 8
End SubPrivate Sub cmdCommPortAction_Click()Dim Count As Integer
Select Case PortIsOpen%
Case False 'COM Port is currently not Open.
On Error GoTo CommErrorHandler 'Set up an error handler. If Form01.optCommPortSelect(0).Value = True Then 'Depending on the users choice first
Let Form01.Comm1.CommPort = 1 'set the appropriate COM Port, 1 or 2.
Else
Let Form01.Comm1.CommPort = 2
End If
Let Form01.Comm1.Settings = "19200,N,8,1" 'The set the transmission parameters,
Let Form01.Comm1.Handshaking = 2 'sets handshaking method to 'RTS/CTS'
Let Form01.Comm1.PortOpen = True 'and attempts to open the port.
Let Form01.Comm1.RTSEnable = True 'If Port Open = OK then power up the
Let Form01.cmdCommPortAction.Caption = "Close Port" 'interface hardware, alter the caption
Let Form01.cmdQuit.Enabled = False 'on the button, disable Quit button,
Let PortIsOpen% = True 'and set the Port State variable. Let Form01.Comm1.InBufferCount = 0 'Because the slider values may have
For Count% = 0 To (NumberOfActiveChannels% - 1) 'changed while the Port was closed an
Let Form01.Comm1.Output = ChannelValueArray(Count%) 'update is performed to ensure the
Next Count% 'hardware reflects the current values.
Exit Sub 'Note the purge of the receive buffer.CommErrorHandler: 'Here if an error occured opening the
Let Form01.cmdCommPortAction.Caption = "Open Port" 'COM Port. Ensure that the button
Let Form01.cmdQuit.Enabled = True 'caption, the Quit button and the
Let PortIsOpen% = False 'Port State variable are correctly
Beep: Beep 'set. Sound to alert the user.
Exit Sub Case True 'Here if the port is already Open so
Let Form01.Comm1.RTSEnable = False 'power down the interface hardware
Let Form01.Comm1.PortOpen = False 'and close it, change the caption on
Let Form01.cmdCommPortAction.Caption = "Open Port" 'the button, re-enable Quit button
Let Form01.cmdQuit.Enabled = True 'stop the Channel Value Write timer
Let PortIsOpen% = False 'and set the Port State variable.
End Select
End SubPrivate Sub cmdQuit_Click()
End
End SubPrivate Sub Form_Load()Dim RetVal As Integer
Dim Count As Integer
Dim ReturnString As StringIf App.PrevInstance = True Then End 'Only permit a single instance.Let Form01.Left = ((Screen.Width - Form01.Width) / 2) 'Centralise the form on screen.
Let Form01.Top = ((Screen.Height - Form01.Height) / 2)For Count% = 0 To 7 'Set all channel values to the mid
Let Form01.hsbChannelValue(Count).Value = 125 'position ie 1.5ms.
Next Count%Form01.cboSetActiveChannels.AddItem "Ch1 Only" 'Set up options in the Active
Form01.cboSetActiveChannels.AddItem "Ch1 - Ch2" 'Channels selection and set to
Form01.cboSetActiveChannels.AddItem "Ch1 - Ch3" 'default value of Ch1 to Ch4.
Form01.cboSetActiveChannels.AddItem "Ch1 - Ch4"Let Form01.cboSetActiveChannels.ListIndex = 3Let PortIsOpen = False 'Initialise any variables.Form01.Show
End SubPrivate Sub hsbChannelValue_Change(Index As Integer)Call hsbChannelValue_Scroll(Index%)
End SubPrivate Sub hsbChannelValue_Scroll(Index As Integer)Dim Count As IntegerLet Form01.lblChannelValue(Index%).Caption = Format$((((Form01.hsbChannelValue(Index%).Value * 4) + 1000) / 1000), "0.000")
Let ChannelValueArray(Index%) = Chr$(Form01.hsbChannelValue(Index%).Value)If PortIsOpen% = True Then 'If Port is Open then check for a
While Form01.Comm1.CTSHolding = True: Wend 'transmission in progress and wait
Let Form01.Comm1.OutBufferCount = 0 'if so. Purge the Transmit buffer
Let Form01.Comm1.InBufferCount = 0 'and Receive buffer.
For Count% = 0 To (NumberOfActiveChannels% - 1) 'Load the new set of values
Let Form01.Comm1.Output = ChannelValueArray(Count%) 'for the Active Channels only
Next Count% 'into the Transmit buffer.
End If
End Sub
请教下面这段代码什么意思,为什么status 的值只是0 或者3 ,而且大多都是3。 如果我想在下位机上面接受上位机器传出来的数据。应该怎么设置下位机的串口属性。 另外在上位机上是多线程的发送,在下位机上一定也要用多线程的接收吗?
while(1)
{
int status = 0;
while (!(status & TIOCM_CTS)) {
ioctl(fd_ser, TIOCMGET, &status);
} if ( write(fd_ser, intdata,4) < 0)
SetAsyncSendIconError (errno, SendID);
else
SetAsyncSendIconError (0, SendID);
tcdrain(fd_ser);
status=0;
}
串口要按照上位程序中的"19200,N,8,1"来设置,波特率19200,偶校验,8数据位,1停止位。
下位机不用多线程。
我的意思是你自己做两个程序,一个上位机和一个下位机。既然用MsComm控件来做,在控件属性上面设置一下,两个程序设置一样就可以了。
我的意思是你自己做两个程序,一个上位机和一个下位机。既然用MsComm控件来做,在控件属性上面设置一下,两个程序设置一样就可以了
============
.