ah3

Hi...I am a noob...first and foremost. I am doing a school project in which I am using motors that run on RS232. My com2.writeline code works fine, however I am stuck on reading back on the port. Here is what I want to do:

When the motor runs, it sets a bit saying it is in motion (RBo). I want to poll that bit until the motor turns off. I can do this by looking at the specific bit as a 1 or a 0. Or, I can look at the status Byte and go from there. I am completely new to doing this, and I would really appreciate any com port samples/examples that implement this, or a good code critique...again I am a noob. :)

Here is my code so far:

com2.WriteLine(Chr(130) + "WAKE ")

com2.WriteLine("A=" + Acceleration2.Text + " ")

com2.WriteLine("V=" + Velocity2.Text + " ")

com2.WriteLine("P=" + d2 + Position2.Text + " ")

com2.WriteLine("G ")

Do

com2.WriteLine("RBo ")

incomingdata = com2.ReadChar()

'If IncomingData Is Nothing Then

bytetest.Text = IncomingData

If IncomingData = "49" Then

Exit Do

End If

Loop

Thanks!!



Re: Visual Basic Express Edition Com port received data HELP

ah3

Here is my latest experiment...

Doing this method on a button click (without a do loop), I get the results I want. But when I perform the following code (with the do loop) I get IndexOutOfRangeException was unhandled errors. Am I on a right track Any ideas/comments/feedback Thanks!!

If testloop.Checked = True Then

com2.WriteLine(Chr(129) + "WAKE ") 'wake motor

com2.WriteLine("A=100" + " ") 'set accel

com2.WriteLine("V=10000" + " ") 'set velocity

com2.WriteLine("P=10000" + " ") 'set position (+ or -)

com2.WriteLine("G ") 'GO

'My.Computer.Ports.OpenSerialPort("COM2", 9600, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)

com2.Encoding = System.Text.Encoding.Default

Do

com2.WriteLine("RS ")

'System.Threading.Thread.Sleep(100)

Dim incomingdata As String = com2.ReadLine(3) 'Hangs Here...since Echo is on, it reads the 4th character in the string "R" "S" " " "bit as 1 or 0".

If incomingdata Is Nothing Then

Exit Do

End If

bytetest.Text = incomingdata

Loop

End If





Re: Visual Basic Express Edition Com port received data HELP

Carsten Kanstrup

First code:

ReadChar returns one character, but you compare it with a string consisting of two characters ( "49" ). If you want to test if the data is ASCII 1, you must either compare it with "1" or with Chr(49).

The goto style programming is not very beautiful or easy to read. Why not test in the Loop statement instead like:

Do

Loop Until IncommingData = "1"

Besides, if the motor does not send a "1", the code will loop forever as there is no timeout. This is not good programming practice either.

Second code:

Where does (3) after ReadLine ( com2.ReadLine(3) ) comes from This method does not have any arguments as far as I know. ReadLine returns when the character specified in the NewLine property is detected (default LF) so what do you mean with: "R" "S" " " "bit as 1 or 0".

If you want to read a specified number of characters you can use the Read method, which reads a specified number of characters into a char array with a specified offset.

Dim instance As SerialPort
Dim buffer As < XML:NAMESPACE PREFIX = MSHelp NS = "http://msdn.microsoft.com/mshelp" />Char()
Dim offset As Integer
Dim count As Integer
Dim returnValue As Integer

returnValue = instance.Read(buffer, offset, count)

Don't use Sleep on programs, which runs on the UI (User Interface) thread like your program - your comment indicate that you have tried this. It blocks the entire UI for the specified amount of time.

If you want to wait for something or do something asynchronous there are much better ways. In the knowledgebase on our homepage we have a small program for serial port communication complete with source code and a very detailed description. The description not only describes the program, but also describes multithreading, events etc. in a language, which even beginners should have a chance of understanding (if you don't, let me know). The URL is:

http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm

Good luck with the project. Maybe we will see you in the automation business one day.

Innovatic

Carsten Kanstrup

 

 





Re: Visual Basic Express Edition Com port received data HELP

ah3

On the motors, I have echo turned on for addressing purposes (its multiaxis). Because of the echo, I am assuming now that when I send out a "RBt " command (with a space delimitter), I will receive "RBt 0" Because I only care about the 0, (or 1), by using the com2.readline(4) it returned for me the 4th element in that array (the 0 or 1), but the looping seems to throw it off into error state. So, by using

returnValue = instance.Read(buffer, offset, count)

dim incoming data as string = com2.read(buffer,0,4) would return 4 bytes of data, essentially "RBt 0"

So then for me to read the 0 only, would I do a com2.read(buffer,4,1) (4th element, 1 byte read)

What syntax goes in place of buffer

I had the sleep in there because I wasn't sure if I was reading it back too fast...

Thank you!





Re: Visual Basic Express Edition Com port received data HELP

Carsten Kanstrup

Using Read is not the easiest way.

When you send the string by means of  

com2.WriteLine("RS ")

VB will add a NewLine character (default LF) after "RS ". The whole string (including the added NewLine character) will of course be echoed back, so you only need to call Com2.ReadLine twice. The first string you receive is "RS " from your own transmission. The second string is the answer from your motor. You can then combine the two strings by means of "&" (although I will not recommend such string operations in .NET).

Note. ReadLine removes the NewLine character again.

Note. ReadLine only works if all telegrams are terminated with a known character (NewLine), so if this is not the case for the telegrams from the motor, this method cannot be used! This is probably the reason why this method hangs! In that case, you can just replace the second ReadLine call with e.g. ReadByte or ReadChar.

You can also use Com2.Read(....) with a code like this:

Dim ReceiveBuffer As Char()

Dim NumberOfBytesReceived As Integer = Com2.Read(ReceiveBuffer, 0, 5)

where 5 is the number of characters to receive. Remember that WriteLine inserts a NewLine character! You must then check if the number of returned bytes is as expected (not guaranteed). This is what the return value is used for.

You can then convert the array to a string by means of:

Dim ReceivedString As New String(ReceiveBuffer, 0, NumberOfBytesReceived)

Note. You cannot just receive one character in the middle of a telegram. You can only receive all characters and then pick out the one you want in the receive array like:

Dim TheOneThatIWant As Char = ReceiveBuffer(4)

 





Re: Visual Basic Express Edition Com port received data HELP

ah3

Thank you, I will try that!