Serial read problem on mac only

I have the following code in the “data available” event in a serial control:

  EditField2.TEXT=Serial1.ReadAll
  
  IF EditField2.TEXT="A" then
    thread1.run
    thread3.kill
  END IF
  
  
  IF EditField2.TEXT="B" THEN 'end lane 1
    thread1.kill
    thread3.run
  END IF

This code works fine on my pc but not on my mac. I can see the data, which is the letter “A” flicker in editfield2 on my mac, but it wont trigger my thread that I have in my “if statement”. Every once in a while it will “see” the “A” and trigger but mostly it doesn’t. It is as if it is happening too fast. Any ideas??? I am testing on a dell pc laptop (works fine) and a macbook pro (does not work).

Just for debugging, change your first line to

EditField2.TEXT=EncodeHex(Serial1.ReadAll)

You may be getting extra, nonprintable characters. Also, try Trim’ing the result.

EditField2.TEXT=Trim(Serial1.ReadAll)

Another thought: an editfield is a terrible place for string storage. It can modify the string. Use a local variable for your actual tests, and shove it into the editfield just for display.

Ok, thanks for the tips, but it still isn’t working…

The trim made it better, it works sometimes, but not all the time. I have this connected to an arduino board connected to an IR timing gate. On my pc I can drop a pen across the beam and it trigger every time. On my mac I can see that data comming in the serial port everytime, but it is like it is ignoring it half the time. Sometimes it trigger just fine, other times I can hold my hand in from of the beam, I can see the data is there, but it just doens’t do anything.

I changed my code so I am now using a variable to read the serial data instead of the editfield. I was just using the editfield for debugging.

any other ideas?

I am also using realbasic 2007 release 3 standard edition.

2007 ???
There’s 7 years of bug fixes & OS changes/patches that could easily be causing you issues

maybe, but my 2006 version on my pc is working fine…I just can’t justify the $300 upgrade at this point in time. But when I can I will.

Just a heads up

Windows has been much more stagnant in terms of not making huge changes like OS X has over that period
Basically MS has put new API’s in place & new lipstick on it but the old APIs still got bug fixes & updates

OS X less so may break your apps built with 2007 in weird & wonderful ways as Apple fixed bugs & we found we had done things in ways that were not optimal for the Carbon API’s from that vintage

You can try your app in the latest Xojo release for free.

It looks to me as though if you hit more than one key, or have an encoding issue, then you may be getting mor than one character at a time whe doing the readall. Perhaps it’s worth looping on the received string to check each character individually, - or at least check it is only 1 byte long and pop up a debug message if so,

ok, thanks guys. I am downloading xojo and will try it out. It would be great if that was the only issue. If not I will try your other suggestions. I love this forum, you guys have helped me out so many times in the past, it is why I keep coming back!

thanks

running in XOJO with the same issue. Sometimes it works, sometimes it doesn’t. I’m pretty sure I am only getting one charecter. My firmware on the arduino I wrote, and it only sends a single letter “A” when the beam of my photogate is broken.

Have you tried this yet? Depending on handshaking and speed of transmission, it is possible to get more than one “send” in a single DataAvailable event. Possible you’re getting “AA” or “AB”?

here is all of the code in my serial event:

  
  
  EditField2.TEXT=trim(Serial1.ReadAll)
  
  IF EditField2.TEXT="A" and lane1running=false then
    lane1running=true
    thread1.run
    thread3.kill
  END IF
  
  
  IF EditField2.TEXT="1" and lane1running=true THEN 'end lane 1
    lane1running=false
    thread1.kill
    thread3.run
  END IF
  
  IF EditField2.TEXT="B" and lane2running=false then
    lane2running=true
    thread2.run
    thread4.kill
  END IF
  
  
  IF EditField2.TEXT="2" and lane2running=true THEN 'end lane 2
    lane2running=false
    thread2.kill
    thread4.run
  END IF
  

ok, how do I check if it one byte long?

Ok, I tried this…

dim s as string
dim i as integer

  EditField2.TEXT=Serial1.ReadAll
  s=Serial1.ReadAll
  i=len(s)
  if EditField2.text<> "A" then
    MsgBox str(i)
  end if

I can break my timing gate beam with a pencil as fast as I possibly can and it catches it everytime, meaning the msgbox pops up everytime showing that “i” has a value of “0”. however, my threads do not fire off. I really can’t wrap my head around what is going on here…

If I has a value of zero, that means you’re getting an empty string. Which means that trim is removing whatever was there. Try EncodeHex.

s = me.readall
s = EncodeHex (s)
msgbox (s)

What do you get?

I think I spot a bug in this code…

[quote]EditField2.TEXT=Serial1.ReadAll
s=Serial1.ReadAll
i=len(s)[/quote]

You are doing the ReadAll twice …
The first line is emptying the buffer into the text field,
the 2nd line is reading what is now an empty buffer.

Good catch! I=0 is just a red herring. But do the EncodeHex anyway.

Thanks for the help guys. You were right, I was getting more than one “A” at a time. Here is the finished working code.

  dim s as string
  

  s=Serial1.ReadAll
  s=Left(s, 1)
  EditField2.text=s
  
  IF s="A" and lane1running=false then
    lane1running=true
    thread1.run
    thread3.kill
  END IF
  
  
  IF s="1" and lane1running=true THEN 'end lane 1
    lane1running=false
    thread1.kill
    thread3.run
  END IF
  
  IF s="B" and lane2running=false then
    lane2running=true
    thread2.run
    thread4.kill
  END IF
  
  
  IF s="2" and lane2running=true THEN 'end lane 2
    lane2running=false
    thread2.kill
    thread4.run
  END IF