Some Basic Serial Port Questions
I have a wrap around plug on COM1 for testing
1) Do I need two programs -- one to send and one to receive running at the same time in order to see if the port received data?
2) If i use the same program -- say a receive form for testing --
if I open the port from the receive side first will the transmit side error because the port is open?
3) If I need a second receiving program, since the port will be open for transmit, my guess is the receiving program will error on port open, hence I need to bypass opening that port.
Re: Some Basic Serial Port Questions
No you don't need a second program.
Only one program can open the port at a time.
Yes you will get an error if you try to open a port that is already open.
A port handles two way communications. If you are looping back the data then the same program, the same port, the same com control that sent the data will receive the data
Re: Some Basic Serial Port Questions
DataMiser: Thanks. However not using MSComm, trying to do a API implementation but your comments help clarify with wrap plug.
One area I'm struggling with is the receive side. Found this on MSDN (my VB6 code on top, original C++ code on bottom) but NOT sure how to translate the &Byte from C++ to VB6 in this statement:
Code:
' ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0);
Edit: Went ahead and did this for &Byte
Dim ThisByte as Byte
Now failing on both WaitCommEvent and ReadFile
last param where need pointer to OVERLAPPED structure.
My attempt from C++ to VB6
Code:
Public Function RxMicrosoft(hPort As Integer)
Dim ThisByte As Byte
Dim lngCommModemState As Long
Dim lngBytesTransferred As Long
Dim arrByte() As Byte
'Specify a set of events to be monitored for the port.
Call SetCommMask(hPort, EV_RXCHAR Or EV_CTS Or EV_DSR Or EV_RLSD Or EV_RING)
While (Not hPort = INVALID_HANDLE_VALUE)
'Wait for an event to occur for the port.
Call WaitCommEvent(hPort, lngCommModemState, 0)
'Re-specify the set of events to be monitored for the port.
Call SetCommMask(hPort, EV_RXCHAR Or EV_CTS Or EV_DSR Or EV_RING)
If (lngCommModemState & EV_RXCHAR) Then
'Loop for waiting for the data.
Do
'Read the data from the serial port.
ReadFile (hPort, ThisByte, 1, lngBytesTransferred, 0)
'Display the data read.
If dwBytesTransferred = 1 Then
Call ProcessChar (ThisByte)
End If
Loop While lngBytesTransferred = 1
End If
Wend
'===========================================
'BYTE Byte;
'DWORD dwBytesTransferred;
'
'// Specify a set of events to be monitored for the port.
'SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);
'
'while (hPort != INVALID_HANDLE_VALUE)
'{
' // Wait for an event to occur for the port.
' WaitCommEvent (hPort, &dwCommModemStatus, 0);
'
' // Re-specify the set of events to be monitored for the port.
' SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RING);
'
' if (dwCommModemStatus & EV_RXCHAR)
' {
' // Loop for waiting for the data.
' Do
' {
' // Read the data from the serial port.
' ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0);
'
' // Display the data read.
' if (dwBytesTransferred == 1)
' ProcessChar (Byte);
'
' } while (dwBytesTransferred == 1);
' }
End Function