First time using serial port: help for receiving data and manage serial port bahaviour |
|
Author |
Message |
Greenstrike

|
Posted: .NET Base Class Library, First time using serial port: help for receiving data and manage serial port bahaviour |
Top |
Hi everybody! First I apologize for my english, then I apologize if I make stupid questions, but it's few days I started using Visual Basic. I'm trying to make an application using serial port: I need to send from an external device data and text to PC and I have to distinguish between data and text: if I send a Byte 30h to the PC, how can I tell to my application if it's data (30h) or text (30h = "0") I was thinking to using CTS, DTS, CD, etc. etc. pins to tell PC if it's data or text and I've seen in this thread http://www.hide-link.com/ ;SiteID=1 these pins have to be enabled; does enabling one of these pins auto-enables the handshake for serial port How can I clean the ReceivedData buffer How can I read the ReceivedData buffer I mean, if I read it as a string, if it's containing 31h 20h 00h 33h bytes, the string ends with 00h byte, but I need to read also 33h: how can I read the ReceivedData buffer as an array of bytes Can anyone help me Thanks.
.NET Development4
|
|
|
|
 |
nobugz

|
Posted: .NET Base Class Library, First time using serial port: help for receiving data and manage serial port bahaviour |
Top |
You cannot effectively distinguish data from text by looking at the byte values. Text is encoded as bytes too, just like data. Instead, you need to impose a structure on the data that is sent through the port. The typical technique is to start with a unique byte value to indicate the start of a record, like 01h. Then, by convention, you'll say that the 2nd and 3rd bytes are data, 4th is text, etc. Similar to the way you would declare a Structure.
|
|
|
|
 |
Greenstrike

|
Posted: .NET Base Class Library, First time using serial port: help for receiving data and manage serial port bahaviour |
Top |
Ok, thanks, I'll try. Just a little question: if I use Temp as Byte, in debug mode, if I put the cursor on Temp, it's shown its value, but in decimal (if Temp is &H10, it's shown 16 value): how can I show the value in exadecimal mode
|
|
|
|
 |
nobugz

|
Posted: .NET Base Class Library, First time using serial port: help for receiving data and manage serial port bahaviour |
Top |
Right-click a value shown in the Locals or Watch or Quick Watch window and choose "Hexadecimal Display".
|
|
|
|
 |
IkeConn

|
Posted: .NET Base Class Library, First time using serial port: help for receiving data and manage serial port bahaviour |
Top |
Hello,
I just did something like what you are looking for. What I did was create a form in VB, drop a serialport on it, and this code reads a 13 byte Hex string and returns ascii text: Maybe you can alter it and get closer to what you need. I attempted to use the Read method with an array of type byte but didn't need it and used the ReadExisting method instead. I will bet that you will require it to be sure you read the hex bytes as hex bytes. You declare an array with code like this: Dim buffer(12) As Byte and you'll use the read method something like this: SerialPort1.Read(buffer, 0, 13) but I'm a newbie also and don't yet know the syntax for the array or Read method.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
usePort()
End Sub
Public Sub OnDataReceived(ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
grabData()
End Sub
Public Sub grabData()
Dim comportData
comportData = " "
Try
Do Until SerialPort1.BytesToRead >= 13
'nothing until the buffer gets all 13 bytes
'if you'll notice I'm running at 300 baud and the OnDataReceived event fires before the buffer gets all 13 bytes
'if you are running at a much faster baud rate you probably won't need this little do...loop.
Loop
If SerialPort1.BytesToRead >= 13 Then 'redundant test in this example, needs reworking only if you use the do...loop
comportData = SerialPort1.ReadExisting() 'this grabs the data from the serial port
closePort()
'SerialPort1.DiscardInBuffer()
'SerialPort1.Write("+++ath" & vbCrLf) 'doesn't work, use closePort for now
findSnum(comportData) 'this is where the action really starts comportData holds the data from the buffer
Else
resetPort()
End If
Catch ex As Exception
resetPort()
End Try
End Sub
Public Sub usePort() 'this sets up the com port
Try
With SerialPort1
.PortName = "Com1"
.BaudRate = 300 'this MUST match baud rate of the modem!!!!!
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = 1
'.ReadBufferSize = 14 'ignored for any value less than 4096
.ReadTimeout = 100
.ReceivedBytesThreshold = 1 'number of bytes required to fire a DataReceived event
.Handshake = IO.Ports.Handshake.None
'.Handshake = IO.Ports.Handshake.RequestToSend
.RtsEnable = True
.DtrEnable = True
'.Encoding = System.Text.Encoding.ASCII
.Open()
End With
Catch ex As Exception
resetPort()
End Try
End Sub
Public Sub closePort()
SerialPort1.Close() 'this closes the port which puts the modem on hook
SerialPort1.Dispose() 'this disposes of "SerialPort1" and frees resources
End Sub
Public Sub resetPort()
SerialPort1.Close() 'this closes the port which puts the modem on hook
SerialPort1.Dispose() 'this disposes of "SerialPort1" and frees resources
usePort()
End Sub
The hex stream I'm sending is ff 30 31 32 33 34 35 36 37 38 39 31 0d which comes out of
comportData = SerialPort1.ReadExisting()
as 1234567891[]
where [] is a small box.
Now you'll need to know what MSDN says about the Open and Close methods:
The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly.
Make sure you don't have any data in your inbound or outbound buffer before you start calling openPort/closePort openPort/resetPort or vice versa or you could get an UnauthorizedAccessException
Hope this helps,
Ike
|
|
|
|
 |
Greenstrike

|
Posted: .NET Base Class Library, First time using serial port: help for receiving data and manage serial port bahaviour |
Top |
Ok, thanks for help, I solved my problem. But now I have a new one: how can I detect serial port is receiving I
mean I would like to have two indicators (checkbox or button or
anithing else, I don't know) changing color while receiving or
transmitting (one for RX, one for TX). How can I detect "now I'm
receiving", then i=1, now for 100ms I'm not receiving then i=0, now
I'm receiving again, then i = 1 again, etc. etc.
|
|
|
|
 |
|
|