Picaxe 20M2 and VB.Net serial comms

JSDL

Senior Member
Hi everyone, I am trying to create a simple program in VB.net to toggle an LED on and OFF but am running into some trouble. I am using a serial to TTL serial breakout board from Ebay to connect to the PC's serial port. Here is the code:

Code:
#Picaxe 08M2
SetFreq M32


HSerSetup B9600_32, %000
Do
  b1 = 1
  Do
    HSerIn w0
  Loop Until b1 = 0
  Select Case b0
    Case "1" : High c.4
    Case "0" : Low  c.4
  End Select
Loop
here is the VB.net code:

Code:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class frm_SerialComms
    Dim WithEvents BLUETOOTH As SerialPort = New System.IO.Ports.SerialPort("COM6", 9600, Parity.None, 8, StopBits.One)

    Private Sub btnLEDON_Click(sender As Object, e As EventArgs) Handles btnLEDON.Click
        If Not BLUETOOTH.IsOpen = True Then
            BLUETOOTH.Open()
        End If
        BLUETOOTH.Write("1")
    End Sub

    Private Sub btnLEDOFF_Click(sender As Object, e As EventArgs) Handles btnLEDOFF.Click
        If Not BLUETOOTH.IsOpen = True Then
            BLUETOOTH.Open()
        End If
        BLUETOOTH.Write("0")
    End Sub
    
End Class
The problem is I have to hit the button multiple times in my VB.net program for the byte to be recognized and therefore the LED to toggle. It is inconsistent, sometime it works right away, other times I have to push the button multiple times, so it seems to me to be some sort of timing issue. Any advice would be greatly appreciated. Thanks in advance.
 

Rick100

Senior Member
Hi everyone, I am trying to create a simple program in VB.net to toggle an LED on and OFF but am running into some trouble. I am using a serial to TTL serial breakout board from Ebay to connect to the PC's serial port..
What voltage do you measure at the serial input pin on the Picaxe? It should idle high. If it's low you'll need an logic level inverter. You could also try using a terminal program instead of your vb.net program to isolate the problem.

From manual 2 on hsersetup:

Polarity:
When bit1 is 0, the serial output polarity is ‘True’ which is same as a ‘Txxx’ baudrate in the ‘serout’ command. In this state the pin idles high and pulses low. This is the state normally used with a MAX232 type inverter for computer connection.

On some parts the hardware serial input polarity is always true, it cannot be inverted (ie bit 2 serial input inversion only applies to X2 parts). This is a limitation of the internal microcontroller structure. Therefore a MAX232 type inverter is required for computer connections.
Good luck,
Rick
 
Last edited:

inglewoodpete

Senior Member
You need to isolate where the problem is occurring.

Using the same hardware, configure the PE's terminal program for the same port, speed etc. When you then manually send a "0" or a "1" from the terminal program, does the PICAXE respond correctly?

Also, I have some reservations about using a word register to receive a single byte from the asynch port. Does it produce a reliable result?
 

Rick100

Senior Member
It's also possible the pc is sending multiple characters for "0" for "1". Try adding the following line to your program.

myComPort.Encoding = System.Text.Encoding.UTF8

In my vb.net programs I've sent the data as bytes so you might try converting "0" and "1" to byte variables before sending them.

Good luck,
Rick
 

Goeytex

Senior Member
In this application there is little to no advantage of using hardware HSERIN vs software SERIN.

To isolate the problem, I would suggest you use serin to confirm that the VB application is working as expected. Try the code below to verify. If this works, then the problem is with the HSERIN code. If it does not work then in the off chance that the serial polarity is inverted, try changing serin to N9600_32. If it still does not work then the problem is most likely with the data being sent by the VB application.

Code:
#Picaxe 08M2
SetFreq M32

Do
     Serin C.1, T9600_32, B0
    
     Select Case b0
         Case "1" : High c.4
         Case "0" : Low  c.4
     End Select
     pause 200 
Loop
 

hippy

Technical Support
Staff member
Also, I have some reservations about using a word register to receive a single byte from the asynch port. Does it produce a reliable result?
That is the best practice for using HSERIN on an M2, allowing the PICAXE to receive all 8-bit byte values 0 through 255.
 

Technical

Technical Support
Staff member
We suspect the problem is with your .net code.

When you have this type of problem send the byte out using PICAXE serial terminal within PE6, that will confirm the PICAXE side of things is working and that the error is in your vb code.

In general in VB you should not keep opening/closing the port on each button click. Keep it open whilst you are clicking the buttons. We also recommend that it is generally better to use bytes rather than strings with this type of work.

Add this line before opening the port - this is necessary as encoding 28591 happens to be the (only) Windows encoding which maps all characters as 8 bit numbers as desired in this type of work (so 0-255 all work as expected)

BLUETOOTH.Encoding = System.Text.Encoding.GetEncoding(28591)
 

grim_reaper

Senior Member
I was about to say I agreed with all the answers given, but it turns out I don't :D

Technical - JDSL isn't opening and closing the port on each click; he's only opening it if it's not already open. I agree that it's not exactly a professional looking bit of script, but for testing like this the construction is fine. I agree with setting the encoding to 28591, although he's not using data over 127 (currently) and there's no guarantee he's using Windows!
I definitely agree that bytes should be used rather than strings (which alleviates the whole encoding problem anyway!).

While I haven't got the stuff with me to try this, I think there may well be nothing wrong with the whole setup - aside from it running too fast? Gooey's code is definitely closer to what should be used for testing this.
 

JSDL

Senior Member
Thanks everyone for the prompt replies, will try these suggestions at school tomorrow and report back.

Hippy, can you explain why I need to use a word variable
That is the best practice for using HSERIN on an M2, allowing the PICAXE to receive all 8-bit byte values 0 through 255.
to receive all 8 bit byte values instead of just a byte variable? Couldn't I do the same with just b0 for example?
 

hippy

Technical Support
Staff member
If all 256 values of a byte are to be received (0-255) there is no value which can be used to indicate HSERIN did not receive anything. By using a word value, that can be set to something greater than 255 which indicates no new data, then 0-255 indicates new data of that value.
 

JSDL

Senior Member
OK so I tried all the above suggestions, and the problem still exists. When I output the values being transmitted from the Picaxe to the MAX3232 in the serial monitor, I do get the values expected sometimes, but I have to keep clicking the button in my VB program for the Picaxe to receive the proper byte. Eventually the LED will toggle ON/OFF but the amount of time it takes to react is random. I tried the same VB code with a USB to TTL serial adapter (PL2303HX) and it works perfectly. I don't understand why it wouldn't work with the MAX3232? Any ideas?
 

inglewoodpete

Senior Member
What happens when you loop the Tx to Rx pins (on the TTL side of the 2323 chip)? Does the MAX2323 correctly echo the characters you think you're sending?
 

Technical

Technical Support
Staff member
You need to be a bit clearer about exactly what hardware you are using in each case and how the wiring is setup.
Also why the BLUETOOTH name in your code - are you also using a bluetooth COM port?

A full prolific USB<>serial adapter is not necessarily going to give the same polarity as a MAX3232 breakout connected to a traditional serial port.
Check with a voltmeter between 0V and the PICAXE input that the serial idle state signal is the same polarity. Is it idle high or idle low in each scenario? A simple loopback test may work with either polarity, but just because a loop back passes does not mean it is the correct polarity as required by the hardware.
 

JSDL

Senior Member
ok, I am using a Picaxe 08M2 for the serial comms. I have a Max3232 breakout board (http://www.ebay.ca/itm/RS232-To-TTL-Converter-Module-COM-Serial-Board-MAX232CSE-Transfer-Chip-atmega16-/181006995656?pt=LH_DefaultDomain_0&hash=item2a24db90c8) with power and ground connected to a 5V supply (same as Picaxe), and the TX pin connected to pin c.1 on the 08M2. I am using the serin command on C.1 to receive the bytes.

The loopback test works fine, when I short TX and RX on the above mentioned breakout board, I get an echo of the characters back on the computer screen, however when using my controlling VB program posted earlier, there is a delay in toggling the LED on/off. The code I am now using (updated from original post) on the Picaxe end is:

Code:
#Picaxe 08M2
SetFreq M32


Do
     Serin C.1, T9600_32, B0
    
     Select Case b0
         Case "1" : High c.4
         Case "0" : Low  c.4
     End Select
     pause 200 
Loop
 

Technical

Technical Support
Staff member
And what happens if you forget about your vb.net program completely for now and just send a "0" or "1" (remember to use the speech marks in raw mode, or just select ascii mode and type without) out of the PICAXE serial terminal within PE6? Remember to match the baud rates.

Such a cheap ebay module may also have a 'fake' Maxim part on it, there are lots of fake Maxim parts about. However you may be lucky.
 

Rick100

Senior Member
I would put a meter on the TX pin without the picaxe connected and make sure when characters are sent from the PC, the voltage level on the pin changes. It's possible the RX and TX pins are reversed. That would explain why the loop back test works.

Good luck,
Rick
 

rossko57

Senior Member
I wonder if there is buffering going on in .net. Is this a delay, or do you need to poke the key multiple times?
 

inglewoodpete

Senior Member
I think your next step would be to have the PICAXE echo the characters back to the sending terminal.

Ie Use a SerOut command immediately after the SerIn.
 

hippy

Technical Support
Staff member
If there are polarity issues they will likely affect what is received and what is sent back. It may be better to start with flashing a LED whenever anything is received, regardless of what is received. That should help verify if data is being received immediately the on-screen icon is clicked.
 
Top