serial communication.....send/recieive
serial communication.....send/recieive
serial communication.....send/recieive
serial communication.....send/recieive
serial communication.....send/recieive
serial communication.....send/recieive serial communication.....send/recieive serial communication.....send/recieive serial communication.....send/recieive serial communication.....send/recieive serial communication.....send/recieive serial communication.....send/recieive serial communication.....send/recieive
serial communication.....send/recieive serial communication.....send/recieive
serial communication.....send/recieive
Go Back  Xtreme Visual Basic Talk > > > serial communication.....send/recieive


Reply
 
Thread Tools Display Modes
  #1  
Old 06-28-2006, 07:42 AM
sting888316 sting888316 is offline
Newcomer
 
Join Date: Jun 2006
Posts: 5
Default serial communication.....send/recieive


I have to do this project where I send 0xFF to any port (that is chosen by the user). Then I have to loop until I receive a 0x3C back from the device on that port. I haven't done visual basic for a few years now and even when I did, I didn't do anything with serial communication so I dont really know what I am doing. It doesnt seem to me like it would be this hard to do but I've been messing with it for a few days now and really can't figure it out. Here is a sample of just trying to send 0xFF to the port:

SerialPort1.PortName = ComPort1.SelectedItem
SerialPort1.Open()
'send signal
If ComPort1.SelectedItem = Nothing Then
MsgBox("Please Choose a COM Port")
Else
SerialPort1.Write(Hex(255))
End If

Any help would be appreciated. I dont know if I'm way off or anything. Right now when I run the program and hit the send button I am getting some "Port access denied error." Another thing Im not sure about is how to send data over in hexadecimal. Thanks for your help.
Reply With Quote
  #2  
Old 06-28-2006, 10:27 AM
b0b b0b is offline
Contributor
 
Join Date: Jul 2003
Posts: 741
Default

Dont know much about Serial Ports but i do know the Hex() function returns a string, So you are actually trying to write "FF". Just pass the write method the actual value you need i.e. 255.

Its sounds from your error that the port is not actually open though. What line do you get the error on?
Reply With Quote
  #3  
Old 06-28-2006, 11:40 AM
sting888316 sting888316 is offline
Newcomer
 
Join Date: Jun 2006
Posts: 5
Default

i actually figured it out....the error was on the SerialPort1.Open (). I still need to figure out how to receive data from the device now
Reply With Quote
  #4  
Old 06-29-2006, 01:10 AM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

Hi,

For the sake of others who may read this thread. There are three errors in the code posted:
Code:
SerialPort1.PortName = ComPort1.SelectedItem SerialPort1.Open() 'send signal If ComPort1.SelectedItem = Nothing Then MsgBox("Please Choose a COM Port") Else SerialPort1.Write(Hex(255)) End If
(1) SerialPort1.Open() - the () should not be there
(2) As pointed out by bOb, Hex(255) returns the characters FF as opposed to Chr(255) which returns a character representation of the value 255.
(3) The MSComm control does not have a .Write property or method (SerialPort1.Output should be used to send data)

There are two ways of receiving data from the Serial port, polling and event driven.
eg Polling
Code:
SerialPort1.Output Chr(255) strReceived = "" Do Until len(strReceived) = 1 strReceived = SerialPort1.Input DoEvents Loop If mid(strReceived,1,1) = Chr(&H3C) Then ' ' Expected Response received ' Else ' ' Unexpected Response ' End If
Here the code loops until a character is received
eg Event Driven (Template taken from http://msdn.microsoft.com/library/en...comm_commx.asp)
Code:
SerialPort1.Output Chr(255) End Sub Private Sub SerialPort1_OnComm () Dim strReceived as String Select Case SerialPort1.CommEvent ' ' Handle each event or error by placing ' code below each case statement ' Errors Case comEventBreak ' A Break was received. Case comEventFrame ' Framing Error Case comEventOverrun ' Data Lost. Case comEventRxOver ' Receive buffer overflow. Case comEventRxParity ' Parity Error. Case comEventTxFull ' Transmit buffer full. Case comEventDCB ' Unexpected error retrieving DCB] ' Events Case comEvCD ' Change in the CD line. Case comEvCTS ' Change in the CTS line. Case comEvDSR ' Change in the DSR line. Case comEvRing ' Change in the Ring Indicator. Case comEvReceive ' Received RThreshold # of ' chars. strReceived = SerialPort1.Input if Mid(strReceived,1,1) = Chr(&H3C) Then ' ' Expected Result ' Else ' ' Unexpected Result ' End If Case comEvSend ' There are SThreshold number of ' characters in the transmit ' buffer. Case comEvEof ' An EOF charater was found in ' the input stream End Select End Sub
Here the code idles until a character is received.

In order to ensure that the OnComm event triggers it is necessary to have set RThreshold to a value greater than zero. (RThreshold sets and returns the number of characters to receive before the MSComm control sets the CommEvent property to comEvReceive and generates the OnComm event.See:http://msdn.microsoft.com/library/en...thres_comm.asp

Regards
Doug
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #5  
Old 06-29-2006, 06:15 AM
mdent mdent is offline
Centurion
 
Join Date: Jun 2006
Location: Teesside, UK
Posts: 108
Default Hex send...

Hi, had a similar problem recently. You are not sending the Hex commands correctly - I think someone has allready said, HEX() is just a string representation. You actually need to do the following and send as a byte:

Code:
            

            Dim myHex() As Byte = New Byte() {&HFF}
            myComPort.Write(myHex)
note that vb uses &H and not 0x like c++ does. Also if you want to send other values, you can do

New Byte() {&HFF, &HC0, &H08} etc.

presumably you then just have to put this is a while loop and then use the Read command with the rs232.vb class. Hope this helps
Reply With Quote
  #6  
Old 06-30-2006, 09:38 PM
MathGeek MathGeek is offline
Newcomer
 
Join Date: Jun 2006
Posts: 9
Default

Quote:
Originally Posted by DougT
Hi,


(3) The MSComm control does not have a .Write property or method (SerialPort1.Output should be used to send data)
In my humble opion, we might have an misunderstanding here.

OP is using SerialPort, not MSComm. MSComm is for older VB.

SerialPort is used in VB 2005. "Write" is perfectly legitimate.
Reply With Quote
  #7  
Old 07-01-2006, 12:08 AM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

Thanks for that, perhaps if Poster had posted in the correct forum the misunderstanding wouldn't have arisen
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #8  
Old 07-01-2006, 12:15 AM
MikeJ's Avatar
MikeJserial communication.....send/recieive MikeJ is offline
Retread

Retired Moderator
* Expert *
 
Join Date: Sep 2002
Location: Austin, Texas
Posts: 6,747
Default

And if the original poster had used .NET functions like MessageBox.Show() rather than MsgBox, the confusion could have been avoided all together!
__________________
{ Lex Fori } { Locus Classicus } { Rutilus Scrinium }
Osculare pultem meam!
Reply With Quote
  #9  
Old 07-03-2006, 10:36 AM
MathGeek MathGeek is offline
Newcomer
 
Join Date: Jun 2006
Posts: 9
Default

Quote:
Originally Posted by mdent
Hi, had a similar problem recently. You are not sending the Hex commands correctly - I think someone has allready said, HEX() is just a string representation. You actually need to do the following and send as a byte:

Code:
            

            Dim myHex() As Byte = New Byte() {&HFF}
            myComPort.Write(myHex)
note that vb uses &H and not 0x like c++ does. Also if you want to send other values, you can do

New Byte() {&HFF, &HC0, &H08} etc.

presumably you then just have to put this is a while loop and then use the Read command with the rs232.vb class. Hope this helps
Wouldn't this code do implicit conversion from Byte to String? Write() only takes String value.

If so, wouldn't the output from the serial port be "FF" as a string?

also, how do you express a binary number?

Last edited by MathGeek; 07-03-2006 at 10:47 AM.
Reply With Quote
  #10  
Old 07-04-2006, 07:44 AM
mdent mdent is offline
Centurion
 
Join Date: Jun 2006
Location: Teesside, UK
Posts: 108
Default

Quote:
Originally Posted by MathGeek
Wouldn't this code do implicit conversion from Byte to String? Write() only takes String value.

If so, wouldn't the output from the serial port be "FF" as a string?

also, how do you express a binary number?
Nope, I can honestly say through testing that it does not just return the string "FF", it returns the Hex &HFF or the binary 11111111. Try it and you will see that it works. Incidentally, you can also use binary with this method by doing

dim myHex() As Byte = New Byte() {11111111}

although this is a roundabout method.
Reply With Quote
  #11  
Old 07-04-2006, 09:45 AM
MathGeek MathGeek is offline
Newcomer
 
Join Date: Jun 2006
Posts: 9
Default

Quote:
Originally Posted by mdent
Nope, I can honestly say through testing that it does not just return the string "FF", it returns the Hex &HFF or the binary 11111111. Try it and you will see that it works. Incidentally, you can also use binary with this method by doing

dim myHex() As Byte = New Byte() {11111111}

although this is a roundabout method.
Hmm, I thought Write method takes String value only UNLESS I put in Byte array with integer off set and integer count. (So three arguments total).


I will try your method.

Edit: If I try your method, I get the following error (screen shot attached). Look at the bottom window in Visual Studio IDE. Something I am missing?
Attached Images
File Type: gif errorVB.GIF (79.9 KB, 20 views)

Last edited by MathGeek; 07-04-2006 at 09:54 AM.
Reply With Quote
  #12  
Old 07-05-2006, 04:53 AM
mdent mdent is offline
Centurion
 
Join Date: Jun 2006
Location: Teesside, UK
Posts: 108
Default

Let me first say that I was talking nonsense about sending the binary with this method, it will just send it as an integer.

Secondly, I am completely stumped as I use the exact same method in my code and it works. Perhaps you don't have the same rs232.vb as I have? Or perhaps it is because it is a different version of VS (I'm grasping at straws here). Just as a reminder, I use

Code:
        myComPort.Open(1, 9600, 8, Rs232.DataParity.Parity_None, Rs232.DataStopBit.StopBit_1, 4096)
        Dim myHex() As Byte = New Byte() {&HC0, &H8} (or &HFF works)
        myComPort.Write(myHex)
in your rs232.vb class, do you have the following:

Code:
Public Overloads Sub Write(ByVal Buffer As Byte())
this is what should let you write as a byte array with the write method. When you open the bracket after the write command, it should come up with the option write(buffer() as byte) and/or write(buffer as string).

Now I have a simple question: when you use the read command with the rs232.vb class, how do you get the text contained? do you just use

Code:
text = myComPort.Read(64)
?
Reply With Quote
  #13  
Old 07-05-2006, 03:29 PM
kski kski is offline
Newcomer
 
Join Date: Jun 2006
Posts: 4
Default

i get an error on the serialport.write argument when i try to send data using the hex method that's listed here

value of type '1-dimensional array of Byte' cannont be converted to 'String'.
Reply With Quote
  #14  
Old 07-05-2006, 04:22 PM
MathGeek MathGeek is offline
Newcomer
 
Join Date: Jun 2006
Posts: 9
Default

Quote:
Originally Posted by kski
i get an error on the serialport.write argument when i try to send data using the hex method that's listed here

value of type '1-dimensional array of Byte' cannont be converted to 'String'.
You are perfectly normal.

Try byte array instead.

Right now, I am controlling mini RC with transmitter hooked up to RS232. Fun
Reply With Quote
  #15  
Old 07-05-2006, 08:20 PM
kski kski is offline
Newcomer
 
Join Date: Jun 2006
Posts: 4
Default

what? what code did you change?
Reply With Quote
  #16  
Old 07-05-2006, 08:35 PM
MathGeek MathGeek is offline
Newcomer
 
Join Date: Jun 2006
Posts: 9
Default

Quote:
Originally Posted by kski
what? what code did you change?
Code:
Dim DataBye() As Byte = {&HFF, &HDD}

SerialPort1.Write(Databyte, 0, 2)

This code will send FF and DD hex. You have to have SerialPort1 control.

"0" is the starting point and "2" is the number of bytes you are sending.

Hope that helps.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
serial communication.....send/recieive
serial communication.....send/recieive
serial communication.....send/recieive serial communication.....send/recieive
serial communication.....send/recieive
serial communication.....send/recieive
serial communication.....send/recieive serial communication.....send/recieive serial communication.....send/recieive serial communication.....send/recieive serial communication.....send/recieive serial communication.....send/recieive serial communication.....send/recieive
serial communication.....send/recieive
serial communication.....send/recieive
 
serial communication.....send/recieive
serial communication.....send/recieive
 
-->