[squeak-dev] Question about serial port communication

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Question about serial port communication

Víctor Casado
Hi everyone,

I'm very new in the world of SmallTalk, so I hope you can help me... I'm working on an application that uploads sketches to an Arduino's board directly without its software (only uploading the .hex file). First of all I found a code based in java that works perfectly and now I'm "translating" into ST.

The problem is: Arduino's responses with a particular bytecode when it receives data (at least that's how works the java-based code), with values 0x14 or 0x10. Before send the data block to the board, the program sends a hello bytecode for check responsivity, which values 0x30 and 0x20. The problem is that the board doesn't recognize the hello bytes, because it doesn't response to it. I made something like this (sorry but I don't have the code at this moment):

sendHello: SerialPort2
| ba |
ba := ByteArray new: 2.
ba at: 1 put: 16r30.
ba at: 1 put: 16r20.
SerialPort2 nextPutAll: ba.

SerialPort2 is a class that I imported from Scratch source code. The port is open correctly, I can see how the board receives data because the RX led blinks, but TX doesn't. I tried several ways to do this but anything worked. If anyone who's worked with Arduino knows what I'm doing wrong, tell me please. I will send the code and more detailed information if needed.

Sorry for my bad english,

Thanks

--
Víctor


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about serial port communication

Jon Hylands
On Sat, 20 Jun 2009 16:37:53 +0200, Víctor C. T. <[hidden email]>
wrote:

> sendHello: SerialPort2
> | ba |
> ba := ByteArray new: 2.
> ba at: 1 put: 16r30.
> ba at: 1 put: 16r20.
> SerialPort2 nextPutAll: ba.

You're putting both bytes at location 1 in the ByteArray, at least in this
code, so you're sending (0x20 0x00), instead of (0x30 0x20)...

Later,
Jon

--------------------------------------------------------------
   Jon Hylands      [hidden email]      http://www.huv.com/jon

  Project: Micro Raptor (Small Biped Velociraptor Robot)
           http://www.huv.com/blog

Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about serial port communication

Víctor Casado
Ups sorry, it was a mistake, the code I have is:

sendHello: SerialPort2
| ba |
ba := ByteArray new: 2.
ba at: 1 put: 16r30.
ba at: 2 put: 16r20.
SerialPort2 nextPutAll: ba.

2009/6/20 Jon Hylands <[hidden email]>
On Sat, 20 Jun 2009 16:37:53 +0200, Víctor C. T. <[hidden email]>
wrote:

> sendHello: SerialPort2
> | ba |
> ba := ByteArray new: 2.
> ba at: 1 put: 16r30.
> ba at: 1 put: 16r20.
> SerialPort2 nextPutAll: ba.

You're putting both bytes at location 1 in the ByteArray, at least in this
code, so you're sending (0x20 0x00), instead of (0x30 0x20)...

Later,
Jon

--------------------------------------------------------------
  Jon Hylands      [hidden email]      http://www.huv.com/jon

 Project: Micro Raptor (Small Biped Velociraptor Robot)
          http://www.huv.com/blog




--
Víctor


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about serial port communication

Jon Hylands
In reply to this post by Víctor Casado
On Sat, 20 Jun 2009 16:37:53 +0200, Víctor C. T. <[hidden email]>
wrote:

> SerialPort2 is a class that I imported from Scratch source code. The port is open correctly, I can see how the board receives data because the RX led blinks, but TX doesn't. I tried several ways to do this but anything worked. If anyone who's worked with Arduino knows what I'm doing wrong, tell me please. I will send the code and more detailed information if needed.

Can you show me how you open the SerialPort?

Later,
Jon

--------------------------------------------------------------
   Jon Hylands      [hidden email]      http://www.huv.com/jon

  Project: Micro Raptor (Small Biped Velociraptor Robot)
           http://www.huv.com/blog

Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about serial port communication

Víctor Casado

Sure:

| names sp |
names := SerialPort2 portNames.
names do: [:each | Transcript show: each; cr]. "List of ports"
sp := SerialPort2 new.
sp openPortNamed: names first baud: 115200.
(Delay forMilliseconds:2000) wait.
sp isOpen ifTrue: [
...
].
sp close.

Is it correct? I don't get any error at this point...

2009/6/20 Jon Hylands <[hidden email]>
On Sat, 20 Jun 2009 16:37:53 +0200, Víctor C. T. <[hidden email]>
wrote:

> SerialPort2 is a class that I imported from Scratch source code. The port is open correctly, I can see how the board receives data because the RX led blinks, but TX doesn't. I tried several ways to do this but anything worked. If anyone who's worked with Arduino knows what I'm doing wrong, tell me please. I will send the code and more detailed information if needed.

Can you show me how you open the SerialPort?

Later,
Jon

--------------------------------------------------------------
  Jon Hylands      [hidden email]      http://www.huv.com/jon

 Project: Micro Raptor (Small Biped Velociraptor Robot)
          http://www.huv.com/blog




--
Víctor


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about serial port communication

Jon Hylands
On Sat, 20 Jun 2009 21:39:37 +0200, Víctor C. T. <[hidden email]>
wrote:

> Is it correct? I don't get any error at this point...

I don't see any code to specify the # of data bits, # of stop bits, or the
parity. Do they default to the "normal" values (8N1)?

Also, you need to make sure the port itself is set up without any hardware
flow control - I've never used an Arduino, but I assume its providing a USB
interface that ends up using an FT232 or something like that.

That serial port class is different than the one I use for my robotics
stuff. I've attached the serial port class I use, and it is set up like
this:

        | serialPort baudRate comPortNumber |
        baudRate := 115200.
        comPortNumber := 2.
        serialPort := SerialPort new
                baudRate: baudRate;
                dataBits: 8;
                stopBitsType: 1;
                parityType: 0;
                yourself.

        (serialPort openPort: comPortNumber) isNil
                ifTrue: [ ^self error: 'COM port not available' ].

        ...
        serialPort close.

Later,
Jon

--------------------------------------------------------------
   Jon Hylands      [hidden email]      http://www.huv.com/jon

  Project: Micro Raptor (Small Biped Velociraptor Robot)
           http://www.huv.com/blog



SerialPort.st (8K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about serial port communication

Víctor Casado
Certainly the class SerialPort2 that I use doesn't have any method with these initialization parameters, I didn't realize it... (this is a bit embarrassing...) :P

Well, I'll prove your code tomorrow at work. Thanks a lot for your help Jon!!!

2009/6/20 Jon Hylands <[hidden email]>
On Sat, 20 Jun 2009 21:39:37 +0200, Víctor C. T. <[hidden email]>
wrote:

> Is it correct? I don't get any error at this point...

I don't see any code to specify the # of data bits, # of stop bits, or the
parity. Do they default to the "normal" values (8N1)?

Also, you need to make sure the port itself is set up without any hardware
flow control - I've never used an Arduino, but I assume its providing a USB
interface that ends up using an FT232 or something like that.

That serial port class is different than the one I use for my robotics
stuff. I've attached the serial port class I use, and it is set up like
this:

       | serialPort baudRate comPortNumber |
       baudRate := 115200.
       comPortNumber := 2.
       serialPort := SerialPort new
               baudRate: baudRate;
               dataBits: 8;
               stopBitsType: 1;
               parityType: 0;
               yourself.

       (serialPort openPort: comPortNumber) isNil
               ifTrue: [ ^self error: 'COM port not available' ].

       ...
       serialPort close.

Later,
Jon

--------------------------------------------------------------
  Jon Hylands      [hidden email]      http://www.huv.com/jon

 Project: Micro Raptor (Small Biped Velociraptor Robot)
          http://www.huv.com/blog






--
Víctor


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about serial port communication

Víctor Casado
Sorry but this code didn't work for me, cause I'm on a Mac and it can't get port numbers, only names. Anyway initialization is not the problem: this SerialPort2 class uses two methods called getOption: and setOption:to: which get and set these port parameters (baud rate, data bits, flow control... even handshake settings). Now after starting the program I'm getting default settings on a transcript window and they seem ok:

1. baud rate: 115200
2. data bits: 8
3. stop bits: 1
4. parity type: 0
5. input flow control type: 0
6. output flow control type: 0

20. DTR: 1
21. RTS: 1
22. CTS: 0
23. DSR: 0
24. CD: 0
25. RI: 0

Then that's not the problem... I guess that the way I'm sending bytecodes it's not correct, because there's no response from board.

Thanks again :)

El 21 de junio de 2009 14:12, Víctor C. T. <[hidden email]> escribió:
Certainly the class SerialPort2 that I use doesn't have any method with these initialization parameters, I didn't realize it... (this is a bit embarrassing...) :P

Well, I'll prove your code tomorrow at work. Thanks a lot for your help Jon!!!

2009/6/20 Jon Hylands <[hidden email]>
On Sat, 20 Jun 2009 21:39:37 +0200, Víctor C. T. <[hidden email]>

wrote:

> Is it correct? I don't get any error at this point...

I don't see any code to specify the # of data bits, # of stop bits, or the
parity. Do they default to the "normal" values (8N1)?

Also, you need to make sure the port itself is set up without any hardware
flow control - I've never used an Arduino, but I assume its providing a USB
interface that ends up using an FT232 or something like that.

That serial port class is different than the one I use for my robotics
stuff. I've attached the serial port class I use, and it is set up like
this:

       | serialPort baudRate comPortNumber |
       baudRate := 115200.
       comPortNumber := 2.
       serialPort := SerialPort new
               baudRate: baudRate;
               dataBits: 8;
               stopBitsType: 1;
               parityType: 0;
               yourself.

       (serialPort openPort: comPortNumber) isNil
               ifTrue: [ ^self error: 'COM port not available' ].

       ...
       serialPort close.

Later,
Jon

--------------------------------------------------------------
  Jon Hylands      [hidden email]      http://www.huv.com/jon

 Project: Micro Raptor (Small Biped Velociraptor Robot)
          http://www.huv.com/blog






--
Víctor



--
Víctor


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about serial port communication

Víctor Casado
I'm sending SerialPort2 if you want to take a look on it... Also I can send the java code if you want.

Regards

El 23 de junio de 2009 11:06, Víctor C. T. <[hidden email]> escribió:
Sorry but this code didn't work for me, cause I'm on a Mac and it can't get port numbers, only names. Anyway initialization is not the problem: this SerialPort2 class uses two methods called getOption: and setOption:to: which get and set these port parameters (baud rate, data bits, flow control... even handshake settings). Now after starting the program I'm getting default settings on a transcript window and they seem ok:

1. baud rate: 115200
2. data bits: 8
3. stop bits: 1
4. parity type: 0
5. input flow control type: 0
6. output flow control type: 0

20. DTR: 1
21. RTS: 1
22. CTS: 0
23. DSR: 0
24. CD: 0
25. RI: 0

Then that's not the problem... I guess that the way I'm sending bytecodes it's not correct, because there's no response from board.

Thanks again :)

El 21 de junio de 2009 14:12, Víctor C. T. <[hidden email]> escribió:

Certainly the class SerialPort2 that I use doesn't have any method with these initialization parameters, I didn't realize it... (this is a bit embarrassing...) :P

Well, I'll prove your code tomorrow at work. Thanks a lot for your help Jon!!!

2009/6/20 Jon Hylands <[hidden email]>
On Sat, 20 Jun 2009 21:39:37 +0200, Víctor C. T. <[hidden email]>

wrote:

> Is it correct? I don't get any error at this point...

I don't see any code to specify the # of data bits, # of stop bits, or the
parity. Do they default to the "normal" values (8N1)?

Also, you need to make sure the port itself is set up without any hardware
flow control - I've never used an Arduino, but I assume its providing a USB
interface that ends up using an FT232 or something like that.

That serial port class is different than the one I use for my robotics
stuff. I've attached the serial port class I use, and it is set up like
this:

       | serialPort baudRate comPortNumber |
       baudRate := 115200.
       comPortNumber := 2.
       serialPort := SerialPort new
               baudRate: baudRate;
               dataBits: 8;
               stopBitsType: 1;
               parityType: 0;
               yourself.

       (serialPort openPort: comPortNumber) isNil
               ifTrue: [ ^self error: 'COM port not available' ].

       ...
       serialPort close.

Later,
Jon

--------------------------------------------------------------
  Jon Hylands      [hidden email]      http://www.huv.com/jon

 Project: Micro Raptor (Small Biped Velociraptor Robot)
          http://www.huv.com/blog






--
Víctor



--
Víctor



--
Víctor



System-Serial Port.st (18K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about serial port communication

Jon Hylands
In reply to this post by Víctor Casado
On Tue, 23 Jun 2009 11:06:29 +0200, Víctor C. T. <[hidden email]>
wrote:

> Sorry but this code didn't work for me, cause I'm on a Mac and it can't get port numbers, only names.

Actually, given that you're running a form of Unix, you can probably do a
soft link to the actual port in the form that Squeak is expecting. I've
done that with the gumstix (which runs Linux).

So, to make a link for ttyUSB0 so you can reference it as port 4, do this:

ln -s /dev/ttyUSB0 /dev/ttyS4

Then you can pass in 4 as the port number, and it should work.

> Then that's not the problem... I guess that the way I'm sending bytecodes it's not correct, because there's no response from board.

It may end up being an endian problem - Macs and PCs are opposite. I can't
imagine that this would manifest with a ByteArray, but its probably worth a
try just to rule it out.

Try sending a ByteArray with the bytes reversed... The other thing to try
is to send the bytes individually, with a slight (say 1 ms) delay in
between each byte.

Later,
Jon

--------------------------------------------------------------
   Jon Hylands      [hidden email]      http://www.huv.com/jon

  Project: Micro Raptor (Small Biped Velociraptor Robot)
           http://www.huv.com/blog

Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about serial port communication

Víctor Casado
Finally I tried your previous solution on a laptop with Windows but doesn't work... Also I send the bytes reversed, and with a delay between but doesn't work too.

Thanks a lot anyway!

2009/6/23 Jon Hylands <[hidden email]>
On Tue, 23 Jun 2009 11:06:29 +0200, Víctor C. T. <[hidden email]>
wrote:

> Sorry but this code didn't work for me, cause I'm on a Mac and it can't get port numbers, only names.

Actually, given that you're running a form of Unix, you can probably do a
soft link to the actual port in the form that Squeak is expecting. I've
done that with the gumstix (which runs Linux).

So, to make a link for ttyUSB0 so you can reference it as port 4, do this:

ln -s /dev/ttyUSB0 /dev/ttyS4

Then you can pass in 4 as the port number, and it should work.

> Then that's not the problem... I guess that the way I'm sending bytecodes it's not correct, because there's no response from board.

It may end up being an endian problem - Macs and PCs are opposite. I can't
imagine that this would manifest with a ByteArray, but its probably worth a
try just to rule it out.

Try sending a ByteArray with the bytes reversed... The other thing to try
is to send the bytes individually, with a slight (say 1 ms) delay in
between each byte.

Later,
Jon

--------------------------------------------------------------
  Jon Hylands      [hidden email]      http://www.huv.com/jon

 Project: Micro Raptor (Small Biped Velociraptor Robot)
          http://www.huv.com/blog




--
Víctor


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about serial port communication

Stephen Pair
In reply to this post by Jon Hylands
On Tue, Jun 23, 2009 at 8:55 AM, Jon Hylands <[hidden email]> wrote:
It may end up being an endian problem - Macs and PCs are opposite.

Slight correction...x86(Intel) and PowerPCs are opposite.  Modern Macs use Intel processors and hence would have the same endianness as Windows PCs.

- Stephen